Error accessing Google Play subscription info

615 views
Skip to first unread message

UKSDroid

unread,
Oct 22, 2012, 9:35:38 AM10/22/12
to oauth...@googlegroups.com
I am trying to get the subscription info (start date, expiry date, etc) from Google Play. I want to get these all on my server, means it will be a server-to-server communication.
For this, I've created a Google API project and activated the Google Play Android Developer API there. I've tried it both ways; using the service account and using a web application account.
But what I am getting is only exceptions;
I tried the following code for web application approach;

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/token");

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("grant_type", "refresh_token"));
nameValuePairs.add(new BasicNameValuePair("code", CODE));
nameValuePairs.add(new BasicNameValuePair("client_id", GOOGLE_CLIENT_ID));
nameValuePairs.add(new BasicNameValuePair("client_secret", GOOGLE_CLIENT_SECRET));
nameValuePairs.add(new BasicNameValuePair("redirect_uri", GOOGLE_REDIRECT_URI));

post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
 
org.apache.http.HttpResponse response = client.execute(post);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer buffer = new StringBuffer();
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
buffer.append(line);
}


JSONObject json = new JSONObject(buffer.toString());
String refreshToken = json.getString("refresh_token");

This way, I got the following exception;

org.json.JSONException: JSONObject["refresh_token"] not found.
at org.json.JSONObject.get(JSONObject.java:454)
at org.json.JSONObject.getString(JSONObject.java:635)
at com.uks.android.jbhoomi.TestSubscription.main(TestSubscription.java:57)


And when I tried with the service account;

GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_ID)
.setServiceAccountScopes(ANDROIDPUBLISHER_SCOPE)
.setServiceAccountPrivateKeyFromP12File(new File(key.p12)).build();  

Androidpublisher androidpublisher = Androidpublisher
.builder(new NetHttpTransport(), new JacksonFactory())
.setApplicationName(APP_NAME)
.setHttpRequestInitializer(credential).build();  

Purchases purchases = androidpublisher.purchases();
Get get = purchases.get(PACKAGE_NAME, SUBSCRIPTION_ID, PURCHASE_TOKEN);

SubscriptionPurchase subscripcion = get.execute();


In this case I am getting the following exception;

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error" : "invalid_grant"
}
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:103)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:303)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:323)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:340)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:505)
at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:266)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:857)
at com.google.api.client.googleapis.json.GoogleJsonResponseException.execute(GoogleJsonResponseException.java:182)
at com.google.api.client.googleapis.services.GoogleClient.executeUnparsed(GoogleClient.java:279)
at com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:207)
at com.google.api.services.androidpublisher.Androidpublisher$Purchases$Get.execute(Androidpublisher.java:416)
at com.uks.android.jbhoomi.OAuth.main(OAuth.java:67)
400 Bad Request
{
  "error" : "invalid_grant"
}

I can't figure out where I am going wrong. Please help me to fix this. I want to get the subscription expiry date from Google Play server.

Vladimir Kolovski

unread,
Oct 22, 2012, 12:14:34 PM10/22/12
to oauth...@googlegroups.com
Hi - I have some comments inline.

On Mon, Oct 22, 2012 at 6:35 AM, UKSDroid <uksd...@gmail.com> wrote:
I am trying to get the subscription info (start date, expiry date, etc) from Google Play. I want to get these all on my server, means it will be a server-to-server communication.
For this, I've created a Google API project and activated the Google Play Android Developer API there. I've tried it both ways; using the service account and using a web application account.
But what I am getting is only exceptions;
I tried the following code for web application approach;

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/token");

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("grant_type", "refresh_token"));

Shouldn't the grant_type be "authorization_code"? You can find more information at:
 
Cheers,
Vladimir

--
 
 

UKSDroid

unread,
Oct 22, 2012, 11:16:46 PM10/22/12
to oauth...@googlegroups.com
Vladimir,

