Subscription for Android and Google APIs

I'm trying to use the new Android subscription system from Google Play in my application (I already have work with billing in the application). I have successfully subscribed, but now I want to get information about this subscription using 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 make an API call to retrieve this information, but I have authentication issues (with oauth2). So far this is what I am doing (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 

Then I try to get an access token with this JWT object. The problem is that when I use the access token, I got an error that the developer account does not own the application, which is wrong.

(I know that this is not a way to do this, but I just wanted to get the access_token using the JWT to understand why it does not work, if I do it as indicated in the google apis documentation, it does not work either).

I need to make this API call from the server, so no end user should participate (without the consent of the management).

+7
source share
2 answers

I had the same problem, and in the end I found that at the moment the service accounts cannot access the Play API.

I'm not sure when Google plans to fix this, but you can get around this by creating a web application client ID and setting up a basic login page to first create the code using the new client data of the web application and go 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 will lead you to the Google login page, where you must log in with the developer account. When you authorize the application, it will return you to the URL of your web application, as defined when setting the client ID using CODE as the get parameter. You can use to create a token (and, more importantly, an update token):

 $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 an update token. Save this token and use it to make another call when you need to create an access token. You essentially run the same code as you to get the update 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 that you can use to obtain purchase data at the following URL: https://www.googleapis.com/androidpublisher/v1/applications/[PACKAGE]/subscriptions/[SKU]/purchases/[PURCHASE_TOKEN]?access_token=[ACCESS_TOKEN]

The trick gets the update token as soon as you have that everything else should be pretty simple.

+7
source

I have the same problem. This is because you allow a user in the Google API who does not own this application and try to get data that matches your application.

This section is well described. http://support.google.com/googleplay/android-developer/bin/answer.py?hl=en&answer=2528691&topic=16285&ctx=topic

You must allow OAuth2 to the application owner, and then use the Google API with the received token.

0
source

All Articles