“服务帐户”androidpublisher验证订阅购买 - > 401“此开发人员帐户不拥有该应用程序”

时间:2013-10-09 17:42:04

标签: in-app-purchase in-app-billing google-api-php-client

api版本:google-api-php-client-0.6.7

我需要在我的服务器上通过我的php脚本验证用户从我的Android应用程序购买的Android订阅。

因此,没有用户交互,用户只会向我的服务器发送他购买的令牌,我的服务器将验证是否是有效的购买。
我发现不需要用户交互的唯一方法似乎是“服务帐户”,所以我按照documentation并制作了CLIENT_ID,SERVICE_ACCOUNT_NAME,KEY_FILE进行测试,我使用了来自的真实购买代币我的申请$ ANDROIDUsertoken

这是我的服务器脚本(出于安全考虑,我更改了一些代码部分):

<?php
include_once('../lib/google-api-php-client/src/Google_Client.php');
include_once('../lib/google-api-php-client/src/contrib/Google_AndroidpublisherService.php');

//user token, in json format
$ANDROIDUsertoken = '{"orderId":"....","packageName":"....","productId":"....","purchaseTime":....,"purchaseState":0,"purchaseToken":"...."}';
$user_token= json_decode($ANDROIDUsertoken,true);

// https://developers.google.com/console/help/#service_accounts
const CLIENT_ID = 'something-private.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'something-private@developer.gserviceaccount.com';
const KEY_FILE = 'secret-dir/privatekey.p12';

$client = new Google_Client();
$client->setApplicationName($user_token['packageName']);
$client->setClientId(CLIENT_ID);
$key = file_get_contents(KEY_FILE);

echo "start auth:<br>";

$auth = new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/androidpublisher'),
    $key);

$client->setAssertionCredentials($auth);

//$client->getAuth()->refreshTokenWithAssertion();
//$accessToken=$client->getAccessToken();
//$client->setAccessToken($accessToken);

echo "start requests:<br>";

$AndroidPublisherService = new Google_AndroidPublisherService($client);
$res = $AndroidPublisherService->purchases->get($user_token['packageName'], $user_token['productId'], $user_token['purchaseToken']);

var_dump($res);
?>

错误:

start auth:
start requests:
Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling GET https://www.googleapis.com/androidpublisher/v1.1/applications/..APPLICATION../subscriptions/..PRODUCT../purchases/..TOKEN..: 
(401) This developer account does not own the application.' in /..PATH TO MY WEBSERVER../lib/google-api-php-client/src/io/Google_REST.php on line 66

我以前也成功地在“web应用程序”上使用OAuth + androidpublisher,但这需要用户交互不是我的目标(我需要服务器端检查)

请注意$ AndroidPublisherService-&gt; purchase-&gt;获得订阅

我看到了this帖子,但略有不同,并没有为我工作

1 个答案:

答案 0 :(得分:2)

好的,我找到了解决方案!

您必须将API与您的应用相关联。您必须转到Google Play发布页面(https://play.google.com/apps/publish),然后在“设置” - >“用户帐户”和“用户帐户”中邀请具有服务帐户电子邮件的用户。 rights-&gt;邀请新用户并授予其“#34;查看财务报告&#34;”的权限。

谢谢对我有用,祝你好运!