使用 Google Apps Marketplace API 后获得 "Not authorized to access the application ID"

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

我有一个网络应用程序,并试图找出 google 域中的哪些用户安装了我的应用程序。我尝试使用此处的代码:Determine if a google user's domain has my marketplace app installed,但它不起作用。我仍然收到错误“(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);