Search code examples
google-cloud-platformgoogle-apigoogle-oauth

Can't login with google.auth Python


import google.auth
import os
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

credentials = "./token.json"

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credentials

scopes = ["https://www.googleapis.com/auth/classroom.courses.readonly", "https://www.googleapis.com/auth/classroom.courses"]

auth, f = google.auth.default()
print(auth.valid)

try:
    service = build('classroom', 'v1', credentials=auth)

    response = service.courses().list().execute()
    print(response)

except HttpError as error:
    print(f"An error occurred: {error}")

I get an empty dictionary (courses), as well as when outputting auth.valid - False is displayed, what should I do?

P.S - I tried to specify scopes - but it also did not give any result, the token is still not working.


Solution

  • If you are using a service account with Google classroom you need to impersonate the domain administrator, assuming you've delegated authority.

    By configuring impersonation it will be able to preform actions on the behalf of a user on the domain who does have acces.