2

I want to read Gmail atom feed to get new mails notification with C#. I followed the answer here: Link1, but it gave me a 401 unauthorized error. I copy the code in the answer as followed:

try {
   System.Net.WebClient objClient = new System.Net.WebClient();
   string response;
   string title;
   string summary;

   //Creating a new xml document
   XmlDocument doc = new XmlDocument();

   //Logging in Gmail server to get data
   objClient.Credentials = new System.Net.NetworkCredential("Email", "Password");
   //reading data and converting to string
   response = Encoding.UTF8.GetString(
              objClient.DownloadData(@"https://mail.google.com/mail/feed/atom"));

   response = response.Replace(
        @"<feed version=""0.3"" xmlns=""http://purl.org/atom/ns#"">", @"<feed>");

   //loading into an XML so we can get information easily
   doc.LoadXml(response);

   //nr of emails
   nr = doc.SelectSingleNode(@"/feed/fullcount").InnerText;

   //Reading the title and the summary for every email
   foreach (XmlNode node in doc.SelectNodes(@"/feed/entry")) {
      title = node.SelectSingleNode("title").InnerText;
      summary = node.SelectSingleNode("summary").InnerText;
   }
} catch (Exception exe) {
   MessageBox.Show("Check your network connection");
}

The reason is that Google does not allow plain login anymore, and I have to use the OAuth2 method. I notice that Google has its API for OAuth2: Link2, Link3.

However, I don't know how to modify these examples to fit my case. It appears to me that I have to pass UserCredential, or at least some information(client_id, client_secret, token, etc) to a WebClient instance. How can I do that?

I know that there are several related questions on StackOverflow, but I don't think any of them are helpful. Most of them are outdated.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Yijun Yuan
  • 237
  • 1
  • 12
  • 1
    Following the steps from the [Quickstart](https://developers.google.com/gmail/api/quickstart/dotnet) should allow you to use OAuth2 correctly. Have you done steps 1 and 2 properly? – Jescanellas Sep 09 '19 at 09:08
  • Any special reason why you are using atom-feed and not the gmail api? You also dont appear to be using the google .net client library or the gmail api so i have removed those tags. – Linda Lawton - DaImTo Sep 09 '19 at 13:45

0 Answers0