Thanks for the answering. I was actually using grant_type=authorization_code but this didn't made any difference. Then I tried with grant_type = refresh_token.

Vladimir Kolovski

unread,
Oct 23, 2012, 6:47:43 PM10/23/12
to oauth...@googlegroups.com
Hi, is it possible for you to share the request response in your two attempts below (scrub the sensitive fields). That might help us pinpoint the cause.

Thanks,
Vladimir


--
 
 

UKSDroid

unread,
Oct 25, 2012, 3:43:22 AM10/25/12
to oauth...@googlegroups.com

Hello Vladimir,
Thanks for your Reply

which type of request and response  are you want?.  It is a Webservice. not a Servlet.

Thanks & Regards 
UKSDroid

Tim Bray

unread,
Oct 25, 2012, 10:08:53 AM10/25/12
to oauth...@googlegroups.com

The http request and response, including headers.
-T

--
 
 
Message has been deleted

UKSDroid

unread,
Oct 25, 2012, 11:20:32 PM10/25/12
to oauth...@googlegroups.com
Hello  Tim Bray,
This is a HttpResponse .

HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/token");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("grant_type",
"authorization code"));
nameValuePairs.add(new BasicNameValuePair("code", CODE));
nameValuePairs.add(new BasicNameValuePair("client_id",
GOOGLE_CLIENT_ID));
nameValuePairs.add(new BasicNameValuePair("client_secret",
GOOGLE_CLIENT_SECRET));
nameValuePairs.add(new BasicNameValuePair("redirect_uri",
GOOGLE_REDIRECT_URI));

post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);


Thanks
UKSDroid

Tim Bray

unread,
Oct 26, 2012, 7:47:05 PM10/26/12
to oauth...@googlegroups.com
If possible, we’d like to look at the actual HTTP request/response
headers. Most web-development packages provide access to these. Of
course, you should hide your client id/secret. -T
> --
>
>

UKSDroid

unread,
Oct 29, 2012, 12:24:17 AM10/29/12
to oauth...@googlegroups.com
Hello Tim,

I am listing the complete code below (using the Web Application approach only);

public class TestSubscription {

private final static String GOOGLE_CLIENT_ID = "myclientid";
private final static String GOOGLE_CLIENT_SECRET = "client_secret";
private final static String GOOGLE_REDIRECT_URI = "https://someurl/";
private final static String CODE = "code";

public static void main(String[] args) {

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/token");

try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("grant_type",
"authorization_code"));
nameValuePairs.add(new BasicNameValuePair("client_id",
GOOGLE_CLIENT_ID));
nameValuePairs.add(new BasicNameValuePair("client_secret",
GOOGLE_CLIENT_SECRET));
nameValuePairs.add(new BasicNameValuePair("code", CODE));
nameValuePairs.add(new BasicNameValuePair("redirect_uri",
GOOGLE_REDIRECT_URI));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

org.apache.http.HttpResponse response = client.execute(post);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
StringBuffer buffer = new StringBuffer();
for (String line = reader.readLine(); line != null; line = reader
.readLine()) {
buffer.append(line);
}
JSONObject json = new JSONObject(buffer.toString());
String refreshToken = json.getString("refresh_token");

} catch (Exception e) {
e.printStackTrace();

}
}
}

When I print the headers of HttpResponse object response, I get the following;

Cache-Control: no-cache, no-store, max-age=0, must-revalidate

Pragma: no-cache

Expires: Fri, 01 Jan 1990 00:00:00 GMT

Date: Mon, 29 Oct 2012 03:58:54 GMT

Content-Type: application/json

X-Content-Type-Options: nosniff

X-Frame-Options: SAMEORIGIN

X-XSS-Protection: 1; mode=block

Server: GSE

Transfer-Encoding: chunked


I can't figure out where I am going wrong.

- UKSDroid
Reply all
Reply to author
Forward
0 new messages