【问题标题】:"403 Forbidden" response from Google Android In App Purchases API with service account带有服务帐户的 Google Android In App Purchases API 的“403 Forbidden”响应
【发布时间】:2023-03-22 05:46:01
【问题描述】:

无法使用原生 Google PHP 客户端使 Android In App Purchases API 与服务帐户凭据一起使用。

我从 Google PHP 客户端包 (google\examples\service-account.php) 中获取了示例脚本, 在其中提供了所有必需的凭据,它非常适合“图书”服务,但不适用于“Android in App Purchases”。

下面的代码可以正常工作:

$client_id = '{SERVICE_CLIENT_ID}';
$service_account_name = '{SERVICE_ACCOUNT_NAME}';
$key_file_location = '{KEY_PATH}';

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");

$service = new Google_Service_Books($client);
$scopes = array('https://www.googleapis.com/auth/books');
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials($service_account_name, $scopes, $key);
$client->setAssertionCredentials($cred);

if($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}

echo '<pre>'; $results = $service->volumes->listVolumes('Henry David Thoreau', array('filter' => 'free-ebooks')); print_r($results); echo '</pre>';

下面的代码以“403 Forbidden”作为来自 Google 的响应:

$client_id = '{SERVICE_CLIENT_ID}';
$service_account_name = '{SERVICE_ACCOUNT_NAME}';
$key_file_location = '{KEY_PATH}';

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");

$service = new Google_Service_AndroidPublisher($client);
$scopes = array('https://www.googleapis.com/auth/androidpublisher');
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials($service_account_name, $scopes, $key);
$client->setAssertionCredentials($cred);

if($client->getAuth()->isAccessTokenExpired()) {
    $client->getAuth()->refreshTokenWithAssertion($cred);
}

echo '<pre>'; $results = $service->inapppurchases->get('{PACKAGE}', '{PRODUCT}', '{TOKEN}'); print_r($results); echo '</pre>';

我还使用网络帐户凭据进行了测试(当访问令牌由 Google 在“重定向”后提供的特殊“代码”生成时)。结果是一样的——403 Forbidden。

谁能提供任何线索,为什么 Android API 不能按预期工作?

【问题讨论】:

    标签: php android


    【解决方案1】:

    好的,我找到了解决方案!

    您必须将您的 API 与您的应用程序连接起来。您必须进入您的 Google Play 发布页面 (https://play.google.com/apps/publish) 并在设置->用户帐户和权限->邀请新用户并授予其“查看财务报告”权限中使用服务帐户电子邮件邀请用户。

    感谢为我工作,祝你好运!

    【讨论】:

      【解决方案2】:

      您需要以“您”的身份登录,但这可能是一次性操作 - 如果您请求离线访问,您将能够获得可用于生成新访问令牌的刷新令牌 - 这样您就可以甚至将其作为带外过程的一部分执行,只需使用您的刷新令牌进行部署。无法使用服务帐户(没有某种委托),因为它不能识别单个用户,这在此处是必需的。

      【讨论】:

        【解决方案3】:

        我遇到了和你类似的问题。我终于设法通过查看此处生成的链接来解决它:https://developers.google.com/oauthplayground/

        显然,Android 发布者范围

        https://www.googleapis.com/auth/androidpublisher
        

        需要添加到链接请求授权码(甚至在生成刷新令牌之前)通过添加:

        &scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher
        

        得到这个:

        https://accounts.google.com/o/oauth2/auth?redirect_uri=<YOUR_REDIRECT_URI>&response_type=code&client_id=<YOUR_CLIENT_ID>&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher&approval_prompt=force&access_type=offline
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-08-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-04-09
          • 1970-01-01
          • 2021-03-18
          相关资源
          最近更新 更多