Close

Service account blues

A project log for PDF Merge

Overlay synchronized spreadsheet calculations on PDF forms

lion-mclionheadlion mclionhead 10/26/2023 at 18:410 Comments

Managing the oauth consent prompts was turning into an impossible dream.  The mane problem was the number of places the text drawing could be interrupted & the browser could have to be redirected to a consent prompt, then the text drawing could have to resume.

The internet recommended using a service account for applications that used a server.  The general procedure for creating the service account is given on

https://robocorp.com/docs/development-guide/google-sheets/interacting-with-google-sheets

The trick is it generates a JSON file for the service account.  You have to copy the client_email from the JSON file to the access list for the spreadsheet.  That user has to be set to viewer mode.

Helas, a similar oauth dance is required to get an access token for the service account, except the job of the consent prompt can be performed by the server without interrupting the browser. 

Both the goog & the GPT drew blanks for this procedure.  There just aren't a lot of animals writing programs in their dorm rooms for accessing the goog.

There was a buried php blob.

https://stackoverflow.com/questions/67338219/a-google-sheet-api-http-request-with-service-account-key-auth-and-without-goog

After translating to python, this failed with 1 of 2 errors:

  "error": "invalid_grant",
  "error_description": "java.security.SignatureException: Invalid signature for token

or

curl output={"error":"invalid_grant","error_description":"Invalid JWT Signature."}

The only clue was + being replaced by spaces in the response.  == was replaced by \u003d\u003d

The --trace-ascii [filename] option causes curl to save all the TCP data to a file, showing all the + & = were properly sent.

-----------------------------------------------------------------------------------------------------------

After many dead ends, the service account idea got abandoned.  It actually is more laborious for the user to deal with the JSON file & access list, hence why they're probably not used.  The consent prompt only requires the user's existing goog account & the developer console.

The problem with the consent prompt is the need to go into a login sequence in the middle of a redraw operation.  The easiest workaround, since it's a rarely used program by 1 lion, is to just save the project & deal with the login sequence when reading a spreadsheet fails.  99% of the effort is preventing an infinite loop if the login fails.  It requires multiple reloads.  Reload #1 loads the consent prompt.  Reload #2 loads the page with a code= argument in order to load the access token.  Reload #3 deletes code= from the URL so a user reload works.  A state variable in the project tells whether it's in a login sequence or a user reload so it doesn't get stuck reloading.

Maybe there's a way to do it with popups or multiple html files.

Every javascript program involving network calls becomes a nest of async calls with callbacks.  They tried to solve this with the promise notation, which lions believe just replaces the callback with a then() block.  If you want a busy flag to be set before the 1st async call & unset after the last async/promise in the chain, you're out of luck.  There's no mention of the 1st then() waiting for the last nested async.  The lion kingdom settled on just calling everything with await instead of using callbacks or promises.  The language doesn't stop you from forgetting to use await if the function is async though.

Most language features these days are marketing tools for selling the language, yet programmers still have yet to believe languages are subject to the same business economics as beer & pizza.  For a 1 user program where no-one cares about the browser freezing, the mandate for network calls to be async has no purpose besides a marketing tool to sell the language.

That got it to finally pasting & syncing spreadsheet values on the PDF.

The mane problem with this is

the nest of menus you have to traverse to copy the cell.  There is a macro feature but it doesn't capture the menu item.  It only captures the cell selection.  There are ways to create add-ons.  There might already be a PDF overlay addon.  This is where it gets into real dry subject matter like https://robertohuertas.com/.

On the 1 paw, it can make you wonder if you're in the right field if visual studio extensions & license managers don't peak your interest.  On the other paw, really needing a fast way to link to a cell can make a goog sheet extension look fascinating.  Lions aren't there yet.

Guys who get into the dry stuff are really financially desperate, have a real personal need for a feature, or get really motivated by corporate pep talks.

Discussions