google-searchconsole icon indicating copy to clipboard operation
google-searchconsole copied to clipboard

Add support for Service Accounts

Open joshcarty opened this issue 8 years ago • 3 comments

Google allow users to authenticate using Service Accounts. We should allow users to authenticate with Google Search Console using Service Account credentials.

joshcarty avatar Jan 10 '18 23:01 joshcarty

If this helps, there is a pretty simple flow to authenticate with service accounts:

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

# environment
path = '/Users/******/Documents/cronjobs/service.json'
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = path

# authenticaton
scope = ['https://www.googleapis.com/auth/webmasters']
creds = ServiceAccountCredentials.from_json_keyfile_name('service.json', scope)
searchconsole = build('webmasters', 'v3')

# select the site(s) of interest
sites = searchconsole.sites().list().execute()

For this to work, one must give permission to the service account email. Some of the steps can be seen in this video from Twilio. What's missing is the link between this authentication and the functionalities of your package. In your authentication example, the account is a <searchconsole.account.Account(client_id='*******')>, while creds from my example is a <oauth2client.service_account.ServiceAccountCredentials at 0x1141a9160>

ghost avatar Apr 09 '18 10:04 ghost

Hey, thanks for this!

I've had a go at implementing support for service accounts using google.oauth2.service_account. I don't have access to an authorised service account at the moment but feel free to download the service-account branch and try it out: pip install git+https://github.com/joshcarty/google-searchconsole@service-accounts

Let me know if you have any luck!

joshcarty avatar Apr 09 '18 16:04 joshcarty

@joshcarty I can confirm that your code changes are working properly with a real service account in production. By merging this branch to master and fixing the 2 minor conflicts other people can use it as well.

symbiotech avatar Sep 28 '21 14:09 symbiotech