OAuth 2.0服务器到服务器凭据授权失败(搜索控制台-网站管理员工具)



我正在尝试使用OAuth 2.0用于谷歌网站管理员工具(搜索控制台)的服务器到服务器应用程序,所以我遵循了这里的说明。

此应用程序不在Google App EngineGoogle 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具有所有者权限。

有什么想法吗?

感谢

我不想回答自己的问题,但以下是我如何做到的;

  • 转到:https://www.google.com/webmasters/tools/home?hl=en
  • 选择您的网站
  • 单击齿轮(右上角),然后单击Users and Property Owners
  • 现在转到:https://console.developers.google.com/permissions/projectpermissions?project=PROJECT-名称
  • 复制服务帐户的电子邮件地址,并将其添加到权限级别为FULLUsers and Property Owners

所以底线是,在上

  • https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority

  • https://developers.google.com/webmaster-tools/v3/quickstart/quickstart-python#step_1_enable_the_search_console_api

教程有人忘记提到将新生成的电子邮件地址添加到应用程序的权限部分。。。

最新更新