【发布时间】:2013-09-24 16:42:54
【问题描述】:
我需要在创建允许身份验证的服务对象的 http post 请求中插入多个范围。然而,不幸的是,简单地将多个作用域放入字符串中似乎并不能实现这一点,在底部返回错误。
import gflags
import apiclient
import oauth2client
import argparse
from oauth2client import client
from oauth2client import file
from oauth2client import tools
from apiclient import discovery
from apiclient import sample_tools
import httplib2
import sys
import os
import pprint
from pprint import pprint
name = 'prediction'
scope = "https://www.googleapis.com/auth/prediction https://www.googleapis.com/auth/devstorage.full_control testing_data/training_data.csv testing_data/training_data.csv"
filename = '__file__'
client_secrets = os.path.join(os.path.dirname(filename),
'client_secrets.json')
flow = client.flow_from_clientsecrets(client_secrets,scope=scope)
storage = file.Storage(name+'.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = tools.run(flow, storage)
http = credentials.authorize(httplib2.Http())
service = discovery.build('prediction','v1.6',http=http)
papi = service.trainedmodels()
result = papi.list(maxResults=10,project='895092811023').execute()
body = {'id':'Universities','storageDataLocation':'testing_data/training_data.csv'}
start = papi.insert(body=body,project='895092811023').execute()
这是错误,指出缺少所需的范围。 (它正在记录一些范围,因为它将结果保存到结果中,但只是不让我插入新模型,我认为这是因为它没有获得访问该模型数据的范围,哪个在 Google Cloud Storage 中?
Traceback (most recent call last):
File "filehere", line 42, in <module>
start = papi.insert(body=body,project='895092811023').execute()
File "build\bdist.win-amd64\egg\oauth2client\util.py", line 132, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Python27\lib\site-packages\apiclient\http.py", line 680, in execute
raise HttpError(resp, content, uri=self.uri)
HttpError: <HttpError 401 when requesting https://www.googleapis.com/prediction/v1.6/projects/895092811023/trainedmodels?alt=json returned "Required scope(s) missing.">
【问题讨论】:
-
尝试用 , 分隔范围
-
用逗号分隔范围对我不起作用,但用空格分隔它们(至少通过 Objective-C API 使用 Swift)。因此,例如
let compositeScope: String = kMyGMailScope + " " + kMyGDriveScope,只需一次登录,即可同时为我提供 Drive 和 GMail 身份验证。
标签: google-api oauth-2.0 google-api-python-client google-prediction