我正在尝试使用Google+ API创建圈子,但我有点卡住,这是我的代码,它或多或少地从官方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+域名API服务我的秘密和客户端ID也可以(否则整个脚本会更快失败)。此外,身份验证成功,我的应用程序名称显示在https://accounts.google.com/IssuedAuthSubTokens下。我错过了什么?
答案 0 :(得分: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+域名API仅允许限制在该域内的用户和内容的API调用。