Pregunta

I'm trying to make my GAE (python) Application talk to the Prediction API, but I keep getting HttpError: <HttpError 401 when requesting ... returned "Invalid Credentials">

The GAE code I have is:

from handler_request import Request_Handler # My extended Request Handler
from apiclient.discovery import build
from oauth2client.appengine import AppAssertionCredentials
import httplib2

api_key = "<my key for server apps>"

http = AppAssertionCredentials('https://www.googleapis.com/auth/prediction').authorize(httplib2.Http())
service = build('prediction', 'v1.6', http=http, developerKey=api_key)

class ListModels(Request_Handler):
    def get(self):
        papi = service.trainedmodels()

        result = papi.list(
            project='my_project_number',
            maxResults=10
        ).execute()

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Result: ' + repr(result))
  • All imported libraries are Google's python API libraries, and are being imported correctly.
  • I'm using, as api_key, my application's "Key for server apps" API Key, as I understand this is the correct one for my use case as my application is directly trying to talk to the Prediction API

For some reason, this doesn't work. I've taken this code directly from this page and adapted it to my needs: https://developers.google.com/prediction/docs/developer-guide#predictionfromappengine

As a matter of fact, if I copy and paste that same code, I still get an Invalid Credentials error.

Any suggestions?

¿Fue útil?

Solución

A few things to check:

  1. For version 1.6 of Prediction API you no longer need to specify the developerKey (so I would suggest not doing so), AppAssertionCredentials is enough.

  2. AppAssertionCredentials will not work in the local dev_appserver.py environment, make sure you deploy to <your app name>.appspot.com to use - or look at using a SignedJwtAssertionCredentials as a substitute when testing locally.

  3. Make sure that the service account associated with the App Engine app is added to the "Team" in the API Console for the project that has Prediction API enabled.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top