Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PredictionAPI+;GAE:无效的凭据_Python_Google App Engine_Google Oauth_Google Prediction - Fatal编程技术网

Python PredictionAPI+;GAE:无效的凭据

Python PredictionAPI+;GAE:无效的凭据,python,google-app-engine,google-oauth,google-prediction,Python,Google App Engine,Google Oauth,Google Prediction,我试图让我的GAE(python)应用程序与预测API对话,但我一直得到HttpError: 我拥有的GAE代码是: from handler_request import Request_Handler # My extended Request Handler from apiclient.discovery import build from oauth2client.appengine import AppAssertionCredentials import httplib2 api

我试图让我的GAE(python)应用程序与预测API对话,但我一直得到
HttpError:

我拥有的GAE代码是:

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))
from handler_request import request_handler#My extended request handler
从apiclient.discovery导入生成
从oauth2client.appengine导入AppAssertionCredentials
导入httplib2
api_key=“”
http=AppAssertionCredentials('https://www.googleapis.com/auth/prediction).authorize(httplib2.Http())
service=build('prediction','v1.6',http=http,developerKey=api\u key)
类ListModels(请求\处理程序):
def get(自我):
papi=服务.培训模型()
结果=papi.list(
project='my_项目编号',
maxResults=10
).execute()
self.response.headers['Content-Type']='text/plain'
self.response.out.write('Result:'+repr(Result))
  • 所有导入的库都是Google的python API库,并且正在正确导入
  • 我使用应用程序的“服务器应用程序密钥”api密钥作为api_密钥,据我所知,这对于我的用例是正确的,因为我的应用程序直接尝试与预测api对话
出于某种原因,这不起作用。我直接从本页获取了此代码,并根据我的需要对其进行了修改:

事实上,如果我复制并粘贴相同的代码,我仍然会得到一个
无效凭据
错误

有什么建议吗?

需要检查的几件事:

  • 对于预测API的1.6版,您不再需要指定developerKey(因此我建议不要这样做),AppAssertionRedentials就足够了

  • AppAssertionCredentials在本地dev_appserver.py环境中不起作用,请确保部署到.appspot.com以在本地测试时使用-或考虑使用SignedJwtAssertionCredentials作为替代

  • 确保与应用程序引擎应用程序关联的服务帐户已添加到已启用预测API的项目的API控制台中的“团队”


  • 您是否在API控制台中为应用程序启用了预测API?是的,在API控制台和云控制台中都为应用程序启用了预测API。谢谢!我不知道它在dev_appserver上不起作用。我读过的所有文档都没有提到它。我可能错过了,但如果没有,我建议在您的文档中提及:)此外,SignedJwtAssertionCredentials导入不起作用。我在谷歌的一些讨论小组中读到,这是一个众所周知的问题。这基本上意味着(据我所知)我不能在本地托管应用程序。这是一个令人不快的问题,因为我们并不总是想仅仅因为不需要部署应用程序(内部用例)就部署应用程序,这使得维护变得非常困难。也许是未来的进步?感谢SignedJwtAssertionCredentials可以在dev|u appserver.py下工作,但它目前需要一些额外的步骤:(1)将p12文件转换为.pem文件:openssl pkcs12-in xxxxx-privatekey.p12-nodes-nocerts | openssl rsa>out.pem(2)确保在app.yaml中启用lib pycrypto:(3)确保安装了pycrypto。(4) 确保您使用的是Google Python API客户端库的1.1版。@aeijdenberg您能详细说明一下吗?我安装了pycrypto 2.6,将库放在app.yaml中,但是我应该如何更改代码?我在导入SignedJwtAssertionCredentials时一直遇到相同的错误。我可以通过在app.yaml中显式请求“version:2.6”来实现这一点。当我请求“版本:最新”时,它不起作用。你能看看这是否有用吗?