2

我需要在创建允许身份验证的服务对象的 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.">
4

1 回答 1

1

您需要为范围传递的唯一内容是 www.googleapis.com 的内容,例如:https://www.googleapis.com/auth/prediction,不要传入“testing_data/training_data.csv testing_data/training_data. .csv”。

您可以随时访问https://accounts.google.com/b/0/IssuedAuthSubTokens并查看您的应用程序已被授予的范围。

于 2013-09-20T15:22:52.057 回答