付费API权限被拒绝

时间:2019-04-25 06:45:04

标签: php google-api google-api-php-client service-accounts google-play-developer-api

我想验证我的应用内购买。

我正在使用this receipt validator,通过查看源代码正在调用https://www.googleapis.com/androidpublisher/v3/applications/<appId>/purchases/products/<productId>/tokens/<token>,这是我的代码,基本上是从自述文件中复制的

$googleClient = new Google_Client();
$googleClient->setScopes([Google_Service_AndroidPublisher::ANDROIDPUBLISHER]);
$googleClient->setApplicationName('my project name');
$googleClient->setAuthConfig('files/googleauthconfig.json');

$validator = new Validator(new Google_Service_AndroidPublisher($googleClient));

try {
  $response = $validator->setPackageName('my.app.id')
          ->setProductId('my.product.id')
          ->setPurchaseToken('token i got from my app when purchasing')
          ->validatePurchase();
} catch (Exception $e) {
  var_dump($e->getMessage());
  exit;
}

var_dump($response);
exit;

我已经硬编码了很多东西,因为我只是想在这一点上使它工作。 googleauthconfig.json是JSON文件开发者控制台为我提供的我的服务帐户,其中包含该帐户的一堆ID以及一个私钥。我进入了游戏机,并让我的服务帐户成为管理员,然后按照其他问题的建议等待了大约24小时才能解决相同的问题。我已经配置了我的产品,这些产品是可消耗的并且是活动的,并且已经通过我的签名发行应用程序使用测试信用卡成功购买了这些产品。

我一直遇到的错误是

  

当前用户的权限不足,无法执行请求的操作。

2 个答案:

答案 0 :(得分:2)

  

当前用户的权限不足,无法执行请求的操作。

完全意味着您要与之进行身份验证的用户无权执行您尝试执行的操作。您正在使用服务帐户。服务帐户不是您。服务帐户已预先授权。

转到Google,可能需要在“管理员”部分中获得“服务”帐户的电子邮件地址,并授予其访问数据的权限。您可能会像其他任何用户一样执行此操作。

如评论中所述,通常需要不到一个小时的时间才能启用服务帐户。在授予其访问权限后。

可能的代码错误。

我认为您应该尝试使用纯代码,而不要使用验证程序来确保其不是问题所在。

$client = new Google_Client();
$client->setScopes([Google_Service_AndroidPublisher::ANDROIDPUBLISHER]);
$client->setApplicationName('my project name');
$client->setAuthConfig('files/googleauthconfig.json');    
$service = new Google_Service_AndroidPublisher($client);   

try {

  $response = $service->purchases_products->get(package_name, product_id, purchase_token);

} catch (Exception $e) {
  var_dump($e->getMessage());
  exit;
}

var_dump($response);
exit;

答案 1 :(得分:0)

奇怪的是,正如其他答案中所建议的那样,我只需要等待大约30个小时,以使权限更改生效。