【问题标题】:Authenticating Multiple Scopes in Google API Oauth2.0在 Google API Oauth2.0 中对多个范围进行身份验证
【发布时间】: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


【解决方案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 并查看您的应用程序已被授予的范围。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 2018-05-28
    • 2013-12-08
    • 2014-11-23
    • 2013-07-23
    • 1970-01-01
    • 2013-06-24
    相关资源
    最近更新 更多