2013-10-17 89 views
1

我试图用Google+ API创建圈子,但我有点卡住了,这是我的代码,它或多或少都是从官方的API文档中复制的(是的,我知道它没有,牛逼创建圈子,但问题是相同的)Google+ Domains API 403 Forbidden

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下。我错过了什么?

回答

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调用。

+0

我试了一下,我仍然得到相同的错误信息,如果我正确理解'urn:ietf:wg:oauth:2.0:oob'是需要的,如果我想创建一个“安装的应用程序”,但我想通过网站做事情。 (“输入代码”部分仅用于测试目的,实际上程序只是从GET参数中读取代码) – kviktor

+0

当我运行代码时,那是我所做的更改以及代码的工作。另一件需要检查的地方是你正在填充你的'CLIENT_ID'和'CLIENT_SECRET',因为那里可能有错误。 – Joanna

+0

我可以问你在你的API控制台中设置了什么?这对我来说是这样的:http://i.imgur.com/f9XCxtz.png,但它仍然会给出同样的错误(服务被添加)。 也是域可用的任何人吗?我怀疑问题可能在那里。 (我只想认证用户并将其添加到圈子,我没有Google Apps) – kviktor