问题描述
用户登录后,我要检查其域是否安装了我的Marketplace应用.从理论上讲, Marketplace License API端点似乎应该可以实现.
When a user logs in, I want to check if their domain has my Marketplace app installed. It looks like this should in theory be possible with the Marketplace License API endpoints.
但是,只要我尝试对客户许可使用立即尝试"功能,用户许可或端点,我总是收到403 Forbidden,消息为未授权访问应用程序ID".
However, whenever I try to use the "Try it now" feature for the Customer License, User License or License Notification endpoints, I always get a 403 Forbidden with the message "Not authorized to access the application ID".
例如,如果我尝试查询 LicenseNotification 端点,以下:
For example, if I try to query the LicenseNotification endpoint, I do the following:
单击授权"切换,然后单击授权",以为我登录的用户(拥有该应用程序的Google Apps Admin帐户,顺便说一句)授权该范围.
Click on the Authorize toggle and click "Authorize" to authorize that scope for my logged-in user (which is the Google Apps Admin account which owns the application, btw).
对于applicationId
,然后在旧开发者控制台的Google Apps Marketplace SDK设置中输入12位的应用ID"字段(在新开发者控制台应用概述页面中也称为项目编号").
For applicationId
, I then enter the 12-digit "App Id" field from the Google Apps Marketplace SDK settings in the old developers console (Also known as Project Number from the new developers console app overview page).
当我单击执行"时,我得到403未授权访问应用程序ID".我还尝试使用我的项目ID(即开发者控制台概述页面中的"my-app")代替项目编号/应用ID,并得到相同的响应.
When I click Execute I get the 403 "Not authorized to access the application ID". I have also tried using my Project Id (i.e. "my-app" from the Developers Console Overview page) in place of the Project Number/App Id, and get the same response.
我在这里想念东西吗?
或者,如果有人知道GAM应用程序所有者的另一种查询安装它的域列表的方法,那对我来说也是一个理想的解决方案-我找不到这样的东西.
Alternatively, if someone knows of another way for the owner of a GAM App to query a list of domains which have it installed, that would be an ideal solution for me as well - I couldn't find anything like this.
推荐答案
好吧,请阅读更多内容并最终弄明白这一点.
Okay, read some more and figured this out at last.
我所缺少的是以下事实:授权端点的身份验证要求您使用服务帐户,而不是普通用户帐户.这就是为什么文档页面上的立即尝试"功能根本不起作用的原因.
What I was missing was the fact that the authentication for the Licensing endpoints requires you to use a Service Account, not a regular user account. Which makes sense why the "Try it Now" feature on the documentation pages didn't work at all.
不幸的是,我们使用PHP,而 google-api-php-client 尚无服务用于许可API.但是,客户端项目的确显示了示例使用服务帐户而不是普通用户的OAuth2流.
Unfortunately, we use PHP and the google-api-php-client does NOT yet have services for the Licensing APIs. However, the client project does show an example of using a service account instead of the normal user OAuth2 flow.
我使用了此示例,并从的call
方法,用于调用客户许可端点检查域是否已安装我们的应用:
I used this example and stole a little bit of the source code from Resource.php's call
method to call the Customer License endpoint to check to see if a domain has our app installed or not:
$privateKey = file_get_contents('path/to/private-key.p12');
$serviceAccountName = '[email protected]';
$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项目的人们最终将为这些端点添加真正的服务,但是就目前而言,这种解决方法还不是很痛苦.
Hopefully the folks over at the google-api-php-client project will eventually add a true service for these endpoints, but for now this workaround isn't too terribly painful.
这篇关于确定Google用户的域是否安装了我的市场应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!