I'd like to know how I can get the user details, First name, Last name and Email Id, using the access token from Google API. Currently, I'm using the following piece of code which returns me
{ "error": "invalid_grant", "error_description": "Bad Request" },
The server side c# code using RestSharp
var client = new RestClient("https://oauth2.googleapis.com/token");
var request = new RestRequest(Method.POST);
request.AddParameter("code", accessToken);
request.AddParameter("redirect_uri", "[my domain ]/oauth?provider=google");
request.AddParameter("client_id", "[ClientId]");
request.AddParameter("client_secret", "[ClientSecret]");
request.AddParameter("scope", "openid email profile");
request.AddParameter("grant_type", "authorization_code");
IRestResponse response = client.Execute(request);
In the OAuth consent screen
in Google Console, I haven't added anything under Scopes
. Could this be a problem? I'm accessing only basic information. Also, I'm not sure which scopes to add.
Thanks in advance and appreciate any help on this.
Update: I tried adding this scope - https://www.googleapis.com/auth/userinfo.profile but its still same issue.
This is the flow I have. I have this link on the page
https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=<Redirect url>?provider=google&prompt=consent&response_type=code&client_id=<Client id>&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+openid&access_type=offline
On clicking this link, the user is redirected to the Google authentication page. Once authenticated it redirects to the redirect URL with the access token which is then validated on the server-side and gets the user info.