【发布时间】:2016-04-12 19:16:40
【问题描述】:
我正在尝试将 OAuth 2.0 用于 Google 网站管理员工具(搜索控制台)的服务器到服务器应用程序,因此我已按照说明进行操作 here。
此应用程序不在 Google App Engine 或 Google Compute Engine 上
创建了一个服务帐户并启用了域范围的委派。下载.json 文件并将其存储到脚本的根目录。
示例:
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
from httplib2 import Http
scopes = ['https://www.googleapis.com/auth/webmasters.readonly']
credentials = ServiceAccountCredentials.from_json_keyfile_name(
'keyfile.json', scopes=scopes)
http_auth = credentials.authorize(Http())
webmasters_service = build('webmasters', 'v3', http=http_auth)
site_list = webmasters_service.sites().list().execute()
print(site_list)
但我得到了
{} 空数据集。即使我更改了keyfile.json 中的电子邮件地址。这告诉我该文件没有以某种方式被使用。因此尝试获取帐户中的站点列表,结果为空。
如果我这样做了
site_list = webmasters_service.sitemaps().list(siteUrl="www.example.com").execute()
我明白了:
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/webmasters/v3/sites/www.example.com/sitemaps?alt=json returned "User does not have sufficient permission for site 'http://www.example.com/'. See also: https://support.google.com/webmasters/answer/2451999.">
这再次告诉我,此帐户无权获取给定 URL 的站点地图,因为它没有适当的权限。
此帐户是所有者帐户,service account 具有所有者权限。
有什么想法吗?
谢谢
【问题讨论】:
标签: python google-search-console google-api-python-client google-api-webmasters