获取“未被授权访问应用程序id”;在使用Google Apps Marketplace API之后


Get "Not authorized to access the application ID" after using Google Apps Marketplace API

我有一个web应用程序,并试图找出哪些用户在谷歌域安装了我的应用程序。我试图从这里使用代码:确定谷歌用户's域是否安装了我的市场应用程序,但它不起作用。我仍然得到错误"(403)未被授权访问应用程序ID"的响应。

代码:

            $private_key = file_get_contents('path_to_p.12_key');
            $service_account_name = '{service_acc_name}'; // name from developers console
            $cred = new Google_Auth_AssertionCredentials($service_account_name, array('https://www.googleapis.com/auth/appsmarketplace.license'), $private_key);
            $client = new Google_Client();
            $client->setAssertionCredentials($cred);
            $url = "https://www.googleapis.com/appsmarket/v2/licenseNotification/{appID}";
            $httpRequest = new Google_Http_Request($url, 'GET');
            $httpRequest->setBaseComponent($client->getBasePath());
            $httpRequest = $client->getAuth()->sign($httpRequest);
            try
            {
                    $result = $client->execute($httpRequest);
            }
            catch (Exception $e)
            {
                    echo $e->getMessage();
            }

我还在开发者控制台的项目设置中添加了https://www.googleapis.com/auth/appsmarketplace.license范围。

我不知道怎么了。

好了,我解决了。你必须将应用程序使用的所有作用域添加到服务帐户对象中(而不仅仅是服务帐户真正想要使用的作用域):

$cred = new Google_Auth_AssertionCredentials($service_account_name, array('https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/admin.directory.user.readonly', 'https://www.googleapis.com/auth/admin.directory.user', 'https://www.googleapis.com/auth/appsmarketplace.license'), $private_key);