1
votes

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);
}
1

1 Answers

0
votes

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.