无法使用Python连接到Google Search Console API

时间:2019-01-21 14:49:38

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

我正在尝试从Google Search Console提取数据,为此,我使用的是python API。

设置API后,出现以下错误:

<HttpError 403 when requesting https://www.googleapis.com/webmasters/v3/sites/https%3A%2F%2Fwww.example.com/searchAnalytics/query?alt=json returned "User does not have sufficient permission for site

我在eleswhere上查找,并读到我必须将自己的“ Google服务帐户”地址添加为网站管理员的所有者。但是,我如何才能允许其他用户使用我的服务?

以下脚本可以一直工作到身份验证弹出为止,

import httplib2

from apiclient import errors
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow


# Copy your credentials from the console
CLIENT_ID = 'id'
CLIENT_SECRET = 'secret'

# Check https://developers.google.com/webmaster-tools/search-console-api-original/v3/ for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/webmasters.readonly'

# Redirect URI for installed apps
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

# Run through the OAuth flow and retrieve credentials
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print ('Go to the following link in your browser: ' + authorize_url)
code = input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)

# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)

webmasters_service = build('webmasters', 'v3', http=http)

webmasters_service.searchanalytics().query( 
    siteUrl='https://www.example.com',
    body={
        "startDate": "2018-01-01",
        "endDate": "2019-01-21",
        "dimensions": ["country", "date"],
        "searchType": "web"
        }
    ).execute()

我可以获取site list,但不能获取分析数据 请指导,谢谢!

0 个答案:

没有答案