0

I'm trying to get a user's subscription using a google api call from server side (Google App Engine). I went over all the documentation I could find, but I couldn't clearly understand how exactly this should be done properly. So far I've tried the following :

    credentials = AppAssertionCredentials(scope='https://www.googleapis.com/auth/androidpublisher')
    http = credentials.authorize(httplib2.Http(memcache))
    service = build('androidpublisher', 'v2', developerKey='[DEVELOPER_KEY]', http=http)

    request = service.purchases().subscriptions().get(packageName='com.example', subscriptionId=self.orderId, token=self.token)
    response = request.execute(http)
    print json.dumps(response, sort_keys=True, indent=4)

But I'm getting the following response :

HttpError: https://www.googleapis.com/androidpublisher/v2/applications/com.ichoose.bettercalls/purchases/subscriptions/incoming_and_outgoing_single_line_monthly/tokens/dfsdfdsfds?alt=json&key=AIzaSyDdxzjIEZme76VEVMKGaE7R4qTdsQrNloA returned "Invalid Credentials">

The developer key is taken from google developers console --> APIs & auth --> Credentials --> Key for server applications --> API KEY. Is this right?

I would appreciate if someone could explain clearly (preferably with code samples) how this should be done. Thanks a lot.

stkent
  • 19,772
  • 14
  • 85
  • 111
AsafK
  • 2,425
  • 3
  • 32
  • 38

1 Answers1

2

Firstly, you have to make sure you have linked your Google Play Developers Console to an API project and then, you can set up the API Access Client. Please read this [1].

You would need to create/use a Service Account (generated on the Google Developers Console), more info here [2].

The typical code for OAuth Authentications would be the following:

SERVICE_ACCOUNT_EMAIL = (
    'ENTER_YOUR_SERVICE_ACCOUNT_EMAIL_HERE@developer.gserviceaccount.com')

def main():
  # Load the key in PKCS 12 format that you downloaded from the Google APIs
  # Console when you created your Service account.
  f = file('key.p12', 'rb')
  key = f.read()
  f.close()

  # Create an httplib2.Http object to handle our HTTP requests and authorize it
  # with the Credentials. Note that the first parameter, service_account_name,
  # is the Email address created for the Service account. It must be the email
  # address associated with the key that was created.
  credentials = client.SignedJwtAssertionCredentials(
      SERVICE_ACCOUNT_EMAIL,
      key,
      scope='https://www.googleapis.com/auth/androidpublisher')
  http = httplib2.Http()
  http = credentials.authorize(http)

  service = build('androidpublisher', 'v2', http=http)

where SERVICE_ACCOUNT_EMAIL would be the email address associated to the Service Account and the key.p12 would be a file with the key generated for that Service Account.

You can obtain both at Google Developers Console --> APIs & auth --> Credentials ---> Service Account.

[1] https://developers.google.com/android-publisher/getting_started

[2] https://developers.google.com/api-client-library/python/auth/service-accounts

Layo
  • 677
  • 6
  • 16
  • this link was useful for me when I googled for implementing of getting user subscriptions https://stackoverflow.com/questions/35634085/attributeerror-module-object-has-no-attribute-signedjwtassertioncredentials – Kirill Oficerov Dec 27 '17 at 09:46