GoogleCloudPlatform/node-red-contrib-google-cloud

"SyntaxError: Unexpected token in JSON at position 2"

d0hnj0e opened this issue · 9 comments

When adding the json key-file, I am receiving the error

"SyntaxError: Unexpected token in JSON at position 2"

and the sentiment analysis is not working. And idea how this can be fixed or what I am doing wrong?

I have seen errors like this when the content of the file wasn't a good JSON document. Can we validate what was pasted or referenced is the correct content? Maybe post your JSON here after having masked out anything sensitive. We sure don't want loose keys floating around. If needed, ping me at user kolban on the GCP slack community here:

googlecloud-community.slack.com

Hi Kolban, thanks for your response! It looks somewhat like this (directly copied from GC):

{
  "type": "service_account",
  "project_id": "xy1234",
  "private_key_id": "something",
  "private_key": "-----BEGIN PRIVATE KEY-----\awholelotofstuff\n-----END PRIVATE KEY-----\n",
  "client_email": "1234-compute@developer.gserviceaccount.com",
  "client_id": "1234",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/a1234/1234-compute%40developer.gserviceaccount.com"
}

Hmmm ... this looks just fine!! Am pinging you on Slack ... I'm thinking a screen share might be needed. Is there more to the error message ... such as what line the error is being detected?

After much study and working with the user, we could find nothing amiss. The user then tried the same operations for a different PC ... and it worked first time. We have no idea what was distinct about the failing PC. Nothing at all seemed odd other than it not working.

Same problem here.. I'm also wondering what a different PC adds to the mix... was this the same OS? Same key used?

Let's see if we can figure it out ... I'm available to assist live if needed. Google chat to kolban@kolban.com as needed.

What locale is your browser running? Are there any hidden or special characters being inserted by the copy/paste? Is perhaps a tab or some other non-printable character being inserted? Can you post your key AFTER having rendered it invalid and XXXXing out anything at all you wouldn't want public?

Hi thank for your help, I had another project running without issues and unfortunately didnt find an obvious difference between these Certs.
The "bad" one was bad even in the other machine and having new certs re-issued caused the same problem.

I ended up deleting the service account ,creating a new one.. and regenerating the json file again..was able to connect.

Hi!

I also saw this error "SyntaxError: Unexpected token in JSON at position 2". I got the credentials working when I reshaped the JSON to fit in just one single line, like:
{"type": "service_account","project_id":...

I hope this gives some clue about the JSON parsing exception.

Thank you @vikingbrain .. That was super nice of you to post. The odd thing is that it appears to be working for most folks. I hear that there are enough users of this package that if it were something grossly broken then we'd hear more about it. For the first user, I shrugged it off as something very odd in their machine. Maybe a strange plugin in their browser. But now that it is cropping up more I'm very keen on getting to the bottom of it. Next time we find an instance I'm going to ask to get my hands on a copy of the offending data or work live with them. If we search the web for "Unexpected token in JSON at position 2" we get results ... quite a few in fact. When I read the causes, each and every time I seem to see that the cause is that the text of the JSON is broken. When I eyeballed it before, I could see NOTHING broken. At the time I figured that there might have been something odd in the browser and asked the user to try somewhere else .. and for them it worked.