获取 android 订阅状态,失败 403 [英] Get android subscription status, failed with 403
问题描述
在尝试获取 android 应用内订阅状态(带有到期日)时,我收到以下错误消息:
<代码>{错误": {错误":[{"domain": "androidpublisher","reason": "projectNotLinked","message": "用于调用 Google Play Developer API 的项目 ID 尚未在 Google Play Developer Console 中链接."}],代码":403,"message": "用于调用 Google Play Developer API 的项目 ID 尚未在 Google Play Developer Console 中链接."}}
它说项目 ID 未链接.我做了以下事情:
<块引用>1.在 Google Developer Console 中创建项目.2. 打开 Google Android Publisher API.3. 链接API访问页面中提到的项目ID (https://developers.google.com/android-publisher/getting_started)4. 填写packageName、productId(来自app-purchase)、purchase_token(来自Android app)
不知道为什么会出现如上的错误消息.我尝试过 OAuth2 游乐场,但没有成功(https://developers.google.com/oauthplayground/)
我花了一些时间来解决这个问题,但最后错误消息说明了一切:[...] 尚未在 Google Play 开发者控制台中链接".
我花了几个小时在 Google 开发者控制台中查看,但链接项目的位置在 Google Play 开发者控制台中.
只需转到设置"->Api 访问",您就可以链接您在 Google Developer Console 中创建的项目.
这是获取订阅状态的有效 PHP 解决方案.您需要在 Google Developer Console -> '您的项目' -> API & 中创建一个服务帐户Auth -> Credential 并从 https://github.com/google/google-api- 下载 Google API PHP 客户端php客户端
set_include_path("../src/" .PATH_SEPARATOR .get_include_path());require_once 'Google/Client.php';require_once 'Google/Service/AndroidPublisher.php';$client_id = '';//您的客户ID$service_account_name = '';//您的服务帐户电子邮件$key_file_location = '';//你的p12文件(key.p12)$client = new Google_Client();$client->setApplicationName("");//这是链接的应用程序的名称$service = new Google_Service_AndroidPublisher($client);$key = file_get_contents($key_file_location);$cred = 新的 Google_Auth_AssertionCredentials($service_account_name,数组('https://www.googleapis.com/auth/androidpublisher'),$key);$client->setAssertionCredentials($cred);if($client->getAuth()->isAccessTokenExpired()) {$client->getAuth()->refreshTokenWithAssertion($cred);}$apiKey = "";//您的API密钥$client->setDeveloperKey($apiKey);$package_name = "";//你的包名(com.example...)$subscriptionId = "";//订阅商品的SKU//购买后返回给app的token$token = "";$service = new Google_Service_AndroidPublisher($client);$results = $service->purchases_subscriptions->get($package_name,$subscriptionId,$token,array());print_r ($results);//这个对象有关于订阅的所有数据回声到期:".$results->expiryTimeMillis;出口;
While trying to get android in-app subscription status (with expiry date), I get the following error message:
{
"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."
}
}
It says the project id is not linked. I have done the following:
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)
Not sure why the error message appear as above. I have tried with the OAuth2 playground with no luck (https://developers.google.com/oauthplayground/)
I spent some time on this but at the end the error message says all: "[...] has not been linked in the Google Play Developer Console".
I spent hours looking in the Google Developer Console, but the place where to link the project is in the Google Play Developer console.
Just go to Settings -> Api Access and you'll be able to link the project you created in the Google Developer Console.
Here is a working PHP solution to get the subscription status. You need to create a service account in the Google Developer Console -> 'your project' -> API & Auth -> Credential and download the Google API PHP Client from https://github.com/google/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;
这篇关于获取 android 订阅状态,失败 403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!