我正在尝试将 OAuth 2.0 用于 Google 网站管理员工具(搜索控制台)的服务器到服务器应用程序,因此我已按照此处的说明进行操作。
此应用程序不在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
具有所有者权限。
有任何想法吗?
谢谢