【发布时间】: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 不能按预期工作?
【问题讨论】: