1

I am using with Google's OAuth 2.0 Playground using my own personal Google account, but I cannot seem to recover my Gmail address using the playground. I am using the api url https://www.googleapis.com/oauth2/v3/userinfo. But didn't get email address. I get various information about the user such as family name, first name, gender, picture, etc. I have referred with Google OAuth API to get user's email address? but it didn't work. I am using the code(c#-windows form) below(to get email address & user information)

string userinfoRequestURI = "https://www.googleapis.com/oauth2/v3/userinfo
HttpWebRequest userinfoRequest = (HttpWebRequest)WebRequest.Create(userinfoRequestURI);
userinfoRequest.Method = "GET";
userinfoRequest.Headers.Add(string.Format("Authorization: Bearer {0}", access_token));
userinfoRequest.ContentType = "application/x-www-form-urlencoded";
userinfoRequest.Accept = "Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

WebResponse userinfoResponse = await userinfoRequest.GetResponseAsync();
//userinfoResponse.
using (StreamReader userinfoResponseReader = new StreamReader(userinfoResponse.GetResponseStream()))
{
    // reads response body
    string userinfoResponseText = await userinfoResponseReader.ReadToEndAsync();
    output(userinfoResponseText);
}
funie200
  • 3,688
  • 5
  • 21
  • 34
Noufal
  • 115
  • 10

1 Answers1

0

When you authorized your request over on oauth playground, make sure you sent email.

If you did send the email scope then i recommend going though the People api rather then the userinfo endpoint, but remember to add the profile scope and the email scopes

The userinfo endpoint is not guaranteed to return the claims every time. Its better to go though the People api for that.

Tip: consider looking into the google api .net client library.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Thanks for the reply. Can you suggest how to add profile scope and the email scopes with above code – Noufal Dec 09 '20 at 10:30
  • Scopes are added to the code requesting the user authorization then the authorization is granted to the access_token. So no its not something thats added in the code above. It needs to be added when you request access over on oauth playground. I hope that you are only using Oauth playground for testing. – Linda Lawton - DaImTo Dec 09 '20 at 10:57
  • You are right, i am using the code string tokenRequestURI = "https://www.googleapis.com/oauth2/v4/token"; string tokenRequestBody = string.Format("code={0}&redirect_uri={1}&client_id={2}&code_verifier={3}&client_secret={4}&scope=&grant_type=authorization_code", code, System.Uri.EscapeDataString(redirectURI), clientID, code_verifier, clientSecret ); [How to given email scope with the above code ?] – Noufal Dec 09 '20 at 11:07
  • scope=email profile <--- you really should do some research and understand this rather than just randomly adding stuff Check the google .net client libary – Linda Lawton - DaImTo Dec 09 '20 at 11:32