google play踩坑报错401:
{
"code" : 401,
"errors" : [ {
"domain" : "androidpublisher",
"message" : "The current user has insufficient permissions to perform the requested operation.",
"reason" : "permissionDenied"
} ],
"message" : "The current user has insufficient permissions to perform the requested operation."
}
代码如下:
public function main()
{
$client = new \Google_Client();
$client->useApplicationDefaultCredentials();
if ($credentials_file = $this->checkServiceAccountCredentialsFile()) {
$client->setAuthConfig($credentials_file);
}
$client->setApplicationName("meitupaytest1");
$client->addScope(\Google_Service_AndroidPublisher::ANDROIDPUBLISHER);
/*
$client->setScopes(['https://www.googleapis.com/auth/books']);
$service = new \Google_Service_Books($client);
$optParams = array('filter' => 'free-ebooks');
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
var_dump($results);exit;
*/
$androidPublishService = new \Google_Service_AndroidPublisher($client);
$package_name = 'com.xxxxx';
$product_id = 'xxxxxx';
$purchase_token = 'xxxxxxxxxx';
try {
$result = $androidPublishService->purchases_products->get($package_name, $product_id, $purchase_token);
$result = get_object_vars($result);
} catch (Exception $e) {
echo 'got error = ' . $e->getMessage() . PHP_EOL;exit;
}
}
function checkServiceAccountCredentialsFile()
{
//google下载下来的jsonc串
$application_creds = \Fw\App::getInstance()->getRootPath() . '/src/config/credentials.json';
return file_exists($application_creds) ? $application_creds : false;
}
解决方法:
创建的service account权限不足,需要增加权限,您必须连接您的API与您的应用程序.您必须访问Google Play发布页面(https://play.google.com/apps/publish),并在“设置” – >“用户帐户”中邀请具有服务帐户电子邮件的用户.权限 – >邀请新用户并授予其“查看财务报告”的权限.
注意(在进行测试验证时常遇到的错误 ):
400: The purchase token does not match the product ID (product_id参数不对,需核对)
401:
报的错是说没有访问相应API的权限(没有权限,上面demo的场景)
403:
一:项目没有关联,或者关联到错误的API项目上()
二:Google服务的BUG,可在该应用的商店内,随意增加一个内购或者订阅,看下问题是否消失(新增的内购或者订阅可以删除的。)
404:
一:No application was found for the given package name (包名参数不对,需核对)。
二:The purchase token was not found(token 参数不对,需核对)