Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Google Search Console API from Google Collaboratory environment?

I want to use google search console api from Google Collaboratory environment.

I tried this code but I got an error of "TypeError: expected str, bytes or os.PathLike object, not dict".

I know that the returned type from files.upload() does not fit to expected type from ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_LOCATION, SCOPES).

However, I don't know how I correct it.

It is the first time to ask a question in Stack Overflow and I am a beginner in programming and English, so would you teach me easily?

import pandas as pd

from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

SCOPES = ['https://www.googleapis.com/auth/webmasters.readonly']

from google.colab import files

uploaded = files.upload()

KEY_FILE_LOCATION = uploaded

credentials = ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_LOCATION, SCOPES)
webmasters = build('webmasters', 'v3', credentials=credentials)

url = '*********'

d_list = ['query', 'page']
start_date = '2019-12-01'
end_date = '2019-12-31'
row_limit = 5000

body = {
    'startDate': start_date,
    'endDate': end_date,
    'dimensions': d_list,
    'rowLimit': row_limit
}

response = webmasters.searchanalytics().query(siteUrl=url, body=body).execute()
df = pd.io.json.json_normalize(response['rows'])

for i, d in enumerate(d_list):
    df[d] = df['keys'].apply(lambda x: x[i])

df.drop(columns='keys', inplace=True)
df.to_csv('{}.csv'.format(start_date), index=False)

print(df)
like image 644
Naoshi Avatar asked Jan 21 '26 05:01

Naoshi


1 Answers

I resolved it. First, I uploaded the Key_file to the same directory with this program file. Then, I connected the program and the Google Drive account by "mounting". Lastly, I got the absolute path to the json key_file.

import pandas as pd


from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

SCOPES = ['https://www.googleapis.com/auth/webmasters.readonly']

from google.colab import drive
drive.mount('/content/drive')

KEY_FILE_LOCATION = '/content/drive/My Drive/<path-to-json>'

credentials = ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_LOCATION, scopes=SCOPES)
webmasters = build('webmasters', 'v3', credentials=credentials)

url = '*********'

d_list = ['query', 'page']
start_date = '2019-12-01'
end_date = '2019-12-31'
row_limit = 5000

body = {
    'startDate': start_date,
    'endDate': end_date,
    'dimensions': d_list,
    'rowLimit': row_limit
}

response = webmasters.searchanalytics().query(siteUrl=url, body=body).execute()
df = pd.io.json.json_normalize(response['rows'])

for i, d in enumerate(d_list):
    df[d] = df['keys'].apply(lambda x: x[i])

df.drop(columns='keys', inplace=True)
df.to_csv('{}.csv'.format(start_date), index=False)

print(df)
like image 119
Naoshi Avatar answered Jan 23 '26 19:01

Naoshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!