Search code examples
python-3.xgoogle-apigoogle-signingoogle-search-consoleoauth2client

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


I want to access the listed websites data in the Google Search Console using the Google Sign-In access_token (that one can get as the response when using Google Sign-In).

But, the thing is I can access that data only by using the authorization_code that can be copied from the OAuth2-Consent screen by going to the generated authorize_url and signing in using the registered Google account.

Here's the minimum reproducible version of the code:

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)})

Consider that I'll be provided with the Google Sign-In access-token as the request-parameter from another server which has implemented Google Sign-In feature.

So, again my question is how can I access the same data using that token instead of manually getting the auth_code from the OAuth2 consent screen ?


Solution

  • I have followed the documentation shared by DaImTo in the comments above. And modified the code as shown below:


    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)})
    

    It's working fine now, without any manual interaction for copying and pasting the authorization_code from the OAuth2-Consent screen.