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

时间:2014-11-29 19:59:30

标签: google-oauth google-apps-marketplace

当用户登录时,我想检查他们的域是否安装了我的Marketplace应用。看来理论上这应该可以用Marketplace License API endpoints来实现。

但是,每当我尝试使用Customer LicenseUser LicenseLicense Notification端点的“立即尝试”功能时,我总是会收到403 Forbidden,并显示“未经授权”访问应用程序ID“。

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

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

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

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

我在这里错过了什么吗?

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

2 个答案:

答案 0 :(得分:6)

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

我遗漏的是,许可端点的身份验证要求您使用Service Account,而不是常规用户帐户。这就是为什么文档页面上的“立即尝试”功能根本不起作用的原因。

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

我使用了这个示例并从Resource.phpcall方法中窃取了一些源代码来调用Customer License endpoint以检查域是否安装了我们的应用或不:

$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');

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

答案 1 :(得分:0)