2015-02-07 79 views
0

我想使用python和驗證網站api(v1)在我的網站管理員工具中驗證網站。在這個例子中,我想使用驗證API獲取所有已驗證的站點,因爲該函數沒有參數。 (我知道,它通過網站管理員工具是可能的,也是)權限不足網站驗證Google

#!/usr/bin/python 

import httplib2 

from apiclient import errors 
from apiclient.discovery import build 
from oauth2client.client import OAuth2WebServerFlow 
from oauth2client.file import Storage 

http = httplib2.Http() 

storage = Storage('credentials') 
storage_verify = Storage('credentials_verify') 

credentials = storage.get() 
credentials_verify = storage_verify.get() 

http_wm = credentials.authorize(http) 
http_sv = credentials_verify.authorize(http) 

webmasters_service = build('webmasters', 'v3', http=http_wm) 
verification_service = build('siteVerification', 'v1', http=http_sv) 

site_list = webmasters_service.sites().list().execute() 
print(site_list) 

# this line generates the error  
verified_site_list = verification_service.webResource().list().execute() 
print(verified_site_list) 

有不足的權限錯誤:

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/siteVerification/v1/webResource?alt=json returned "Insufficient Permission"> 

我已閱讀並寫了siteVerification API訪問(我檢查我的令牌在這裏:https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=TOKEN

任何想法我做錯了或如何調試代碼?

回答

0

請務必閱讀並理解https://developers.google.com/site-verification/v1/invoking的全部內容。尤其是你需要欣賞:

Verify a new site

To verify a site,

- First request a verification token by calling getToken. 
- Place the token on your site using whatever method you choose. 
- Ask Google to verify that the site is yours, using the insert operation. 

所有你需要做的是正確地構建兩個請求,getTokeninsert。 「正確」意味着什麼,在上面鏈接的文檔中詳細討論。

如果您真的需要代碼幫助,您需要展示一個最小的工作示例,以便我們可以重現您檢索到的錯誤。

+0

我要驗證網站,但問題是,我的API實現不起作用。因爲'list'沒有參數,所以我先試了一個。希望你瞭解我的問題。謝謝你的幫助。 – Wikunia 2015-02-07 16:22:33

+0

除「無效」之外,沒有人瞭解您的問題。這不足以幫助您詳細說明。您應該編輯您的問題,並提供一個我們可以認真查看的最小工作示例!首先用您實際導入的內容替換「#some import statements ...」。 – 2015-02-07 16:27:41

+0

對不起,我已經提供了一個完整的代碼示例。 – Wikunia 2015-02-07 16:36:29

0

,我發現我的問題......

這兩條線:

http_wm = credentials.authorize(http) 
http_sv = credentials_verify.authorize(http) 

使用這是不允許同一個HTTP對象。

我現在用這個代碼:

http_wm = httplib2.Http() 
http_sv = httplib2.Http() 
http_wm = credentials.authorize(http_wm) 
http_sv = credentials_verify.authorize(http_sv)