Jorginton
Jorginton

Reputation: 79

Keep running into errors with the Google Cloud API

Recently I have been working with the Google Cloud API for some data analysis projects. Currently I keep following instructions from other websites and make my own dataset based off that. I keep running into errors with the Google Cloud API however.

For example, I'm trying the method from the following article: https://practicaldatascience.co.uk/data-science/how-to-query-the-google-search-console-api-with-ecommercetools

However, Python responds with the following:

Error: Service account info was not in the expected format, missing fields token_uri, client_email.

I created basic credentials under 'OAuth 2.0 Client IDs' as mentioned in the article.

Here is the code I currently have:

import pandas as pd
from ecommercetools import seo
from oauth2client.service_account import ServiceAccountCredentials

pd.set_option('max_colwidth', 100)

key = "G:\Python Projects\query_gsc_secrets.json"
site_url = "https://startselect.com/"
start_date = "2019-01-01"
end_date = "2020-12-31"

print(key)


payload = {
    'startDate': start_date,
    'endDate': end_date,
    'dimensions': ["page"],
    'rowLimit': 10000,
    'startRow': 0
}

df = seo.query_google_search_console(key, site_url, payload)
df.sort_values(by='clicks', ascending=False).head()

The reason why I'm asking this is because this error seems to be recurring in one form or another. Am I missing something when creating credentials? Is there a mistake in the code?

Thanks in advance!

Upvotes: 2

Views: 3483

Answers (1)

Totem
Totem

Reputation: 1222

It seems like the format of the key is not as expected.

You've mentioned you created the basic credentials under the "OAuth 2.0 Client IDs" but if you read through the linked article, you can see that the API expect a service account key.

Service account key format looks like this:

{
"type": "service_account",
"project_id": "project-id",
"private_key_id": "key-id",
"private_key": "-----BEGIN PRIVATE KEY-----\nprivate-key\n-----END PRIVATE KEY-----\n",
"client_email": "service-account-email",
"client_id": "client-id",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": 
"https://www.googleapis.com/robot/v1/metadata/x509/service-account-email"
}

Which matches the format requested in the error missing fields token_uri, client_email.

In this GCP documentation you can read how to create a service account and service account key

Upvotes: 3

Related Questions