Garima
Garima

Reputation: 201

Chrome Extension identity: OAuth2 request failed: Service responded with error: 'bad request'

I followed https://developer.chrome.com/extensions/tut_oauth exactly step by step but I am stuck at

https://developer.chrome.com/extensions/tut_oauth#identity_permission

where after I execute my extension, instead of getting the token, I get the error:

Unchecked runtime.lastError while running identity.getAuthToken: 
OAuth2 request failed: Service responded with error: 'bad request'

Please suggest what is the possible cause of this error.

Upvotes: 16

Views: 7330

Answers (3)

B.Mr.W.
B.Mr.W.

Reputation: 19628

I changed the scope as suggested but the problem still persisted. Then I tried not only to reload the extension but also tried to update it and it worked in the end (errors next to Remove button went away and I am able to authenticate).

Honestly, I am not sure if it is the Update that did the magic or change the empty Scope that did the magic but here is an answer from a 2020 July user who got it working. :)

(PS, if you don't have that many Google friends, or your friends who don't bother to have image, likely your code will fail with data.photos[0].url in the next step, you just need to take care of that)

enter image description here enter image description here

Upvotes: 0

pratZ
pratZ

Reputation: 3346

tl;dr

Update the scopes to the following if its empty,

 "oauth2": {
    "client_id": "yourExtensionOAuthClientIDWillGoHere.apps.googleusercontent.com",
    "scopes":["https://www.googleapis.com/auth/userinfo.email"]
  }

People who were following the tutorial on OAuth2: Authenticate Users with Google

If you've landed into this problem, it is probably because it's 2020 and the documentation isn't updated.

The tutorial asks you to,

Include the "oauth2" field in the extension manifest. Place the generated OAuth client ID under "client_id". Include an empty string in "scopes" for now.

{
  "name": "OAuth Tutorial FriendBlock",
  ...
  "oauth2": {
    "client_id": "yourExtensionOAuthClientIDWillGoHere.apps.googleusercontent.com",
    "scopes":[""]
  },
  ...
}

but never updates it before calling the identity API call to fetch the token.

Updating the scopes, with the following should fix the issue,

"scopes": ["https://www.googleapis.com/auth/userinfo.email"]

Upvotes: 49

Mohammed AlBanna
Mohammed AlBanna

Reputation: 2398

I think that's because the scope is empty. I was like you follow the article, but found the problem is from the scope area.

Upvotes: 14

Related Questions