0

I'm developing an app which has several non-consumable inApp products in it and i need to get the purchase status of those by directly sending requests to Google server and receiving the result. As i understood "Purchase Status API" gives that possibility but it's not working for me. I made all the needed settings in "Google API Console": Switched on "Google Play Android Developer API". Created a client ID for my App. I tried to use the methods described over here: http://developer.android.com/google/play-services/auth.html But the methods GoogleAuthUtil.getToken(); and GoogleAuthUtil.getTokenWithNotification(); are throwing me an "Unknown exception". However i managed to get the access token by using this code:

AccountManager acountM=AccountManager.get(Utils.curentActivity);
        Account[] acount=acountM.getAccountsByType("com.google");
        String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/androidpublisher";
        AccountManagerFuture<Bundle> future=acountM.getAuthToken(acount[0], AUTH_TOKEN_TYPE, null, Utils.curentActivity, null, null);
        try{
            String token=future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
        }catch(Exception e){e.printStackTrace();}

But after this, i have no idea what to do with this token. If i try to Authenticate like this:

URL url=new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token="+token);
            URLConnection conection=url.openConnection();
            conection.setDoInput(true);
            conection.connect();
            InputStream is=conection.getInputStream();

It throws me a "File not found exception". If i try to get the purchase status of a product:

String packageName=MainActivity.this.getPackageName();
            String productId="SKU_KEY_1";
            HttpClient client=new DefaultHttpClient();
            HttpGet get=new HttpGet("https://www.googleapis.com/androidpublisher/v1.1/applications/"+packageName+"/inapp/"+productId+"/purchases/"+token);
            HttpResponse response=client.execute(get);
            InputStream is=response.getEntity().getContent();
            String result=Utils.readInputStream(is);
            is.close();

The result is a JSON which looks like this:

{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}

I'm desperately looking for a solution for several days. I'll will be glad if somebody will provide me a solution or an up to date documentation on this point.

nolanic
  • 321
  • 4
  • 16

1 Answers1

0

So looking over your code and information I would try a couple things, first verifying your access token. I have used this as a reference. Using a browser and a simple html page (see below) I am able to acquire the token and verify it. You will need to need to fill out the values as specified on that page.

Make sure that your token is correct first. I did this by using that userinfo address in a browser.

Next you inapp purchase html location needs ?access_token={access_token} at the end after the If you notice the section "Accessing the API" in the above page about authorization, you need to add this.

Here is the webpage that I have used to help get the token for testing. NOTE that this is for testing and will not work as final solution because the authorization token is only good for a short time. You will need to make sure that the code that gets that access token functions correctly. In addition again refer to the page above to fill this out for your product. The only thing that is dynamic in this is the "code" value which comes from the instructions on that page.

Hope some of this can help you...

<form action=" https://accounts.google.com/o/oauth2/token" method="post"> <input name="grant_type" type="hidden" value="authorization_code" /> <input name="code" type="hidden" value="the code from the previous step"/> <input name="client_id" type="hidden" value="the client ID token created in the APIs Console"/> <input name="client_secret" type="hidden" value="the client secret corresponding to the client ID"/> <input name="redirect_uri" type="hidden" value="the URI registered with the client ID"/> <input type="submit" />
</form>

vbbartlett
  • 179
  • 2
  • 14
  • oh make sure you have the Google Play Android Developer API turned on in the https://code.google.com/apis/console – vbbartlett Oct 09 '13 at 03:25