
OAuth is an open protocol to allow secure API authorization in a simple and standard method from desktop and web applications.
In this post I’ll show how to access Gmail account using 3-legged OAuth authentication method. The key advantage of this method is that it allows an application to access user email without knowing user’s password.
You can read more on OAuth authentication with Google accounts here:
http://code.google.com/apis/accounts/docs/OAuth_ref.html
Gmail IMAP and SMTP using OAuth:
http://code.google.com/apis/gmail/oauth/protocol.html
If your application/website is not registered, you should use following key and secret:
consumer key: “anonymous”
consumer secret: “anonymous”
Remember to add reference to Maill.dll and appropriate namespaces.
// C#
using Lesnikowski.Client.IMAP;
using Lesnikowski.Client.Authentication;
using Lesnikowski.Client.Authentication.Google;
const string userEmailAccount = "pat@gmail.com";
const string consumerKey = "anonymous";
const string consumerSecret = "anonymous";
GmailOAuth oauth = new GmailOAuth(
consumerKey, consumerSecret);
string url = oauth.GetAuthorizationUrl("http://localhost:64119/");
Process.Start(url);
// You can use Response.Redirect(url) in ASP.NET
string oauthVerifier = HttpUtility.UrlDecode(Console.ReadLine());
// You can use Request["oauth_verifier"].ToString() in ASP.NET
oauth.GetAccessToken(oauthVerifier);
using (Imap client = new Imap())
{
client.ConnectSSL("imap.gmail.com");
string oauthImapKey = oauth.GetXOAuthKeyForImap();
client.LoginOAUTH(oauthImapKey);
// Now you can access user's emails
//...
client.Close();
oauth.RevokeToken(oauthImapKey);
}
1.
GmailOAuth.GetAuthorizationUrl method returns url you should redirect your user to so he can authorize access.
As you can see Mail.dll is asking for access to user’s email information and Gmail access:

2.
If you don’t specify callback parameter, user will have to manually copy&paste the token to your application:

In case of a web project, you can specify a web address on your website. oauth_verifier will be included as the redirection url parameter.
After the redirection, your website/application needs to read oauth_verifier query parameter:

3.
GmailOAuth.GetAccessToken method authorizes the token.
4.
GmailOAuth.GetXOAuthKeyForImap method uses Google API to get the email address of the user, and generates XOAuth key for IMAP protocol (you can use GetXOAuthKeyForSmtp for SMTP).
5.
GmailOAuth.RevokeToken method revokes XOAuth key, so no further access can be made with it.
…and finally VB.NET version of the code:
' VB.NET
Imports Lesnikowski.Client.IMAP
Imports Lesnikowski.Client.Authentication
Imports Lesnikowski.Client.Authentication.Google
Const userEmailAccount As String = "pat@gmail.com"
Const consumerKey As String = "anonymous"
Const consumerSecret As String = "anonymous"
Dim oauth As New GmailOAuth(consumerKey, consumerSecret)
Dim url As String = oauth.GetAuthorizationUrl("http://localhost:64119/")
Process.Start(url)
' You can use Response.Redirect(url) in ASP.NET
Dim oauthVerifier As String = HttpUtility.UrlDecode(Console.ReadLine())
' You can use Request["oauth_verifier"].ToString() in ASP.NET
oauth.GetAccessToken(oauthVerifier)
Using client As New Imap()
client.ConnectSSL("imap.gmail.com")
Dim oauthImapKey As String = oauth.GetXOAuthKeyForImap()
client.LoginOAUTH(oauthImapKey)
' Now you can access user's emails
'...
client.Close()
oauth.RevokeToken(oauthImapKey)
End Using