获取android订阅状态,403失败

时间:2014-07-31 09:06:29

标签: android google-api subscription in-app

在尝试获取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/

4 个答案:

答案 0 :(得分:12)

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

我花了好几个小时查看Google Developer Console,但链接项目的位置在Google Play 开发者控制台中。

只需转到设置 - > Api Access,您将能够链接您在Google Developer Console中创建的项目。

这是一个有效的PHP解决方案,可以获得订阅状态。您需要在Google Developer Console中创建一个服务帐户 - > '你的项目' - > API&验证 - >凭据并从https://github.com/google/google-api-php-client

下载Google API PHP客户端
    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;

答案 1 :(得分:3)

我遇到了同样的问题。似乎v2 api中的身份验证已更改。

您仍然可以使用旧的api端点(v1.1)。它适用于我。

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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