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.