Tagged: android

Server-side verification of Google Play subscriptions

TL;DR To programatically verify Google Play subscriptions, you have to use the OAuth 2.0 for web server applications auth flow, not service accounts. This is a massive FUBAR on Google’s side and makes life of developers very painful.

Lately, I’ve been working on the backend part of a upcoming app we’re developing for one of our clients. This app offers monthly and yearly subscriptions, so I had to implement a check if the recurring payment happened, the credit card got billed and the app provider got the money. Of course, for multiple reasons, this has to be done server-side, completely automatically and without any intervention from the app user or provider.

Google provides an API called android-publisher for this. To use any API from Google, first you have to enable it from the Console and then authenticate with it. The authentication is done via OAuth 2.0. As Google offers API access to many of their services which are used in different occasions, they also offer different OAuth 2.0 authentication flows.

The flow/mechanism for server to server communication is called Service accounts in Google terminology. This is precisely what I needed. However, for reasons beyond my understanding, this is not the one used for android-publisher API. Instead, they chose Web server applications flow, which for this use case is absurd.

(Sidenote: When we started to build the aforementioned app, recurring transaction were not even available for Android. We planned to use Paypal as we did for the Blackberry version. However, during development, Google introduced subscriptions for Android which made us happy.

I started reading the docs and implementing the whole auth and check code, but it didn’t work; I was getting “This developer account does not own the application.” HTTP 401 error. Googling for this didn’t help – at that time, the only search results were two couple of hours old questions on Stack Overflow. I would swear the docs at that time mentioned to use Service accounts for authentication and later Google changed it. I had to re-read the docs from the beginning to debug this infuriating error.)

Using Web server applications flow is ridiculous because human interaction is involved. At least once, you (in this case our client!) need to press an “Allow” button in you web browser. Palm, meet face.

Here are the instructions you need to follow to achieve automated subscription verification. The code is in Python but it’s easy to adapt.

First of all, in the Console, you need to create a Client ID for Web applications. You can use http://localhost as the redirect hostname. As you’ll see in a minute, it doesn’t matter much. You mostly need the Client ID and Client secret.

Next, fire up the Python REPL and enter this:

https://gist.github.com/3450666

Use the Client ID and Client secret you obtained from Console. This piece of code will give you an authentication URL; by default, it will contain access_type=offline parameter. This is very important, make sure it’s there. Open the URL in your browser and log in with the Google account that you will be using for publishing the Android application. After a successfull login and authorization, you’ll be redirected to localhost in your browser. Unless you’re running a webserver locally, this will probably fail, but it doesn’t matter. The address you are redirected to will contain a code parameter. Copy its value and go back to the REPL again:

https://gist.github.com/3450785

Finally you’ve got an instance of the oauth2client.client.OAuth2Credentials class. It contains couple of properties but the only one that’s really interesting is the refresh_token. Store the refresh token to your server configuration, you can use it forever meaning until someone does not revoke the access to the API. Then you would have to got through this whole process again.

Basically, thanks to this refresh token you will able to obtain a new access token on each call to the API. To do that, you create an instance of OAuth2Credentials and use that to authorize an httplib2.Http object:

https://gist.github.com/3451039

You can now build a service and call the get purchases API call.

The following gist summarizes the whole blogpost:

https://gist.github.com/3451509

As long as the API access will not be revoked, you should be fine using this method.

Advertisements

On Android market share and lack of developers

Yesterday evening, I attended For Mobile at HUB Prague. It’s a regular meeting of local mobile developers. One thing that struck me was the dominance of iOS developers amongst attendees. There were almost none Android devs there.

This obsession with iOS is a common theme here in Czech Republic. There are various reasons for it, e.g. loud advocates, local success stories or historical lack of Android against iOS. This lead to wider iOS adoption not only by developers, but also by clients who were, sometimes mindlessly, ordering only iOS apps.

With the widespread adoption of Android, times are changing. At Wildfuse, we have clients from the Western Europe and overseas and we could see the turn already about half a year ago. It is only a matter of time when the regional media houses, advertising agencies and other institutions core to the business of local mobile studios will acknowledge the size of the Android market. I think it will come in about 3 to 6 months. I’m really looking forward to seeing how the locals will adapt to this market turn, more so with the current lack of Android devs.

If you are a developer playing with the idea of “going mobile”, we’re hiring. Non-experienced devs can learn from one of the best Android developers in the country. We’re also looking for iOS programmers. Don’t hesitate to say hello.