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

时间:2018-11-05 23:10:04

标签: php google-api-php-client google-play-developer-api in-app-subscription

调用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."
  }
}

或此错误消息( 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."
 }
}

我遵循发现的所有迹象,并且一直出现此错误。


在我的系统上

我的代码

        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();
        }

凭据文件

{
  "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"
}

在GOOGLE PLAY控制台上

我将项目链接到Google Play控制台 enter image description here

我将服务帐户添加到Google Play控制台 enter image description here

它存在于Google Play控制台的用户菜单中 enter image description here

我给他所有许可: enter image description here


在GOOGLE API开发控制台上

添加FYI :我的“项目ID”在某个组织下。

在google开发者控制台中,我将所有可能的权限授予了该服务帐户: enter image description here

当然,我已经启用了Google Play Android开发者Api(显示我的失败): enter image description here

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。

就我而言,我使用的是Google App引擎(python)作为后端服务。首先,我在Google Play控制台中链接了一个新的Google云项目。我不完全确定这是否是我收到401“权限不足”错误的主要原因,但是在将链接的项目切换到我的云项目(用于应用程序引擎)后,第二天它就可以工作了。切换帐户后,我立即收到403个“未链接的项目”错误。因此,我猜测Google无法立即识别出所链接项目的更改,因此您需要等待几个小时。

如果您使用的是应用引擎,则必须确保您的应用引擎默认服务帐户具有JSON凭证文件。默认情况下未创建。

这是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()