如何使用 Google 登录访问令牌而不是授权代码从 Google Search Console 获取数据?

How to use the Google Sign In access token instead of authorization code for getting the data from the Google Search Console?

我想使用 Google 登录 access_token 访问 Google Search Console 中列出的网站数据(当使用 Google 登录)。

但是,问题是我只能通过使用 authorization_code 访问该数据,可以通过转到生成的 authorize_url 并使用登录从 OAuth2-Consent 屏幕复制注册的 Google 帐户。

这是代码的最低可复制版本

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

CLIENT_ID = 'YOUR_CLIENT_ID'
CLIENT_SECRET = 'YOUR_CLIENT_SECRET'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/webmasters.readonly'
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, redirect_uri=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)
http = httplib2.Http()
http = credentials.authorize(http)
        
webmasters_service = build('webmasters', 'v3', http=http)

def get_property_list(webmasters_service):
    '''
    Get a list of validated properties from GSC
    '''
    site_list = webmasters_service.sites().list().execute()
 
    # Filter for verified websites
    verified_sites_urls = [s['siteUrl'] for s in site_list['siteEntry']
                        if s['permissionLevel'] != 'siteUnverifiedUser'
                            and s['siteUrl'][:4] == 'http']
    return verified_sites_urls
        
        
print({"available_websites": get_property_list(webmasters_service)})

考虑到我将从另一台实现了 Google 登录功能的服务器获得 Google 登录访问令牌作为请求参数。

所以,我的问题又是如何使用该令牌访问相同的数据,而不是从 OAuth2 同意屏幕手动获取 auth_code?

我已经关注了上面评论中的documentation shared by DaImTo。并修改如下代码:


from oauth2client.client import  OAuth2WebServerFlow
import httplib2
from apiclient.discovery import build
from oauth2client import tools, file

CLIENT_ID = 'YOUR_CLIENT_ID'
CLIENT_SECRET = 'YOUR_CLIENT_SECRET'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/webmasters.readonly'
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

# Acquire and store oauth token.
storage = file.Storage('token.json')
credentials = storage.get()

if credentials is None or credentials.invalid:
    flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, redirect_uri=REDIRECT_URI)
    authorize_url = flow.step1_get_authorize_url()
    credentials = tools.run_flow(flow, storage)

http = httplib2.Http()
http = credentials.authorize(http)
        
webmasters_service = build('webmasters', 'v3', http=http)

def get_property_list(webmasters_service):
    '''
    Get a list of validated properties from GSC
    '''
    site_list = webmasters_service.sites().list().execute()
 
    # Filter for verified websites
    verified_sites_urls = [s['siteUrl'] for s in site_list['siteEntry']
                        if s['permissionLevel'] != 'siteUnverifiedUser'
                            and s['siteUrl'][:4] == 'http']
    return verified_sites_urls

print({"available_websites": get_property_list(webmasters_service)})

现在工作正常,无需任何手动交互即可从 OAuth2-Consent 屏幕复制和粘贴 authorization_code