Android的订阅和谷歌APIAndroid、API

2023-09-04 11:30:19 作者:傲娇小橘子

我试图用全新的Andr​​oid订阅系统从谷歌播放到我的应用程序(我已经在应用内结算工作的罚款)。我已经成功地完成了订阅计费,但我现在想使用谷歌的API作为Android的文件中指出,以获取有关该订阅信息(http://developer.android.com/guide/market/billing/billing_subscriptions.html)

我希望我的服务能够做到的API调用来检索这些信息,但我有问题,验证(带oauth2)。到目前为止,这是我做的(在我的PHP服务):

  require_once'谷歌API的PHP客户端/ src目录/ apiClient.php

常量SERVICE_ACCOUNT_NAME ='从服务帐户访问电子邮件;
$键='我的私人密钥的内容检索到的服务帐户访问;

$客户端=新apiClient();
$ CRED =新apiAssertionCredentials(SERVICE_ACCOUNT_NAME,阵列('https://www.googleapis.com/auth/androidpublisher'),$键);
$断言= $信贷项目> generateAssertion(); //这个我生成加密的智威汤逊
 

我然后尝试检索与此智威汤逊对象的访问令牌。问题是,当我使用访问令牌给我得到了错误的开发者账户没有自己的应用程序,这是不正确的。

(我知道这是不是要做到这一点,但我只是想找回使用智威汤逊理解为什么它不工作,如果我做它作为access_token的谷歌的API文档,它是不是工作压力太大的说明)。

我需要做的从服务器这个API调用,所以没有最终用户参与(没有手动同意)。

解决方案

我有同样的问题,并最终发现,截至目前服务帐户不能访问播放API 。

我不知道当谷歌正计划修复这一点,但你可以通过创建一个Web应用程序客户端ID,并设置一个基本的登录页面先使用新的Web应用程序客户端数据生成一个code解决这个问题并打算$客户 - > createAuthUrl():

  $客户端=新apiClient();

$键=的file_get_contents(KEY_FILE);
$客户 - > SETCLIENTID(CLIENT_ID);
$客户 - > setClientSecret(CLIENT_SECRET);
$客户 - > setRedirectUri(MY_WEBAPP_URL);
$客户 - > setDeveloperKey($键);
$客户 - > setScopes(阵列('https://www.googleapis.com/auth/androidpublisher'));

$ authUrl = $客户 - > createAuthUrl();
打印<一类='登录'的href ='$ authUrl'>连接我和LT;!/ A>中;
 

这应该带你到谷歌的登录页面,您应该与开发者账户登录。当您授权的应用程序,它会带你回到你的Web应用程序的URL作为当您设置客户端ID与code作为获取参数定义。你可以用它来生成令牌(更重要的是,刷新令牌),像这样:

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

$领域=阵列(
    grant_type'=>'authorization_ code',
    code'=> $code,
    CLIENT_ID'=> CLIENT_ID,
    client_secret'=> CLIENT_SECRET,
    redirect_uri'=> MY_WEBAPP_URL
);

//卷曲调用OAuth的URL以$域发送的POST
 
谷歌将在未来Android版本中彻底杀掉屏幕叠加显示API

这应该返回JSON数据刷新令牌。保存此令牌,并用它来拨打另一个电话时,你需要生成一个访问令牌。您将主要运行在同一code你没有得到刷新令牌,但与不同的领域:

  $领域=阵列(
    grant_type'=>refresh_token,
    refresh_token'=> $ refresh_token,
    CLIENT_ID'=> CLIENT_ID,
    client_secret'=> CLIENT_SECRET,
);
 

这会给你,你可以用它来从以下网址获得购买数据的访问令牌: https://www.googleapis.com/androidpublisher/v1/applications/[PACKAGE]/subscriptions/[SKU]/purchases/[PURCHASE_TOKEN]?access_token=[ACCESS_TOKEN]

窍门就是刷新令牌,一旦你有剩下的应该是pretty的简单。

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).

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

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.

(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).

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

解决方案

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

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>";

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

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,
);

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.