我想使用python和验证网站api(v1)在我的网站管理员工具中验证网站。在此示例中,我希望使用验证API来获取所有经过验证的站点,因为该函数没有参数。 (我知道通过网站管理员工具也可以)
#!/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 :(得分:0)
确保您完整阅读并理解https://developers.google.com/site-verification/v1/invoking。特别需要欣赏:
验证新网站
要验证网站,
- 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.
您需要做的就是正确构建两个请求getToken
和insert
。什么&#34;正确&#34;在上面链接的文档中详细讨论了。
如果您真的需要有关代码的帮助,则需要显示一个最小的工作示例,以便我们可以重现您检索的错误。
答案 1 :(得分: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)