0

So I am working with google sheets API , where I need to authorize the credentials before I can hit api for my account , for that OAuth2 client provides an auth URL which opens in browser and there I have to login in Gmail account and then accept the permissions and then it redirects to a redirect URI and in it's URL there is a code.

But I want to get all this done automatically as this will be part of my backend code , and I want it be automated without me going to browser and logging in . Can this be done using puppeteer ? or anything else ?

Seth
  • 57
  • 10

1 Answers1

1

The only option to avoid user interaction is to use a service account with domain-wide delegation. And to use this, you need to be a Workspace domain admin, and you can only act on behalf of users in your Workspace domain.

If you are not a domain admin, there's no way to avoid the workflow you mentioned, with user interaction.

Reference:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • @lamblichus With service account way can I hit this API https://docs.google.com/spreadsheets/d/${spreadsheetId}/export?format=${format}&id=${spreadsheetId} without having to send access token in headers , as I would have to do in oAuth2 way2 ? – Seth Jun 03 '22 at 13:09
  • @ShubhamSeth You would send an access token, but the access token is retrieved programmatically, without user interaction. I don't know which language you're using, but here's a description of the process https://developers.google.com/identity/protocols/oauth2/service-account – Iamblichus Jun 03 '22 at 13:59
  • @lamblichus I have created the service account with domain-wide delegation , but I am unable to understand how to get access token from that service account. And language I am using is Javascript (NodeJS) – Seth Jun 06 '22 at 08:46
  • @ShubhamSeth The library can do that for you and call the API in one step. See, for example, the sample in [this answer](https://stackoverflow.com/a/60092848) (in this case it's calling Directory API instead of Sheets API, but the workflow is the same). Or alternatively, [this one](https://stackoverflow.com/q/58565907) which uses Sheets API but no impersonation. I hope this is useful to you. – Iamblichus Jun 06 '22 at 11:07