Google Search Console API - 域范围授权 - 不返回任何结果

时间:2016-02-24 09:18:31

标签: python api google-api google-webmaster-tools google-api-python-client

我为我们的Google应用域设置了(至少尝试过)域范围的委派,以便与python脚本集成。特别是,我目前正在尝试与Search Console API进行交互。

这就是我所做的:

  1. 设置服务帐户
  2. 启用域范围授权
  3. https://www.googleapis.com/auth/webmasters.readonly添加到Google管理控制台,以获取授权的API客户端列表。
  4. 根据Google提供的简单脚本设置
  5. 这是我的test.py脚本:

    #!/usr/bin/python
    
    from oauth2client.service_account import ServiceAccountCredentials
    from httplib2 import Http
    from apiclient.discovery import build
    
    
    scopes = [
            'https://www.googleapis.com/auth/webmasters.readonly',
        ]
    
    credentials = ServiceAccountCredentials.from_json_keyfile_name('/path/to/my/key.json', scopes)
    delegated_credentials = credentials.create_delegated('me@mydomain.com')
    
    http_auth = credentials.authorize(Http())
    
    webmasters_service = build('webmasters', 'v3', http=http_auth)
    
    site_list = webmasters_service.sites().list().execute()
    
    print site_list
    

    结果:

    {}
    

    糟糕,

    我知道这是不正确的,因为当我使用API Explorer并授权me@mydomain.com时,我会在Search Console中看到我的网站的完整列表。有些东西坏了,我不确定问题出在哪里。

    对于我在此过程中错过的步骤的任何想法?一切似乎都在验证。我没有403

    抓我的头。

1 个答案:

答案 0 :(得分:1)

所以我不是100%确定我做了什么,或者我是如何修理的。

我正在使用管理控制台API客户端访问实用程序中的权限,并且没有尾随/。最后,我将范围从https://www.googleapis.com/auth/webmasters.readonly更改为https://www.googleapis.com/auth/webmasters,并且确实有效!

我还没有回去玩readonly范围,部分是因为我希望我的程序具有写访问权限。我只是将readonly用于测试目的。

相关问题