在Google API v2中,为什么会出现错误"用于调用Google Play Developer API的项目ID尚未链接"?

Mar*_*ark 7 php google-api google-play google-api-php-client

当我使用Google API v2时,为了获得一个inapp列表,我在进行API调用时遇到以下错误:

{
 "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."
 }
}
Run Code Online (Sandbox Code Playgroud)

然后我研究了这个错误,最后看到了这个页面,这个页面以及这个页面上的建议.我跟着并仔细检查了项目在那里的链接方式,但没有帮助.

这就是我做的......

在Google Developer Console中:

  1. 使用现有项目.
  2. 去了API&auth> API.
  3. 确保"Google Play Android Developer API"已启用.
  4. 使用我的CLIENT ID,CLIENT SECRET和REDIRECT URIS的现有凭据.应用程序类型是"本机应用程序".
  5. 也创建了一个API KEY,但没有使用它.

在Google Play开发者控制台中:

  1. 去设置> API访问.
  2. 在LINKED PROJECT下,我确保链接了Google Developer Console上的上述项目.

在我的PHP脚本中:

<?php

require_once('/var/www/html/common/include.php');
require_once realpath(dirname(__FILE__) . '/lib/Google/autoload.php');

$refreshToken = '1/sometoken';
$packageName = 'com.somepackage';

$client_id = GOOGLE_CLIENT_ID;
$client_secret = GOOGLE_CLIENT_SECRET;
$redirect_uri = 'http://localhost';

$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setScopes(['https://www.googleapis.com/auth/androidpublisher']);
$client->refreshToken($refreshToken);

$service = new Google_Service_AndroidPublisher($client);
$response = $service->inappproducts->listInappproducts($packageName);

var_dump($service);
Run Code Online (Sandbox Code Playgroud)

这导致了上述错误.

使用刷新令牌解决此错误的任何其他建议?

Krt*_*tko 3

嘿,我遇到了与您类似的问题,我发现问题是我正在验证用户没有正确的权限。

首先进入您的 Google Play 控制台,进入“设置”>“API 访问”。确保您有一个链接的“Google Play Android Developer”(这听起来像您和我一样)。

接下来进入“设置”>“用户帐户和权限”,确保用户具有查看订阅或您需要的任何内容的适当权限。出于测试目的,只需授予它所有权利即可。

然后确保通过 Oauth 验证该用户(即从列表中选择他们的电子邮件),然后获取典型的刷新/访问令牌

现在对我来说确实如此明显,但仍然经历了一天的沮丧。我希望这能解决您的问题:)