android - 获取 android 订阅状态​​,失败并返回 403

标签 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 Playground ,但没有成功 ( https://developers.google.com/oauthplayground/ )

最佳答案

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

我花了几个小时查看 Google Developer Console,但链接项目的地方是在 Google Play Developer Console 中。

只需转到设置 -> Api 访问,您就可以链接您在 Google 开发者控制台中创建的项目。

这是一个用于获取订阅状态的有效 PHP 解决方案。您需要在 Google Developer Console -> 'your project' -> API & Auth -> Credential 中创建一个服务帐户,然后从 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;

关于android - 获取 android 订阅状态​​,失败并返回 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25054919/

相关文章:

reporting-services - SSRS 数据驱动订阅中没有电子邮件选项

android - Emma 不生成 coverage.ec

android - 如何将 repo 镜像同步到新的上游 repo

android - Google Analytics Activity 跟踪未显示在报告中

c# - 当我在 c# 中使用 gmail API 修改消息标签时出现问题

http - 角度 5 HTTPClient/HTTPHeader 'Preflight response is not successful'

javascript - 有没有可以使用 google api 来监控 google 电子表格中的更改的服务?

paypal - 使用 PayPal 订阅 API 收取税款

Angular Reactive Form - 订阅表单数组中的表单控件实例以获取总值

android - 使用 MapView 之外的站点线返回相对于用户的 GeoPoint