1

我需要在我的服务器上验证用户之前在我的 Android APP 中进行的每个 Android 购买。我虽然与google-api-php-client一起使用它会很容易验证和管理服务器中的令牌。但是没有任何示例,昨天谷歌发布了新版本 0.6.3,提供应用内购买服务。

我跟着-> *code.google.com/p/google-api-php-client/wiki/OAuth2#Service_Accounts*

在我的code.google.com/apis/console/上,我推送了“Google Play Android Developer API”,并在 API Access 中配置了“服务帐户”。

从 Android 客户端 APP,服务器接收 PACKAGE_NAME、PRODUCT_ID 和购买 TOKEN。

我的服务器代码如下:

require_once '../../src/Google_Client.php';
require_once '../../src/contrib/Google_AndroidpublisherService.php';

// Set your client id, service account name, and the path to your private key.
// For more information about obtaining these keys, visit:
// https://developers.google.com/console/help/#service_accounts
const CLIENT_ID = 'asdf.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'asdf@developer.gserviceaccount.com';

// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = '../../asdf/privatekey.p12';;

$client = new Google_Client();
$client->setApplicationName({APP_PACKAGE_NAME});

// Set your cached access token. Remember to replace $_SESSION with a
// real database or memcached.
session_start();
if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}

// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/androidpublisher'),
    $key)
);

$client->setClientId(CLIENT_ID);
$service = new Google_AndroidPublisherService($client);
$res = $service->inapppurchases->get({APP_PACKAGE_NAME},{APP_PACKAGE_NAME.PRODUCT_ID}, {PURCHASE_TOKEN});
var_dump($res);

显示的错误是:

Google_ServiceException: Error calling GET https://www.googleapis.com/androidpublisher
/v1.1/applications/{APP_PACKAGE_NAME}/inapp/{APP_PACKAGE_NAME.PRODUCT_ID}/purchases
/{PURCHASE_TOKEN}: (401) This developer account does not own the application. in 
/.../google-api-php-client/src/io/Google_REST.php on line 66 Call Stack: 0.0201 
266376 1. {main}() ............

令牌是正确的,我在 Google API 控制台(https://code.google.com/apis/console)和 Google 开发者控制台(https://play.google.com/apps/publish )中使用同一个帐户/)。我只使用服务帐户 api,不使用 Web 应用程序的客户端 ID 和简单 API 访问。为了安全起见,我在这里更改了一些代码值。

有人可以帮我知道我使用 Google API 验证购买服务器时出了什么问题吗?我怎么知道我的应用程序的所有者?跟谷歌Apis新项目、项目域、项目号、项目ID等有关系吗?

4

1 回答 1

2

我认为我的问题是因为我试图将服务帐户与 Google Apps Gmail 自己的帐户(非 @gmail.com 帐户)一起使用。我必须将域范围的权限委派给我的服务帐户。我必须按如下方式实例化 Android Publisher Service:(仅在 Google Api Drive 文档中创建)。我在 Google_AssertionCredentials 中以编程方式添加了“sub”参数,如下所示:

$auth = new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
'https://www.googleapis.com/auth/androidpublisher',
$key);
$auth->sub = "myown@email.com";
$client->setAssertionCredentials($auth);

Google Play Android Developer API 中的文档很差,Google 支持也帮不上忙,他们会将您重定向到文档。Google PHP 开发人员甚至不知道服务帐户是如何工作的。尽管我自己找到了答案,但 Google 需要改进所有新的 Google Play In-app Billing API 版本 3。

于 2013-08-02T12:40:19.243 回答