Android 订阅和 Google API [英] Android subscription and Google API

查看:25
本文介绍了Android 订阅和 Google API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中使用来自 Google Play 的新 Android 订阅系统(我的应用程序内结算工作正常).我已成功完成订阅计费,但我现在想使用 android 文档 (http://developer.android.com/guide/market/billing/billing_subscriptions.html) 中指示的 google api 检索有关此订阅的信息.

I'm trying to use the new Android subscription system from Google Play into my application (I already had in-app billing working fine). I have successfully done the subscription billing, but I now want to retrieve informations about this subscription by using the google apis as indicated in the android documentation (http://developer.android.com/guide/market/billing/billing_subscriptions.html).

我希望我的服务能够执行 API 调用来检索这些信息,但是我遇到了身份验证问题(使用 oauth2).到目前为止,这就是我所做的(在我的 php 服务中):

I want my service to be able to do the API call to retrieve these informations, but I have problems with authentication (with oauth2). So far, this is what I do (in my php service) :

require_once 'google-api-php-client/src/apiClient.php'

const SERVICE_ACCOUNT_NAME = 'email from services account access';
$key = 'content of my private key retrieved from services account access';    

$client = new apiClient();
$cred = new apiAssertionCredentials(SERVICE_ACCOUNT_NAME, array('https://www.googleapis.com/auth/androidpublisher'), $key);
$assertion = $cred->generateAssertion(); // This generate my encrypted JWT

然后我尝试使用此 JWT 对象检索访问令牌.问题是,当我使用访问令牌时,我收到了开发者帐户不拥有该应用程序的错误,这不是真的.

I then try to retrieve the access token with this JWT object. The problem is that when I use the access token given I got the error that the developer account does not own the application, which is not true.

(我知道这不是这样做的方法,但我只是想使用 JWT 检索 access_token 以了解它为什么不起作用,如果我按照 google apis 文档中的说明进行操作,它也不起作用).

(I know this is not the way to do it, but I just wanted to retrieve the access_token using the JWT to understand why it is not working, if I do it as indicated in the google apis documentation it is not working too).

我需要从服务器执行此 API 调用,因此无需最终用户参与(无需手动同意).

I need to do this API call from a server, so no end-user has to be involved (no manual consent).

推荐答案

我遇到了同样的问题,最终发现目前服务帐户无法访问 Play API.

I had the same issue, and ultimately discovered that as of right now service accounts can not access the Play API.

我不确定 Google 何时计划解决此问题,但您可以通过创建 Web 应用客户端 ID 并设置基本登录页面来首先使用新的 Web 应用客户端数据生成代码并转到$client->createAuthUrl():

I'm not sure when Google is planning on fixing this but you can get around this by creating a web app client ID and setting up a basic login page to first generate a code using the new web app Client data and going to $client->createAuthUrl():

$client = new apiClient();

$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(MY_WEBAPP_URL);
$client->setDeveloperKey($key);
$client->setScopes(array('https://www.googleapis.com/auth/androidpublisher'));

$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";

这应该会将您带到 Google 登录页面,您应该在其中使用开发者帐户登录.当您授权应用程序时,它会将您带回到您设置客户端 ID 时定义的 Web 应用程序 URL,其中使用 CODE 作为获取参数.您可以使用生成令牌(更重要的是,刷新令牌),如下所示:

This should take you to a Google login page where you should log in with the developer account. When you authorize the app, it will take you back to your web app URL as defined when you set up the client ID with a CODE as a get parameter. You can use to generate a token (and more importantly, a refresh token) like so:

$url = 'https://accounts.google.com/o/oauth2/token';

$fields = array(
    'grant_type'=>'authorization_code',
    'code'=>$code,
    'client_id'=>CLIENT_ID,
    'client_secret'=>CLIENT_SECRET,
    'redirect_uri'=>MY_WEBAPP_URL
);

// cURL call to OAuth URL with $fields sent as POST

这应该会返回带有刷新令牌的 JSON 数据.保存此令牌并在需要生成访问令牌时使用它进行另一次调用.您将基本上运行与获取刷新令牌相同的代码,但具有不同的字段:

This should return you JSON data with a refresh token. Save this token and use it to make another call whenever you need to generate an access token. You will essentially run the same code you did to get the refresh token, but with different fields:

$fields = array(
    'grant_type'=>'refresh_token',
    'refresh_token'=>$refresh_token,
    'client_id'=>CLIENT_ID,
    'client_secret'=>CLIENT_SECRET,
);

这将为您提供一个访问令牌,您可以使用它从以下 URL 获取购买数据:https://www.googleapis.com/androidpublisher/v1/applications/[PACKAGE]/subscriptions/[SKU]/purchases/[PURCHASE_TOKEN]?access_token=[ACCESS_TOKEN]

This will give you an access token you can use to get purchase data from the following URL: https://www.googleapis.com/androidpublisher/v1/applications/[PACKAGE]/subscriptions/[SKU]/purchases/[PURCHASE_TOKEN]?access_token=[ACCESS_TOKEN]

诀窍是获取刷新令牌,一旦获得,其余的应该非常简单.

The trick is getting the refresh token, once you have that the rest should be pretty straightforward.

这篇关于Android 订阅和 Google API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