使用服务帐户验证Google InAppPurchase

glu*_*nge 16 php api android publisher google-api-php-client

我在最后几天尝试了很多东西,但现在我没有想法:(

我想验证在我的Android应用程序中创建的inAppPurchase.

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

1a)服务帐户列出了非权限,并具有"可以查看"权限

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 = '123456789123-vxoasdt8qwe6awerc9ysdfmjysdfysf64werweria8fh@developer.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() ); 
}
Run Code Online (Sandbox Code Playgroud)

4)Google的回复:

获取https://www.googleapis.com/androidpublisher/v1.1/applications/de.test.myapp/inapp/de.test.inapp.google.balance5eur/purchases/[PURCHASETOKEN] :( 401)当前用户已拥有权限不足以执行请求的操作.

[PURCHASETOKEN]是我从谷歌收到的购买代币

5)将$ cred-> sub ='foo@bar.de'设置为我的邮件地址

刷新OAuth2令牌时出错,消息:'{"error":"unauthorized_client","error_description":"请求中未经授权的客户端或范围".}"

fri*_*ser 38

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

您必须将API与应用程序相关联.您必须转到Google Play发布页面(https://play.google.com/apps/publish)并在设置 - >用户帐户和权限 - >邀请新用户中邀请具有服务帐户电子邮件的用户并将其提供给"查看财务报告"的权限.

这意味着,您通常可能会在Google Play控制台中看到您域中的许多用户(电子邮件:user1@mydomain.com,角色:财务;电子邮件:user2 @mydomain.com,角色:管理员),您将现在添加另一个用户并给他一个财务角色(电子邮件:XXX@developer.gserviceaccount.com,角色:财务).

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

  • 你救了我的一天!非常感谢你!为什么Google文档中没有记录这些内容? (3认同)
  • @BobMitchell"将您的API与您的应用程序连接"意味着"授权您的应用程序通过Google Api"获取您拥有并接收购买的Google Play发布帐户的'财务'信息(如应用内购买和应用内购买) .因此,您必须在Google Play发布商页面中为您用作clientid(此问题中的"123456789123-vxoasdt8qwe6awerc9ysdfmjysdfysf64werweria8fh.apps.googleusercontent.com")的用户授予权限,以访问财务信息.我希望它对你有所帮助. (2认同)