Avenger
Avenger

Reputation: 877

Authorize in google search console

I want to get sites of google searh console.

I am suing the code:

from google.oauth2.credentials import Credentials
from googleapiclient import discovery

class GoogleSearchConsoleApiClient():
    SCOPES = ['https://www.googleapis.com/auth/webmasters.readonly']
 
    def __init__(self):
        pass

    def get_sites(self, creds):
        try:
            service = discovery.build('searchconsole', 'v1', credentials=creds, cache_discovery=False)
            site_list = service.sites().list().execute()
            print(site_list)
        except Exception as e:
            raise e
        return url

    def execute(self):
        credentials = {
            "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
            "auth_uri": "https://accounts.google.com/o/oauth2/auth",
            "client_email": "XXXXX",
            "client_id": "XXXXX",
            "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/XXXX",
            "owner": "XXX",
            "private_key": "XXX",
            "private_key_id": "XX",
            "project_id": "XXX",
            "token_uri": "https://oauth2.googleapis.com/token",
            "type": "service_account"
        }
        credentials = Credentials.from_authorized_user_info(credentials, scopes=self.SCOPES)
        sites_list = self.get_sites(credentials)
        return sites_list


if __name__ == '__main__':
    a = GoogleSearchConsoleApiClient().execute()
    print(a)

While running code, I am getting exception:

credentials = Credentials.from_authorized_user_info(credentials, scopes=self.SCOPES)

Error:

File "/Users/j/venv/lib/python2.7/site-packages/google/oauth2/credentials.py", line 231, in from_authorized_user_info
    "fields {}.".format(", ".join(missing))
ValueError: Authorized user info was not in the expected format, missing fields client_secret, refresh_token

.

Is there any way of authorization that could work with the given credentials?

Upvotes: 0

Views: 502

Answers (1)

Kris
Kris

Reputation: 8868

The auth details seems to be of a service account. For that you need to use service account authorization like below.

from google.oauth2 import service_account
auth_credentials = service_account.Credentials.from_service_account_info(credentials)

You can pass the auth_credentials to the service.

Upvotes: 2

Related Questions