1

我正在尝试使用 Google+ API 创建 Circles,但我有点卡住了,这是我的代码,它或多或少是从官方 API 文档中复制的(是的,我知道它不会创建 Circle,但问题是相同)

import httplib2

from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow
import json

with open('client_secrets.json', 'r') as f:
    json_data = json.load(f)

data = json_data['web']
CLIENT_ID = data['client_id']
CLIENT_SECRET = data['client_secret']

# List the scopes your app requires:
SCOPES = ['https://www.googleapis.com/auth/plus.me',
          'https://www.googleapis.com/auth/plus.circles.write']

# The following redirect URI causes Google to return a code to the user's
# browser that they then manually provide to your app to complete the
# OAuth flow.
REDIRECT_URI = 'http://localhost/oauth2callback'

# For a breakdown of OAuth for Python, see
# https://developers.google.com/api-client-library/python/guide/aaa_oauth
# CLIENT_ID and CLIENT_SECRET come from your APIs Console project
flow = OAuth2WebServerFlow(client_id=CLIENT_ID,
                           client_secret=CLIENT_SECRET,
                           scope=SCOPES,
                           redirect_uri=REDIRECT_URI)

auth_uri = flow.step1_get_authorize_url()

# This command-line server-side flow example requires the user to open the
# authentication URL in their browser to complete the process. In most
# cases, your app will use a browser-based server-side flow and your
# user will not need to copy and paste the authorization code. In this
# type of app, you would be able to skip the next 3 lines.
# You can also look at the client-side and one-time-code flows for other
# options at https://developers.google.com/+/web/signin/
print 'Please paste this URL in your browser to authenticate this program.'
print auth_uri
code = raw_input('Enter the code it gives you here: ')

# Set authorized credentials
credentials = flow.step2_exchange(code)

# Create a new authorized API client.
http = httplib2.Http()
http = credentials.authorize(http)
service = build('plusDomains', 'v1', http=http)

from apiclient import errors
try:
    people_service = service.people()
    people_document = people_service.get(userId='me').execute()
except errors.HttpError, e:
    print e.content

我的输出:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "forbidden",
    "message": "Forbidden"
   }
  ],
  "code": 403,
  "message": "Forbidden"
 }
}

我搜索了答案,但没有真正找到任何答案。在 API 控制台上,我添加了 Google+ API 和 Google+ Domains API 服务,我的秘密和客户端 ID 也可以(否则整个脚本会更快失败)。身份验证也成功,我的应用名称显示在https://accounts.google.com/IssuedAuthSubTokens下。我错过了什么?

4

1 回答 1

1

问题出在你的REDIRECT_URI变量上。当您在纯服务器端流程中使用 OAuth 2.0 时,重定向 URI 必须是'urn:ietf:wg:oauth:2.0:oob'.

尝试像这样更改变量(并确保在 API 控制台中更新您的客户端 ID): REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

编辑:另外,请确保您正在为域内的用户进行 API 调用。Google+ Domains API 仅允许仅限于该域内的用户和内容的 API 调用。

于 2013-10-17T21:56:41.360 回答