Google Play and Python - Checking the purchase and consumption status of an inapp item

Some time ago I needed to check if a given package or subscription was purchased in a Google Play store. It seemed to be a simple task but i spent a lot of time on this. For the first time the way which is described in documentation does not work. Why? I don't know. Every time when i asked API for subscription, API returned error message like "This developer account is not the owner of this application." I was not a person who set up this application in store and i couldn't check if this was correct but i hoped it was. The strange thing was that we could check request to the Android API on this account and this account can manage this application. After some time of testing i tried to use Google APIs Client Library for Python. That was another waste of time.

I knew that it would not work but why not try it if there were no other ideas. The same error message was returned. I got back to the way from documentation. I wrote the same code again because i thought that maybe i had made some mistake. After that.. still the same error. I could go on talking about this but it does not make sense. Someone created a new google account with the same application and.. the first code based on documentation worked correctly!

Now we need to generate code which is required for generating access_token.
This operation will be executed only once.

from google_api.oauth2client.client import OAuth2WebServerFlow
flow = OAuth2WebServerFlow(
client_id=client_id,
client_secret=client_secret,
scope="https://www.googleapis.com/auth/androidpublisher",
redirect_uri=redirect_uri
)
auth_url = flow.step1_get_authorize_url()print auth_url
"""
Read URL from auth_url and paste it to the browser. (You must be logged in
to your account) You will be asked about allowing to use this apllication
or somethig like that. Allow it. After that it will redirect you to URL
from redirect_uri but with additional data. Copy code from this URL.
"""
code ="CODE_EXTRACTED_FROM_auth_url"
flow_credentials = flow.step2_exchange(code)print flow_credentials.refresh_token

Good. Now we have a refresh_token. So at the moment we will need only the following data:

Before we will ask Android API we need to check if our
access token is not expired. Token lives for 60 minutes and after that we need to generate a new one. So this is a example method for checking it. It returns True if token is not expired and it if will be live more than 100 seconds.

One more thing. I don't know why but default Android Publisher documentation is in version 1. I don't remember how i found this but if you want version 1.1 go to https://developers.google.com/android-publisher/v1_1/. The only difference between versions is that the version 1 has only one resource type - purchases. Version 1.1 has additional resource type - inapppurchases.

And that is all. I think that i have described the most important things. If you
have some questions, please ask. I hope that i will be able to answer you ;-)