Connect a Google Cloud function in Python to the Google Search Console API

Viewed 230

I want to create a Google Cloud Function in Python to

  • connect to the Google Search Console API
  • extract data
  • pipe it into BigQuery

I can find various code samples to authenticate and connect via OAuth, secret keys etc however these all appear to apply to Python scripts running locally, not in the cloud. (I need this to run in the cloud, unattended).

The Google documentation says that, within a Cloud Function, authentication can happen automatically using "Application Default Credentials (ADC) to automatically find your service account credentials".

This code sample for example connects and authenticates to Cloud Storage:

def implicit():
    from google.cloud import storage

    # If you don't specify credentials when constructing the client, the
    # client library will look for credentials in the environment.
    storage_client = storage.Client()

    # Make an authenticated API request
    buckets = list(storage_client.list_buckets())
    print(buckets)

I cannot however find any documentation on how to create a Google Search Console client that will authenticate via the ADC mechanism.

Help please!

2 Answers

According to Search API documnetation OAuth 2.0 is only way:

Your application must use OAuth 2.0 to authorize requests. No other authorization protocols are supported.

Cloud Storage has convenient API in Python (and other languages as well) and it is dealing with this ADC authentication. When it comes to Google Search API, I am not very familar with it, but I have checked the example code. In the example, if I understand correctly, authentication is done outside of the api using oauth2client.

But, I am sure, that you can do it with OAuth 2.0 from Cloud Function as well in the exactly same why as you it locally.

Related