1

Gmail API to get profile info

Base on this link, it is possible to get the email address from the authenticated profile. Unfortunately I wasn't able to find how to get this in C# API client.

I have tried this: GmailService.Users.GetProfile("me"), but this does not contain the email address.

I hope someone know how to do this with C# API client.

Thank you.

Andrei C.
  • 11
  • 1

2 Answers2

1

This worked for me

// Create Gmail API service.
GmailService service = new GmailService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = ApplicationName,
});

var gmailProfile = service.Users.GetProfile("me").Execute();
var userGmailEmail = gmailProfile.EmailAddress;
Zeshan
  • 71
  • 6
0

You could use a simple WebRequest and parse the JSON to get the emailAddress field.

You could build a Response model that would look something like:

public class APIResponse 
{
    public string emailAddress {get; set;}
    public int messagesTotal {get; set;}
    public int threadsTotal {get; set;}
    public int id {get; set;}
}

Then you will want to make a GET Request to the GoogleAPI url and deserialize the JSON response from your API Response class.

WebRequest request = WebRequest.Create("https://www.googleapis.com/gmail/v1/users/me/profile");
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
            request.Timeout = 50000;
            //Get response from server
            using (StreamReader stream = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.UTF8))
            {
                sResponse = stream.ReadToEnd();
                resp = new JavaScriptSerializer().Deserialize<APIResponse>(sResponse);
            }

Then you could get your emailAddress back by simply calling resp.emailAddress. I have not tested this code for the GoogleAPI but this should get you going in the right direction.

Andrew Reese
  • 854
  • 1
  • 11
  • 27
  • 1
    Gmail API has an api client for C# (NuGet package). I was hoping that it already contains the necessary method I could call to retrieve this info. – Andrei C. Jun 26 '20 at 14:00