简体   繁体   English

通过API检索有关Google Play的应用内购买订阅的信息

[英]Retrieve info about a in-app purchase subscription of Google Play via API

Calling Google API got this message: 调用Google API会收到以下消息:

    {
  "error": {
    "errors": [
      {
        "domain": "androidpublisher",
        "reason": "permissionDenied",
        "message": "The current user has insufficient permissions to perform the requested operation."
      }
    ],
    "code": 401,
    "message": "The current user has insufficient permissions to perform the requested operation."
  }
}

or this error message ( ADDED ): 或此错误消息( ADDED ):

{
 "error": {
  "errors": [
   {
    "domain": "androidpublisher",
    "reason": "projectNotLinked",
    "message": "The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console."
   }
  ],
  "code": 403,
  "message": "The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console."
 }
}

I follow all indications I found and I keep having this error. 我遵循发现的所有迹象,并且一直出现此错误。


ON MY SYSTEM 在我的系统上

My code 我的密码

        try {
            ini_set('max_execution_time', 3000);
            $client = new Google_Client();

            if ($credentials_file = $this->checkServiceAccountCredentialsFilePlay()) {
                // set the location manually
                $client->setAuthConfig($credentials_file);
            } elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
                // use the application default credentials
                $client->useApplicationDefaultCredentials();
            } else {
                $rv= "missingServiceAccountDetailsWarning()";
                return [$rv];
            }

            $client->addScope("https://www.googleapis.com/auth/androidpublisher");
            $serviceAndroidPublisher = new \Google_Service_AndroidPublisher($client);
            $servicePurchaseSubscription = $serviceAndroidPublisher->purchases_subscriptions;

            $rv = $servicePurchaseSubscription->get(
                "com.my.app",
                "sub1month",
                "ajgbkxxxxxxxxx.AO-J1OxTOKENTOKENTOKEN-"
            );

        } catch (\Exception $e) {
            return $e->getMessage();
        }

The credential file 凭证文件

{
  "type": "service_account",
  "project_id": "project-id",
  "private_key_id": "abababababababababababababababababa",
  "private_key": "-----BEGIN PRIVATE KEY-----KEYBASE64=\n-----END PRIVATE KEY-----\n",
  "client_email": "google-play-account@project-id.iam.gserviceaccount.com",
  "client_id": "123450000000000000000",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/google-play-account%40project-id.iam.gserviceaccount.com"
}

ON GOOGLE PLAY CONSOLE 在Google Play控制台上

I link the project to the google play console 我将项目链接到Google Play控制台 在此处输入图片说明

I add the Service account to the google play console 我将服务帐户添加到Google Play控制台 在此处输入图片说明

It's present in the user menu of google play console 它存在于Google Play控制台的用户菜单中 在此处输入图片说明

And I give him all permission: 我完全允许他: 在此处输入图片说明


ON GOOGLE API DEVELOPER CONSOLE 在GOOGLE API开发人员控制台上

ADD FYI : my "project-id" is under an organization. 仅供参考 :我的“项目ID”在某个组织下。

In google developer console I gave all possible permission to the service account: 在Google Developer Console中,我将所有可能的权限授予了该服务帐户: 在此处输入图片说明

And of course I've enabled the google Play Android Developer Api (showing my failures): 当然,我已经启用了Google Play Android开发者Api(显示我的失败): 在此处输入图片说明

I had the same problem. 我有同样的问题。

In my case, I was using the google app engine (python) as a backend service. 就我而言,我使用的是Google App Engine(python)作为后端服务。 In the beginning, I linked a new Google cloud project in the Google Play Console. 首先,我在Google Play控制台中链接了一个新的Google云项目。 I'm not entirely sure if it's the main reason that I got a 401 'insufficient permissions' error, but after switching the linked project to my cloud project (which I used for the app engine) it worked the next day. 我不完全确定这是否是我收到401“权限不足”错误的主要原因,但是在将链接的项目切换到我的云项目(用于应用程序引擎)后,第二天它就可以工作了。 Right after switching accounts, I received the 403 'projects not linked' error. 切换帐户后,我立即收到403个“未链接的项目”错误。 So, I'm guessing that Google doesn't recognize the change of the linked projects immediately and you need to wait for a few hours. 因此,我猜想Google无法立即识别出所链接项目的更改,因此您需要等待几个小时。

If you are using app engine, you have to make sure that your default service account for the app engine has a JSON credential file. 如果您使用的是App Engine,则必须确保您的App Engine的默认服务帐户具有JSON凭证文件。 It's not created by default. 默认情况下未创建。

Here is the python code: 这是python代码:

        if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
            # production environment
            credentials = oauth2client.contrib.appengine.AppAssertionCredentials(scope='https://www.googleapis.com/auth/androidpublisher')
            http = credentials.authorize(httplib2.Http(memcache))
            response = googleapiclient.discovery.build('androidpublisher', 'v3').purchases().subscriptions()\
                .get(packageName=package_name, subscriptionId=subscription.product_id, token=subscription.token)\
                .execute(http)
        else:
            # local environment
            # setting the scope is not needed because the api client handles everything automatically
            credentials = google.oauth2.service_account.Credentials.from_service_account_file('local_dev.json')
            response = googleapiclient.discovery.build('androidpublisher', 'v3', credentials=credentials).purchases().subscriptions()\
                .get(packageName=package_name, subscriptionId=subscription.product_id, token=subscription.token)\
                .execute()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM