我想在我的网站管理员工具中使用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
)
你知道我做错了什么,或者我该如何调试代码吗?
确保您阅读并理解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
。我在上面链接的文档中详细讨论了"正确"的含义。
如果你真的需要代码方面的帮助,你需要展示一个最小的工作示例,这样我们就可以重现你检索到的错误。
我发现了我的问题。。。
这两条线:
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)