确定Google用户的域是否安装了我的marketplace应用

bre*_*dev 6 google-apps-marketplace google-oauth

当用户登录时,我想检查他们的域是否安装了我的Marketplace应用.理论上,这应该可以使用Marketplace License API端点.

但是,每当我尝试使用"立即尝试"功能进行客户许可,用户许可许可通知端点时,我总是会收到403 Forbidden,并显示"未授权访问应用程序ID"消息.

例如,如果我尝试查询LicenseNotification端点,我会执行以下操作:

单击"授权"切换,然后单击"授权"以授权我的登录用户(即拥有该应用程序的Google Apps管理员帐户,btw)的范围.

对于applicationId,然后我从旧开发者控制台中的Google Apps Marketplace SDK设置中输入12位"应用ID"字段(也称为新开发者控制台应用概述页面中的项目编号).

当我单击执行时,我得到403"未授权访问应用程序ID".我也尝试使用我的项目ID(即开发人员控制台概述页面中的"我的应用程序")代替项目编号/应用程序ID,并获得相同的响应.

我在这里错过了什么吗?

或者,如果有人知道GAM应用程序的所有者查询已安装它的域列表的另一种方式,那对我来说也是一个理想的解决方案 - 我找不到这样的东西.

bre*_*dev 6

好的,再读一些,最后想出来.

我缺少的是,许可端点的身份验证要求您使用服务帐户,而不是常规用户帐户.这就是为什么文档页面上的"立即尝试"功能根本不起作用的原因.

不幸的是,我们使用PHP并且google-api-php-client还没有为Licensing API提供服务.但是,客户端项目确实显示使用服务帐户而不是普通用户OAuth2流的示例.

我使用了这个例子,从Resource.phpcall方法中窃取了一些源代码来调用Customer License端点来检查域是否安装了我们的应用程序:

$privateKey = file_get_contents('path/to/private-key.p12');
$serviceAccountName = '12345-j@developer.gserviceaccount.com';

$cred = new \Google_Auth_AssertionCredentials(
    $serviceAccountName,
    array('https://www.googleapis.com/auth/appsmarketplace.license'),
    $privateKey
);
$client = new \Google_Client();
$client->setApplicationName('Apps_Marketplace_Licensing_Check');
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}


$url = \Google_Http_REST::createRequestUri(
    'appsmarket/v2/',
    'customerLicense/{appId}/{customerId}', [
        'appId' => ['location' => 'path', 'type' => 'string', 'value' => $appId], 
        'customerId' => ['location' => 'path', 'type' => 'string', 'value' => $domain]
    ]
);

$httpRequest = new \Google_Http_Request($url, 'GET');
$httpRequest->setBaseComponent($client->getBasePath());
$httpRequest = $client->getAuth()->sign($httpRequest);

/* returns JSON array */
$result = $client->execute($httpRequest);
$isDomainInstalled = ($result && isset($result['state']) && $result['state'] == 'ACTIVE');
Run Code Online (Sandbox Code Playgroud)

希望google-api-php-client项目中的人们最终会为这些端点添加真正的服务,但是现在这种解决方法并不是非常痛苦.