How to do impersonation #513
Replies: 2 comments 2 replies
-
I dont really understand the question. But if you want to use service account you can just do |
Beta Was this translation helpful? Give feedback.
-
In case someone else has this issue, I think I've found the solution. You need to create a custom credentials object to set the account you want to impersonate. This assumes you've already configured the service account with domain-wide delegation. Note: this only works within a Google Workplace domain and has to be setup by an Administrator. I assume there are other requirements if you are creating a public facing application, YMMV. import pygsheets
SCOPES = ["https://www.googleapis.com/auth/drive"]
credentials = (
pygsheets
.authorization
.service_account
.Credentials
.from_service_account_file("./path/to/service_account_credentials.json", scopes=SCOPES)
.with_subject("user@yourdomain.com")
)
gc = pygsheets.authorization.Client(credentials=credentials) I was able to use the docs here: https://developers.google.com/identity/protocols/oauth2/service-account#authorizingrequests to piece it together. Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
I have a service account, and a regular account. Regular account has all the permissions too edit sheets. How do I connect using service account and impersonating the user account?
I believe the setup on GCP is already done because a Java application using the same service account and impersonating the user account works (using com.google.api.client.googleapis.auth.oauth2.GoogleCredential). How can this be done through pygsheets?
Beta Was this translation helpful? Give feedback.
All reactions