【问题标题】:OAuth 2.0 Server to server Credentials authorization Fail(Search Console - Webmaster tools)OAuth 2.0 服务器到服务器凭据授权失败(搜索控制台 - 网站管理员工具)
【发布时间】:2016-04-12 19:16:40
【问题描述】:

我正在尝试将 OAuth 2.0 用于 Google 网站管理员工具(搜索控制台)的服务器到服务器应用程序,因此我已按照说明进行操作 here

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

有什么想法吗?

谢谢

【问题讨论】:

    标签: python google-search-console google-api-python-client google-api-webmasters


    【解决方案1】:

    我不想回答我自己的问题,但这是我如何让它发挥作用的;

    所以底线是,在

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

    【讨论】:

    • 您使用的是哪个版本的oauth2client?我尝试from oauth2client.service_account import ServiceAccountCredentials,但它看起来不像ServiceAccountCredentials 包含在我的oauth2client.service_account 版本中。我看到一个ServiceAccountCredentials 类...
    • 我正在使用google-api-python-client (1.5.0)。那是带有pip install --upgrade google-api-python-client 的版本。希望能帮助到你。 Doc
    • 我最初运行的是pip install --upgrade oauth2client,但由于我使用的是旧版本的pip,因此无法正确升级。我与pip 保持同步,现在oauth2client 拥有ServiceAccountCredentials 课程。谢谢!
    • 我的电子邮件有多个与之关联的 SearchConsole 站点。我没有放置这封新电子邮件(在keyfile.json 中),而是使用delegated_credentials = credentials.create_delegated('user@example.org')http_auth = delegated_credentials.authorize(Http()) 委派了我的电子邮件凭据,这使我可以访问我的电子邮件地址可以访问的所有站点。
    • 哦,我正在寻找一种方法来做到这一点。谢谢你的提示
    猜你喜欢
    • 2013-10-05
    • 2015-01-15
    • 2016-08-12
    • 1970-01-01
    • 2022-11-02
    • 2014-09-08
    • 2012-10-01
    • 2014-07-09
    • 1970-01-01
    相关资源
    最近更新 更多