gpt4 book ai didi

google-app-engine - 用Python实现谷歌云机器学习引擎的在线预测

转载 作者:太空宇宙 更新时间:2023-11-03 15:28:28 26 4
gpt4 key购买 nike

我想从 App Engine 向 ML Engine 模型发送请求。

我在本地机器上构建了用于测试的示例代码。但是,我收到错误消息“UNAUTHENTICATED”:

WARNING:root:No ssl package found. urlfetch will not be able to validate SSL certificates.
91bnQuY34Oh8cJyF=....
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}

我的示例代码:

from google.appengine.api import urlfetch
import urllib
import time
import jwt

data_to_post = {"instances":[{"text":[185,15,14,124,370,832,112,120,...]}]}
encoded_data = urllib.urlencode(data_to_post)

model_url = 'https://ml.googleapis.com/v1/projects/myproject/models/analyizesentiment:predict'

from google.appengine.api import apiproxy_stub_map
from google.appengine.api import urlfetch_stub
apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap()
apiproxy_stub_map.apiproxy.RegisterStub('urlfetch', urlfetch_stub.URLFetchServiceStub())

PRIVATE_KEY_ID_FROM_JSON = '??????????????????'
PRIVATE_KEY_FROM_JSON = "-----BEGIN PRIVATE KEY-----\n??????????\n-----END PRIVATE KEY-----\n"
iat = time.time()
exp = iat + 3600
payload = {'iss': 'accountname@myprojectname.iam.gserviceaccount.com',
'sub': 'accountname@myprojectname.iam.gserviceaccount.com',
'aud': 'https://ml.googleapis.com/v1/',
'iat': iat,
'exp': exp}
additional_headers = {'kid': PRIVATE_KEY_ID_FROM_JSON,
'alg': "RS256",
"typ": "JWT",}
signed_jwt = jwt.encode(payload, PRIVATE_KEY_FROM_JSON, headers=additional_headers, algorithm='RS256')
print(signed_jwt)

result = urlfetch.fetch(model_url, encoded_data, method=urlfetch.POST, headers={'Content-type': 'application/json', 'Authorization': 'Bearer ' + signed_jwt})
print(result.content)

最佳答案

缺少的是标题

headername: "Authorization"值: "Bearer $token"

其中 token 是一个 oauth token 。您可以使用

生成 token

gcloud auth 登录

或者您可以下载服务帐户的 token 。有用的阅读:https://developers.google.com/api-client-library/python/guide/aaa_oauth

但是,在应用引擎上,您应该能够从标准凭据 api 获取它

您还可以修改示例以使用 Credentials 对象并以编程方式附加该 header 。

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

PROJECT=''
MODEL=''
VERSION=''
credentials = GoogleCredentials.get_application_default()
api = discovery.build('ml', 'v1', credentials=credentials)

# Add your sample instances here.
request_data = {'instances':[] }

parent = 'projects/%s/models/%s/versions/%s' % (PROJECT, MODEL, VERSION)
response = api.projects().predict(body=request_data, name=parent).execute()
print "response={0}".format(response)

如果你想提供不同的超时,上面的 execute() 方法可以使用 http 对象。并且您应该能够提供额外的 header 。

关于google-app-engine - 用Python实现谷歌云机器学习引擎的在线预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49758982/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com