I have a batch-like application which is periodically invoked by a scheduler, no human user involved. It uses the Perl Net::Google::Spreadsheets package to update some cells in a Google-sheets spreadsheet, by data fetched from a database.
For a long time it was simple to authenticate itself by just providing a username and a password to the 'new' method of the package. But as of lately, Google require us to authenticate using the OAuth2 protocol.
J.T. provided a solution that I am sure is very helpful to many people more knowledgeable than I am. I will appreciate however if somebody could answer some questions to clarify it, as follows:
Creating credentials: once you created a project in the Google Developer Console and you are asking to create a new client ID, you are presented with three options:
- For a "Web Application" (It then asks to provide an "Authorized JavaScript origins" and an "Authorized redirect URIs". Are these relevant to my situation?)
- For a "Service Account" (I Suspect this is the choice for me, but without answers to the following questions I can't verify it.)
- For an "Installed application" (Can one give examples to such?)
Which one should I choose for may application?
Should I asked for a JSON or a P12 key?
What do I do with the various types of entities I get? What do I embed in the Perl script?
At line 13, J.T commented that "you will need to put code here and receive a token". What kind of code? Doing what?
Since there is no human user, I need the application to do the full authentication process by itself. J.T.'s code prompts a user for a 'code'. Is this code one of the "credentials" entities? How do I do it?
In other words, I need somebody to walk me gently through the whole process, step by small step.
Thanks folks!
MeirG
I had to go through this too, without knowing much at the start, so I'm happy to help explain it. Here are the answers, but feel free to ask for clarification. Basically, you need to first run a script that requires manual intervention - this lets you obtain an access token from Google, which your batch-like script can then use over and over without human intervention. So you have to jump through some hoops at the start, but once that's done, you're all set. So:
1b. You'll be asked to configure a "consent screen". Doesn't really matter what you put here - just give the project a title.
1c. For "redirect uri", delete the provided "example.com" value and enter "https://developers.google.com/oauthplayground".
Ignore the JSON and P12 keys; they are for other types of applications. Once you fill in the above info and click "Create Client ID", you'll get a page (after a pause) that displays a client ID and client secret. Those are the two strings you'll need in the code below.
The code below is essentially the same solution that you linked to above (and I relied heavily on it), but I've edited it to change a few things, primarily to give more information about what's going on. Once you have added your client ID and client secret to the code below, run it. Then you'll go through these steps:
If all goes well, the script will exchange that code for an access token, and save the token on disk. Then your batch script can use that token repeatedly.
Here's the expanded code to do all of this:
Once you've gotten that working and have the token stored on disk, then the beginning of your batch script can set up the spreadsheet access like this: