2013-10-17 160 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