2014-06-20 51 views
9

我在最后的日子里尝试了很多事情,但现在我的想法:(使用服务帐户来验证谷歌InAppPurchase

我想验证已经在我的Android应用已经取得了inAppPurchase。

1)我在google API控制台中创建了一个新的服务帐户。

1A)服务帐户列出的Unter权限和具有“可以查看”的权限

2)我使用https://github.com/google/google-api-php-client

3)代码段的最新版本,从我的PHP脚本:

$client = new Google_Client(); 
$client->setApplicationName('myAppName'); 
$client->setClientId('123456789123-vxoasdt8qwe6awerc9ysdfmjysdfysf64werweria8fh.apps.googleusercontent.com'); 
$key = file_get_contents('/shr/data/stor/b516cexx3123asdf3988345d8133e7f86bfas2553-privatekey.p12'); 
$service_account_name = '[email protected]per.gserviceaccount.com'; 

$client->setScopes(array('https://www.googleapis.com/auth/androidpublisher')); 
$cred = new Google_Auth_AssertionCredentials($service_account_name, array('https://www.googleapis.com/auth/androidpublisher'), $key); 
$client->setAssertionCredentials($cred); 

try { 
    $service = new Google_Service_AndroidPublisher($client); 
    $googleApiResult = $service->inapppurchases->get($externalAppId, $externalProductId, $purchaseToken); 
} catch (Exception $e) { 
    var_dump($e->getMessage()); 
} 
从谷歌

4)响应:

GET https://www.googleapis.com/androidpublisher/v1.1/applications/de.test.myapp/inapp/de.test.inapp.google.balance5eur/purchases/[PURCHASETOKEN]: (401)当前用户没有足够的权限执行请求的操作 。

[PURCHASETOKEN]我是从谷歌收到的购买令牌

5)设置$ cred->子= '[email protected]' 到我的邮箱地址带来了

错误刷新OAuth2令牌,消息:'{“error”: “unauthorized_client”,“error_description”:“未经授权的客户端或请求中的 范围。 }'

回答

29

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

您必须将您的API与您的应用程序连接起来。您必须转到您的Google Play发布页面(https://play.google.com/apps/publish),并邀请具有服务帐户电子邮件的用户在设置 - >用户帐户&权限 - >邀请新用户并授予其“查看财务报告”权限。

这意味着,您通常可能会看到Google Play控制台(电子邮件地址:[email protected],角色:财务;电子邮件地址:[email protected])中列出的您域中的多个用户,角色:管理员),您现在将添加另一个用户并给他一个财务角色(电子邮件:[email protected],角色:财务)。

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

+2

你救了我的一天!非常感谢你!为什么这不在Google文档中记录? – glutorange

+0

太棒了,谢谢!你刚从头痛中拯救了我...... – Mike

+0

不幸的是,这并不能解决我的问题。相同的错误响应。 “将您的API与您的应用连接起来”是什么意思?那是添加服务帐户电子邮件吗? –

-1

我认为你必须使用OAuth 2.0 Web客户端。

+0

这应该是一个评论 – NSNoob

+0

应该是一个评论。 –

相关问题