Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example of validating a recurring payment from Google Play using the Web server application OAuth 2.0 flow
import datetime
import httplib2
# to see in detail what's going on, uncomment
# httplib2.debuglevel = 4
from apiclient.discovery import build
from oauth2client.client import OAuth2Credentials, OAuth2WebServerFlow
if __name__ == "__main__":
flow = OAuth2WebServerFlow(client_id="",
auth_url = flow.step1_get_authorize_url(redirect_uri="http://localhost")
# the auth_url looks similar to this:
# open it up in the browser, log in with the appropriate Google account and press the Allow button
# after redirect, extract the code parameter from the URL
code = "4/kGRRVluW_MustsRmVcL2azzPHVFu.UuoPHSEws3AXOl01ti4ZT3YBGCqocgI"
flow_credentials = flow.step2_exchange(code)
# the flow_credentials object contains the refresh_token property,
# the one we're interested in; store it for later use
credentials = OAuth2Credentials("FUBAR", # random value for access token, does not matter
"wildfuse/1.0") # be a good citizen and pass a custom User-Agent too
http = httplib2.Http()
http = credentials.authorize(http)
service = build("androidpublisher", "v1", http=http)
# get the packageName, productId and purchaseToken from the Android client
validation_request = service.purchases().get(packageName=packageName,
# example return value of the validation_request.execute() call
# {u'initiationTimestampMsec': u'1340280336000',
# u'kind': u'androidpublisher#subscriptionPurchase',
# u'autoRenewing': False,
# u'validUntilTimestampMsec': u'1342879522220'}
print validation_request.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment