【问题标题】:Get android subscription status, failed with 403获取android订阅状态,403失败
【发布时间】:2014-09-23 03:54:55
【问题描述】:

在尝试获取 android 应用内订阅状态(带有到期日期)时,我收到以下错误消息:

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

网址是:https://www.googleapis.com/androidpublisher/v2/applications/[packageName]/inapp/[productId]/purchases/[purchase_token]?access_token=[access_token]

它说项目 ID 未链接。我做了以下事情:

1. Create the project in Google Developer Console.
2. Turn on the Google Android Publisher API.
3. Link the project ID as mentioned in API access page (https://developers.google.com/android-publisher/getting_started)
4. Fill in the packageName, productId (from in app-purchase), purchase_token (from Android app)

不知道为什么会出现如上的错误消息。我尝试过 OAuth2 游乐场,但没有成功 (https://developers.google.com/oauthplayground/)

【问题讨论】:

    标签: android google-api subscription in-app


    【解决方案1】:

    我在这上面花了一些时间,但最后错误消息说明了一切: “[...] 尚未在 Google Play 开发者控制台中链接”。

    我花了几个小时查看 Google 开发者控制台,但链接项目的地方是 Google Play 开发者控制台。

    只需转到 Settings -> Api Access,您就可以链接您在 Google Developer Console 中创建的项目。

    这是获取订阅状态的有效 PHP 解决方案。您需要在 Google Developer Console -> 'your project' -> API & Auth -> Credential 中创建一个服务帐户,然后从 https://github.com/google/google-api-php-client 下载 Google API PHP Client

        set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
        require_once 'Google/Client.php';
        require_once 'Google/Service/AndroidPublisher.php';
    
        $client_id = '';    //Your client id
        $service_account_name = '';  //Your service account email
        $key_file_location = ''; //Your p12 file (key.p12)
    
        $client = new Google_Client();
        $client->setApplicationName(""); //This is the name of the linked application
        $service = new Google_Service_AndroidPublisher($client);
    
        $key = file_get_contents($key_file_location);
        $cred = new Google_Auth_AssertionCredentials(
            $service_account_name,
            array('https://www.googleapis.com/auth/androidpublisher'),
            $key
        );
        $client->setAssertionCredentials($cred);
        if($client->getAuth()->isAccessTokenExpired()) {
          $client->getAuth()->refreshTokenWithAssertion($cred);
        }        
        $apiKey = ""; //Your API key
        $client->setDeveloperKey($apiKey);
    
        $package_name = ""; //Your package name (com.example...)
        $subscriptionId = "";   //SKU of your subscription item
    
        //Token returned to the app after the purchase
        $token = "";
    
        $service = new Google_Service_AndroidPublisher($client);
        $results = $service->purchases_subscriptions->get($package_name,$subscriptionId,$token,array());
    
        print_r ($results); //This object has all the data about the subscription
        echo "expiration: " . $results->expiryTimeMillis;
        exit;
    

    【讨论】:

    • 谢谢,你帮我省了很多麻烦。简直不敢相信设置起来有多么困难!
    • 您是否能够确认使用 v2 api 您只能使用服务帐户获取购买列表?
    • 附加信息:开发者控制台 API 项目(和 oauth2 凭据)必须使用与 Google Play 开发者控制台中使用的相同的 Google 帐户创建。否则,您无法从 Developer Console API 链接特定项目。
    • 如何获取我的p12文件(key.p12)?
    【解决方案2】:

    我也有同样的问题。似乎 v2 api 中的身份验证已更改。

    您仍然可以使用旧的 api 端点 (v1.1)。它对我来说很好。

    https://www.googleapis.com/androidpublisher/v1.1/applications/{packageName}/inapp/{productId}/purchases/{token}

    【讨论】:

    • 嘿,您确定您对具有正确权限的用户进行身份验证吗?
    • 'package name" ,productID, 但是这里的 {token} 是什么,我将从哪里得到它
    【解决方案3】:

    现在是 2019 年,这个问题仍然存在。但是,我在某个地方的另一个答案中发现,有人说您必须在设置此应用程序后在您的帐户中创建一个新产品……而 实际上对我有用。

    【讨论】:

    • 感谢您提供创建新产品的提示。就我而言,不需要创建新产品,只需在开发者控制台中更新产品即可。
    【解决方案4】:

    我使用了 Fabrizio Farenga 的代码和 Google API 代码,结果很好。此外,值得注意的是,您的服务帐户需要访问它的权限只是查看财务报告

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 2018-04-12
      • 1970-01-01
      • 2017-11-15
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      相关资源
      最近更新 更多