<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog &#124; Limilabs &#187; OAuth</title>
	<atom:link href="http://www.limilabs.com/blog/tag/oauth/feed" rel="self" type="application/rss+xml" />
	<link>http://www.limilabs.com/blog</link>
	<description></description>
	<lastBuildDate>Mon, 21 May 2012 09:49:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Get Google contacts</title>
		<link>http://www.limilabs.com/blog/get-google-contacts?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=get-google-contacts</link>
		<comments>http://www.limilabs.com/blog/get-google-contacts#comments</comments>
		<pubDate>Fri, 13 Apr 2012 14:30:44 +0000</pubDate>
		<dc:creator>Limilabs support</dc:creator>
				<category><![CDATA[Component]]></category>
		<category><![CDATA[Gmail]]></category>
		<category><![CDATA[Mail.dll]]></category>
		<category><![CDATA[OAuth]]></category>

		<guid isPermaLink="false">http://www.limilabs.com/blog/?p=2636</guid>
		<description><![CDATA[Although neither POP3 nor IMAP protocol allows retrieving the list of users contacts, it is possible to use Google API for that. As long as you are using OAuth to access Gmail, Mail.dll email component allows you to easy download Gmail contacts of a particular user. // C# GmailOAuth oauth = new GmailOAuth(_consumerKey, _consumerSecret); List&#60;GoogleScope&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Although neither POP3 nor IMAP protocol allows retrieving the list of users contacts, it is possible to use Google API for that.</p>
<p>As long as you are using <a href="http://www.limilabs.com/blog/oauth-with-gmail">OAuth to access Gmail</a>, Mail.dll <a href="http://www.limilabs.com/mail">email component </a>allows you to easy download Gmail contacts of a particular user.</p>
<pre class="brush: csharp;">
// C#

GmailOAuth oauth = new GmailOAuth(_consumerKey, _consumerSecret);

List&lt;GoogleScope&gt; scopes = new List&lt;GoogleScope&gt;
    {
        GoogleScope.EmailAddressScope,
        GoogleScope.ContactsScope
    };

string authUrl = oauth.GetAuthorizationUrl(&quot;http://localhost:64119/&quot;, scopes);

// ASP.NET client:
// Save oauth in permanent storage:
// Cache[oauth.RequestToken.Token] = oauth;

// Windows client:
Process.Start(url);

// ASP.NET client:
// Response.Redirect(url);

// Windows client with url:
string rawReturnUrl = Console.ReadLine();
ReturnUrl returnUrl = new ReturnUrl(rawReturnUrl);
oauth.GetAccessToken(returnUrl.OAuthVerifier);

// Windows client with verification code (oob):
// string oauthVerifier = HttpUtility.UrlDecode(Console.ReadLine());
// oauth.GetAccessToken(oauthVerifier);

// ASP.NET client:
// ReturnUrl returnUrl = new ReturnUrl(Request.RawUrl);
// Retrieve oauth from permanent storage:
// GmailOAuth oauth = Cache[returnUrl.OAuthToken]
// oauth.GetAccessToken(returnUrl.OAuthVerifier);

GoogleApi google = new GoogleApi(oauth.ConsumerToken, oauth.AccessToken);

XmlDocument contacts = google.GetContacts();

XmlNamespaceManager nsmgr = new XmlNamespaceManager(contacts.NameTable);
nsmgr.AddNamespace(&quot;gd&quot;, &quot;http://schemas.google.com/g/2005&quot;);
nsmgr.AddNamespace(&quot;a&quot;, &quot;http://www.w3.org/2005/Atom&quot;);

foreach (XmlNode contact in contacts.GetElementsByTagName(&quot;entry&quot;))
{
    XmlNode title = contact.SelectSingleNode(&quot;a:title&quot;, nsmgr);
    XmlNode email = contact.SelectSingleNode(&quot;gd:email&quot;, nsmgr);

    Console.WriteLine(&quot;{0}: {1}&quot;,
        title.InnerText,
        email.Attributes[&quot;address&quot;].Value);
}

oauth.RevokeToken(oauth.AccessToken.Token);
</pre>
<pre class="brush: vb;">
' VB.NET

Dim oauth As New GmailOAuth(_consumerKey, _consumerSecret)

Dim scopes As New List(Of GoogleScope)() With { _
	GoogleScope.EmailAddressScope, _
	GoogleScope.ContactsScope _
}
Dim authUrl As String = oauth.GetAuthorizationUrl(&quot;http://localhost:64119/&quot;, scopes)

' ASP.NET client:
' Save oauth in permanent storage:
' Cache[oauth.RequestToken.Token] = oauth;

' Windows client:
Process.Start(url)

' ASP.NET client:
' Response.Redirect(url)

' Windows client with url:
Dim rawReturnUrl As String = Console.ReadLine()
Dim returnUrl As New ReturnUrl(rawReturnUrl)
oauth.GetAccessToken(returnUrl.OAuthVerifier)

' Windows client with verification code (oob):
' Dim oauthVerifier As String = HttpUtility.UrlDecode(Console.ReadLine())
' oauth.GetAccessToken(oauthVerifier)

' ASP.NET client:
' Dim returnUrl As New ReturnUrl(Request.RawUrl)
' Retrive oauth from permanent storage:
' Dim oauth As GmailOAuth = Cache(returnUrl.OAuthToken)
' oauth.GetAccessToken(returnUrl.OAuthVerifier)

Dim google As New GoogleApi(oauth.ConsumerToken, oauth.AccessToken)
Dim contacts As XmlDocument = google.GetContacts()

Dim nsmgr As New XmlNamespaceManager(contacts.NameTable)
nsmgr.AddNamespace(&quot;gd&quot;, &quot;http://schemas.google.com/g/2005&quot;)
nsmgr.AddNamespace(&quot;a&quot;, &quot;http://www.w3.org/2005/Atom&quot;)

For Each contact As XmlNode In contacts.GetElementsByTagName(&quot;entry&quot;)
	Dim title As XmlNode = contact.SelectSingleNode(&quot;a:title&quot;, nsmgr)
	Dim email As XmlNode = contact.SelectSingleNode(&quot;gd:email&quot;, nsmgr)
	Console.WriteLine(&quot;{0}: {1}&quot;, _
            title.InnerText, _
            email.Attributes(&quot;address&quot;).Value)
Next

oauth.RevokeToken(oauth.AccessToken.Token)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.limilabs.com/blog/get-google-contacts/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OAuth with Gmail</title>
		<link>http://www.limilabs.com/blog/oauth-with-gmail?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=oauth-with-gmail</link>
		<comments>http://www.limilabs.com/blog/oauth-with-gmail#comments</comments>
		<pubDate>Fri, 21 Oct 2011 17:27:39 +0000</pubDate>
		<dc:creator>Limilabs support</dc:creator>
				<category><![CDATA[Component]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Gmail]]></category>
		<category><![CDATA[IMAP]]></category>
		<category><![CDATA[Mail.dll]]></category>
		<category><![CDATA[OAuth]]></category>
		<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[XOAuth]]></category>

		<guid isPermaLink="false">http://www.limilabs.com/blog/?p=2030</guid>
		<description><![CDATA[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&#8217;ll show how to access Gmail account using 3-legged OAuth authentication method with Mail.dll IMAP component. The key advantage of this method is that it allows an application to access user [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.limilabs.com/blog/wp-content/uploads/2009/11/gmail.png" alt="" title="gmail" width="131" height="61" class="alignleft size-full wp-image-271" /></p>
<p><strong>OAuth </strong> is an open protocol to allow secure API authorization in a simple and standard method from desktop and web applications.</p>
<p>In this post<strong> I&#8217;ll show how to access Gmail</strong> account using 3-legged OAuth authentication method with <a href="http://www.limilabs.com/mail">Mail.dll IMAP component</a>. The key advantage of this method is that it allows an application to access user email <strong>without knowing user&#8217;s password.</strong></p>
<p>You can read more on OAuth authentication with Google accounts here:<br />
<a href="http://code.google.com/apis/accounts/docs/OAuth_ref.html" rel="nofollow">http://code.google.com/apis/accounts/docs/OAuth_ref.html</a></p>
<p>Gmail IMAP and SMTP using OAuth:<br />
<a href="http://code.google.com/apis/gmail/oauth/protocol.html" rel="nofollow">http://code.google.com/apis/gmail/oauth/protocol.html</a></p>
<p>If your application/website is not registered, you should use following key and secret:<br />
consumer key: &#8220;anonymous&#8221;<br />
consumer secret: &#8220;anonymous&#8221;</p>
<p>Remember to add reference to Mail.dll and appropriate namespaces.</p>
<pre class="brush: csharp;">
// C#

using Limilabs.Client.IMAP;
using Limilabs.Client.Authentication;
using Limilabs.Client.Authentication.Google;

const string userEmailAccount = &quot;pat@gmail.com&quot;;
const string consumerKey = &quot;anonymous&quot;;
const string consumerSecret = &quot;anonymous&quot;;

GmailOAuth oauth = new GmailOAuth(consumerKey, consumerSecret);

string url = oauth.GetAuthorizationUrl(&quot;http://localhost:64119/&quot;);

// ASP.NET client:
// Save oauth in permanent storage:
// Cache[oauth.RequestToken.Token] = oauth;

// Windows client:
Process.Start(url);

// ASP.NET client:
// Response.Redirect(url);

// Windows client with url:
string rawReturnUrl = Console.ReadLine();
ReturnUrl returnUrl = new ReturnUrl(rawReturnUrl);
oauth.GetAccessToken(returnUrl.OAuthVerifier);

// Windows client with verification code (oob):
// string oauthVerifier = HttpUtility.UrlDecode(Console.ReadLine());
// oauth.GetAccessToken(oauthVerifier);

// ASP.NET client:
// ReturnUrl returnUrl = new ReturnUrl(Request.RawUrl);
// Retrieve oauth from permanent storage:
// GmailOAuth oauth = Cache[returnUrl.OAuthToken]
// oauth.GetAccessToken(returnUrl.OAuthVerifier);

using (Imap client = new Imap())
{
    client.ConnectSSL(&quot;imap.gmail.com&quot;);
    string oauthImapKey = oauth.GetXOAuthKeyForImap();
    client.LoginOAUTH(oauthImapKey);

    // Now you can access user's emails
    //...

    client.Close();
    oauth.RevokeToken(oauthImapKey);
}
</pre>
<p>1.<br />
<strong>GmailOAuth.GetAuthorizationUrl</strong> method returns url you should redirect your user to, so he can authorize access.<br />
As you can see, Mail.dll is asking for access to user&#8217;s email information and Gmail access:</p>
<p><img src="http://www.limilabs.com/blog/wp-content/uploads/2011/10/GmailOAuth_Authorize.png" alt="" title="GmailOAuth_Authorize" width="536" height="537" class="aligncenter size-full wp-image-2043" /></p>
<p>2.<br />
If you<strong> don&#8217;t specify callback</strong> parameter, user will have to <strong>manually </strong>copy&#038;paste the token to your application (oob):</p>
<p><img src="http://www.limilabs.com/blog/wp-content/uploads/2011/10/GmailOAuth_OOBAuthorize.png" alt="" title="GmailOAuth_OOBAuthorize" width="536" height="350" class="aligncenter size-full wp-image-2044" /></p>
<p>In case of a <strong>web project</strong>, you can specify<strong> a web address on your website</strong>. oauth_verifier will be included as the redirection url parameter.</p>
<p>After the redirection, your website/application needs to<strong> read oauth_verifier query parameter</strong>:</p>
<p><img src="http://www.limilabs.com/blog/wp-content/uploads/2011/10/GmailOAuth_Redirect.png" alt="" title="GmailOAuth_Redirect" width="536" height="133" class="aligncenter size-full wp-image-2045" /></p>
<p>3.<br />
<strong>GmailOAuth.GetAccessToken</strong> method authorizes the token.</p>
<p>4.<br />
<strong>GmailOAuth.GetXOAuthKeyForImap</strong> method <strong>uses Google API to get the email address</strong> of the user, and generates XOAuth key for IMAP protocol (you can use GetXOAuthKeyForSmtp for SMTP).</p>
<p>5.<br />
<strong>GmailOAuth.RevokeToken</strong> method <strong>revokes XOAuth key</strong>, so no further access can be made with it.</p>
<p>&#8230;and finally VB.NET version of the code:</p>
<pre class="brush: vb;">
' VB.NET

Imports Limilabs.Client.IMAP
Imports Limilabs.Client.Authentication
Imports Limilabs.Client.Authentication.Google

Const  userEmailAccount As String = &quot;pat@gmail.com&quot;
Const  consumerKey As String = &quot;anonymous&quot;
Const  consumerSecret As String = &quot;anonymous&quot;

Dim oauth As New GmailOAuth(consumerKey, consumerSecret)

Dim url As String = oauth.GetAuthorizationUrl(&quot;http://localhost:64119/&quot;)

' ASP.NET client:
' Save oauth in permanent storage:
' Cache[oauth.RequestToken.Token] = oauth;

' Windows client:
Process.Start(url)

' ASP.NET client:
' Response.Redirect(url)

' Windows client with url:
Dim rawReturnUrl As String = Console.ReadLine()
Dim returnUrl As New ReturnUrl(rawReturnUrl)
oauth.GetAccessToken(returnUrl.OAuthVerifier)

' Windows client with verification code (oob):
' Dim oauthVerifier As String = HttpUtility.UrlDecode(Console.ReadLine())
' oauth.GetAccessToken(oauthVerifier)

' ASP.NET client:
' Dim returnUrl As New ReturnUrl(Request.RawUrl)
' Retrive oauth from permanent storage:
' Dim oauth As GmailOAuth = Cache(returnUrl.OAuthToken)
' oauth.GetAccessToken(returnUrl.OAuthVerifier)

Using client As New Imap()
	client.ConnectSSL(&quot;imap.gmail.com&quot;)
	Dim oauthImapKey As String = oauth.GetXOAuthKeyForImap()
	client.LoginOAUTH(oauthImapKey)

	' Now you can access user's emails
	'...

	client.Close()
	oauth.RevokeToken(oauthImapKey)
End Using
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.limilabs.com/blog/oauth-with-gmail/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>2-legged OAuth with Gmail</title>
		<link>http://www.limilabs.com/blog/2-legged-oauth-with-gmail?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=2-legged-oauth-with-gmail</link>
		<comments>http://www.limilabs.com/blog/2-legged-oauth-with-gmail#comments</comments>
		<pubDate>Fri, 21 Oct 2011 16:31:02 +0000</pubDate>
		<dc:creator>Limilabs support</dc:creator>
				<category><![CDATA[Component]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Gmail]]></category>
		<category><![CDATA[IMAP]]></category>
		<category><![CDATA[Mail.dll]]></category>
		<category><![CDATA[OAuth]]></category>
		<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[XOAuth]]></category>

		<guid isPermaLink="false">http://www.limilabs.com/blog/?p=2031</guid>
		<description><![CDATA[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&#8217;ll show how to access Gmail account using 2-legged OAuth authentication method. The basic idea is that domain administrator can use this method to access user email without knowing user&#8217;s password. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.limilabs.com/blog/wp-content/uploads/2009/11/gmail.png" alt="" title="gmail" width="131" height="61" class="alignleft size-full wp-image-271" /></p>
<p><strong>OAuth </strong> is an open protocol to allow secure API authorization in a simple and standard method from desktop and web applications.</p>
<p>In this post<strong> I&#8217;ll show how to access Gmail</strong> account using 2-legged OAuth authentication method. The basic idea is that domain administrator can use this method to access user email <strong>without knowing user&#8217;s password.</strong></p>
<p>You can read more on OAuth authentication with Google accounts here:<br />
<a href="http://code.google.com/apis/accounts/docs/OAuth_ref.html" rel="nofollow">http://code.google.com/apis/accounts/docs/OAuth_ref.html</a></p>
<p>Gmail IMAP and SMTP using OAuth:<br />
<a href="http://code.google.com/apis/gmail/oauth/protocol.html" rel="nofollow">http://code.google.com/apis/gmail/oauth/protocol.html</a></p>
<p>Remember to add reference to Mail.dll and appropriate namespaces.</p>
<pre class="brush: csharp;">
// C#

using Limilabs.Client.IMAP;
using Limilabs.Client.Authentication;
using Limilabs.Client.Authentication.Google;

const string consumerKey = &quot;example.com&quot;;
const string consumerSecret = &quot;secret&quot;;
const string email = &quot;pat@example.com&quot;;

Gmail2LeggedOAuth oauth = new Gmail2LeggedOAuth(
    consumerKey, consumerSecret);

using (Imap client = new Imap())
{
    client.ConnectSSL(&quot;imap.gmail.com&quot;);

    string oauthImapKey = oauth.GetXOAuthKeyForImap(email);

    client.LoginOAUTH(oauthImapKey);

    //...

    client.Close();
}
</pre>
<pre class="brush: vb;">
' VB.NET

Imports Limilabs.Client.IMAP
Imports Limilabs.Client.Authentication
Imports Limilabs.Client.Authentication.Google

Const  consumerKey As String = &quot;example.com&quot;
Const  consumerSecret As String = &quot;secret&quot;
Const  email As String = &quot;pat@example.com&quot;

Dim oauth As New Gmail2LeggedOAuth(consumerKey, consumerSecret)

Using client As New Imap()
	client.ConnectSSL(&quot;imap.gmail.com&quot;)

	Dim oauthImapKey As String = oauth.GetXOAuthKeyForImap(email)

	client.LoginOAUTH(oauthImapKey)

	'...

	client.Close()
End Using
</pre>
<p>Here are the google apps configuration screens:</p>
<p><a href="http://www.limilabs.com/blog/wp-content/uploads/2011/01/2LeggedOAuth1.png"><img src="http://www.limilabs.com/blog/wp-content/uploads/2011/01/2LeggedOAuth1-300x233.png" alt="" title="2LeggedOAuth1" width="300" height="233" class="aligncenter size-medium wp-image-1937" /></a></p>
<p><a href="http://www.limilabs.com/blog/wp-content/uploads/2011/01/2LeggedOAuth2.png"><img src="http://www.limilabs.com/blog/wp-content/uploads/2011/01/2LeggedOAuth2-300x233.png" alt="" title="2LeggedOAuth2" width="300" height="233" class="aligncenter size-medium wp-image-1938" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.limilabs.com/blog/2-legged-oauth-with-gmail/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>2-legged OAuth with IMAP</title>
		<link>http://www.limilabs.com/blog/2-legged-oauth-with-imap?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=2-legged-oauth-with-imap</link>
		<comments>http://www.limilabs.com/blog/2-legged-oauth-with-imap#comments</comments>
		<pubDate>Wed, 12 Jan 2011 20:09:42 +0000</pubDate>
		<dc:creator>Limilabs support</dc:creator>
				<category><![CDATA[Component]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[IMAP]]></category>
		<category><![CDATA[Mail.dll]]></category>
		<category><![CDATA[OAuth]]></category>
		<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[XOAuth]]></category>

		<guid isPermaLink="false">http://www.limilabs.com/blog/?p=1670</guid>
		<description><![CDATA[OAuth is an open protocol to allow secure API authorization in a simple and standard method from desktop and web applications. This article describes generic OAuth class, if you are using Gmail please read 2-legged OAuth authentication with Gmail Remember to add reference to Mail.dll and appropriate namespaces. // C# using Limilabs.Client.Authentication; using Limilabs.Client.IMAP; string [...]]]></description>
			<content:encoded><![CDATA[<p>  <strong>OAuth </strong> is an open protocol to allow secure API authorization in a simple and standard method from desktop and web applications.</p>
<p>This article describes generic OAuth class, <strong>if you are using Gmail please read <a href="http://www.limilabs.com/blog/2-legged-oauth-with-gmail">2-legged OAuth authentication with Gmail</a></strong></p>
<p>Remember to add reference to Mail.dll and appropriate namespaces.</p>
<pre class="brush: csharp;">
// C#

using Limilabs.Client.Authentication;
using Limilabs.Client.IMAP;

string consumerKey = &quot;your_domain.com&quot;;
string consumerSecret = &quot;your_oauth_consumer_secret&quot;;
string emailAccount = &quot;address@your_domain.com&quot;;

using (Imap client = new Imap())
{
    client.ConnectSSL(&quot;imap.gmail.com&quot;);

    string imapUrl = string.Format(
        &quot;https://mail.google.com/mail/b/{0}/imap/?xoauth_requestor_id={1}&quot;,
        emailAccount,
        HttpUtility.UrlEncode(emailAccount));

    string oauthImapKey = OAuth.ForUrl(imapUrl)
        .Consumer(consumerKey, consumerSecret)
        .SignatureMethod(SignatureType.HMACSHA1)
        .Sign()
        .GetXOAuthKey();

    client.LoginOAUTH(oauthImapKey);

    //...

    client.Close();
}
</pre>
<pre class="brush: vb;">
' VB.NET

Imports Limilabs.Client.Authentication
Imports Limilabs.Client.IMAP

Dim consumerKey As String = &quot;example.com&quot;
Dim consumerSecret As String = &quot;secret&quot;
Dim emailAccount As String = &quot;pat@example.com&quot;

Using client As New Imap()
    client.ConnectSSL(&quot;imap.gmail.com&quot;)

    Dim imapUrl As String = String.Format( _
        &quot;https://mail.google.com/mail/b/{0}/imap/?xoauth_requestor_id={1}&quot;, _
        emailAccount, _
        HttpUtility.UrlEncode(emailAccount))

    Dim oauthImapKey As String = OAuth.ForUrl(imapUrl) _
        .Consumer(consumerKey, consumerSecret) _
        .SignatureMethod(SignatureType.HMACSHA1) _
        .Sign() _
        .GetXOAuthKey()

    client.LoginOAUTH(oauthImapKey)

    '...

    client.Close()
End Using
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.limilabs.com/blog/2-legged-oauth-with-imap/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>OAuth with IMAP</title>
		<link>http://www.limilabs.com/blog/oauth-with-imap?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=oauth-with-imap</link>
		<comments>http://www.limilabs.com/blog/oauth-with-imap#comments</comments>
		<pubDate>Mon, 28 Jun 2010 19:44:43 +0000</pubDate>
		<dc:creator>Limilabs support</dc:creator>
				<category><![CDATA[Component]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[IMAP]]></category>
		<category><![CDATA[Mail.dll]]></category>
		<category><![CDATA[OAuth]]></category>
		<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[XOAuth]]></category>

		<guid isPermaLink="false">http://www.limilabs.com/blog/?p=936</guid>
		<description><![CDATA[OAuth is an open protocol to allow secure API authorization in a simple and standard method from desktop and web applications. This article describes generic OAuth class, if you are using Gmail please read OAuth authentication with Gmail The following code makes several HTTP requests to authenticate your application. It also fires up the web [...]]]></description>
			<content:encoded><![CDATA[<p>  <strong>OAuth </strong> is an open protocol to allow secure API authorization in a simple and standard method from desktop and web applications.</p>
<p>This article describes generic OAuth class, <strong>if you are using Gmail please read <a href="http://www.limilabs.com/blog/oauth-with-gmail">OAuth authentication with Gmail</a></strong></p>
<p>The following code makes <strong>several HTTP requests</strong> to authenticate your application. It also <strong>fires up the web browser, so the user can allow or deny</strong> the application to access his emails.</p>
<p>Remember to add reference to Mail.dll and appropriate namespaces.</p>
<p>1.</p>
<pre class="brush: csharp;">
// C#

using Limilabs.Client.Authentication;
using Limilabs.Client.IMAP;

const string userEmailAccount = &quot;pat@gmail.com&quot;;
const string consumerKey = &quot;anonymous&quot;;
const string consumerSecret = &quot;anonymous&quot;;

// Get request token
ParameterList parameters1 = OAuth.ForUrl(
        &quot;https://www.google.com/accounts/OAuthGetRequestToken&quot;)
    .Consumer(consumerKey, consumerSecret)
    .AddParameter(&quot;scope&quot;, &quot;https://mail.google.com/&quot;)
   .AddParameter(OAuthParameterName.OAuthCallback, &quot;oob&quot;)
    .Sign()
    .ExecuteWebRequest();

// Authorize token
string url2 = OAuth.ForUrl(
        &quot;https://www.google.com/accounts/OAuthAuthorizeToken&quot;)
   .Consumer(consumerKey, consumerSecret)
   .Token(parameters1.GetValue(OAuthParameterName.OAuthToken))
   .TokenSecret(parameters1.GetValue(OAuthParameterName.OAuthTokenSecret))
   .Sign()
   .GetUrl();

// Fire up the browser.
Process.Start(url2);
// You can use Response.Redirect(url) in ASP.NET
</pre>
<p>2.<br />
The user needs now to log-in to Gmail account (note that user does not enter credentials in your application):<br />
<a href="http://www.limilabs.com/blog/wp-content/uploads/2010/06/OAuth_Login.png"><img src="http://www.limilabs.com/blog/wp-content/uploads/2010/06/OAuth_Login.png" alt="" title="OAuth_Allow" width="558" height="576" class="aligncenter size-full wp-image-1193" /></a></p>
<p>3.<br />
Then he needs to allow your application to access Gmail:<br />
<a href="http://www.limilabs.com/blog/wp-content/uploads/2010/06/OAuth_Allow.png"><img src="http://www.limilabs.com/blog/wp-content/uploads/2010/06/OAuth_Allow.png" alt="" title="OAuth_Allow" width="558" height="576" class="aligncenter size-full wp-image-1193" /></a></p>
<p>4.<br />
If you<strong> don&#8217;t specify callback</strong> parameter, user will have to <strong>manually </strong>copy&#038;paste the token to your application:</p>
<p><img src="http://www.limilabs.com/blog/wp-content/uploads/2011/10/GmailOAuth_OOBAuthorize.png" alt="" title="GmailOAuth_OOBAuthorize" width="536" height="350" class="aligncenter size-full wp-image-2044" /></p>
<p>In case of a <strong>web project</strong>, instead of <strong>oob</strong> value as OAuthCallback parameter, you can specify<strong> a web address on your website</strong>. oauth_verifier will be included as the redirection url parameter.</p>
<p>After the redirection, your website/application needs to<strong> read oauth_verifier query parameter</strong>:</p>
<p><img src="http://www.limilabs.com/blog/wp-content/uploads/2011/10/GmailOAuth_Redirect.png" alt="" title="GmailOAuth_Redirect" width="536" height="133" class="aligncenter size-full wp-image-2045" /></p>
<p>5.</p>
<pre class="brush: csharp;">
// C#

Console.WriteLine(&quot;Please enter the key: &quot;);
string oauth_verifier = Console.ReadLine();
// You can use Request[&quot;oauth_verifier&quot;].ToString() in ASP.NET

// Get access token
ParameterList parameters3 = OAuth.ForUrl(
        &quot;https://www.google.com/accounts/OAuthGetAccessToken&quot;)
   .Consumer(consumerKey, consumerSecret)
   .Token(parameters1.GetValue(OAuthParameterName.OAuthToken))
   .TokenSecret(parameters1.GetValue(OAuthParameterName.OAuthTokenSecret))
   .AddParameter(&quot;oauth_verifier&quot;, oauth_verifier)
   .Sign()
   .ExecuteWebRequest();

// Log-in to IMAP server using XOAuth
using (Imap client = new Imap())
{
    client.ConnectSSL(&quot;imap.gmail.com&quot;);

    string imapUrl = string.Format(
        &quot;https://mail.google.com/mail/b/{0}/imap/&quot;, userEmailAccount);

    string oauthImapKey = OAuth.ForUrl(imapUrl)
        .Consumer(consumerKey, consumerSecret)
        .Token(parameters3.GetValue(OAuthParameterName.OAuthToken))
        .TokenSecret(parameters3.GetValue(OAuthParameterName.OAuthTokenSecret))
        .Sign()
        .GetXOAuthKey();

    client.LoginOAUTH(oauthImapKey);

    // Now you can access user's emails.
    //...

    client.Close();
}
</pre>
<p>Here&#8217;s the VB.NET version of the code samples:</p>
<p>Remember to add reference to Maill.dll and appropriate namespaces.</p>
<p>1.</p>
<pre class="brush: vb;">
' VB.NET

import Limilabs.Client.Authentication
import Limilabs.Client.IMAP

Const  userEmailAccount As String = &quot;alice@gmail.com&quot;
Const  consumerKey As String = &quot;anonymous&quot;
Const  consumerSecret As String = &quot;anonymous&quot;

' Gget request token
Dim parameters1 As ParameterList = OAuth _
	.ForUrl(&quot;https://www.google.com/accounts/OAuthGetRequestToken&quot;) _
	.Consumer(consumerKey, consumerSecret) _
	.AddParameter(&quot;scope&quot;, &quot;https://mail.google.com/&quot;) _
	.AddParameter(OAuthParameterName.OAuthCallback, &quot;oob&quot;) _
	.Sign() _
	.ExecuteWebRequest()

' Authorize token
Dim url2 As String = OAuth _
	.ForUrl(&quot;https://www.google.com/accounts/OAuthAuthorizeToken&quot;) _
	.Consumer(consumerKey, consumerSecret) _
	.Token(parameters1.GetValue(OAuthParameterName.OAuthToken)) _
	.TokenSecret(parameters1.GetValue(OAuthParameterName.OAuthTokenSecret)) _
	.Sign() _
	.GetUrl()

' Fire up the browser
Process.Start(url2)
' You can use Response.Redirect(url) in ASP.NET
</pre>
<p>2.<br />
First the user needs to log in to Gmail account (note that user does not enter credentials in your application):<br />
<a href="http://www.limilabs.com/blog/wp-content/uploads/2010/06/OAuth_Login.png"><img src="http://www.limilabs.com/blog/wp-content/uploads/2010/06/OAuth_Login.png" alt="" title="OAuth_Allow" width="558" height="576" class="aligncenter size-full wp-image-1193" /></a></p>
<p>3.<br />
Then he needs to allow your application to access Gmail:<br />
<a href="http://www.limilabs.com/blog/wp-content/uploads/2010/06/OAuth_Allow.png"><img src="http://www.limilabs.com/blog/wp-content/uploads/2010/06/OAuth_Allow.png" alt="" title="OAuth_Allow" width="558" height="576" class="aligncenter size-full wp-image-1193" /></a></p>
<p>4.<br />
If you<strong> don&#8217;t specify callback</strong> parameter, user will have to <strong>manually </strong>copy&#038;paste the token to your application:</p>
<p><img src="http://www.limilabs.com/blog/wp-content/uploads/2011/10/GmailOAuth_OOBAuthorize.png" alt="" title="GmailOAuth_OOBAuthorize" width="536" height="350" class="aligncenter size-full wp-image-2044" /></p>
<p>In case of a <strong>web project</strong>, instead of <strong>oob</strong> value as OAuthCallback parameter, you can specify<strong> a web address on your website</strong>. oauth_verifier will be included as the redirection url parameter.</p>
<p>After the redirection, your website/application needs to<strong> read oauth_verifier query parameter</strong>:</p>
<p><img src="http://www.limilabs.com/blog/wp-content/uploads/2011/10/GmailOAuth_Redirect.png" alt="" title="GmailOAuth_Redirect" width="536" height="133" class="aligncenter size-full wp-image-2045" /></p>
<p>5.</p>
<pre class="brush: vb;">
' VB.NET

Console.WriteLine(&quot;Please enter the key: &quot;)
Dim oauth_verifier As String = Console.ReadLine().Trim()
' You can use Request(&quot;oauth_verifier&quot;).ToString() in ASP.NET

' Third: get access token
Dim parameters3 As ParameterList = OAuth _
	.ForUrl(&quot;https://www.google.com/accounts/OAuthGetAccessToken&quot;) _
	.Consumer(consumerKey, consumerSecret) _
	.Token(parameters1.GetValue(OAuthParameterName.OAuthToken)) _
	.TokenSecret(parameters1.GetValue(OAuthParameterName.OAuthTokenSecret)) _
	.AddParameter(&quot;oauth_verifier&quot;, oauth_verifier) _
	.Sign() _
	.ExecuteWebRequest()

' Log-in to IMAP server using XOAuth
Using client As New Imap()
	client.ConnectSSL(&quot;imap.gmail.com&quot;)

	Dim imapUrl As String = String.Format(&quot;https://mail.google.com/mail/b/{0}/imap/&quot;, userEmailAccount)

	Dim oauthImapKey As String = OAuth.ForUrl(imapUrl) _
		.Consumer(consumerKey, consumerSecret) _
		.Token(parameters3.GetValue(OAuthParameterName.OAuthToken)) _
		.TokenSecret(parameters3.GetValue(OAuthParameterName.OAuthTokenSecret)) _
		.Sign() _
		.GetXOAuthKey()

	client.LoginOAUTH(oauthImapKey)

	' Now you can access user's emails.
        ' ...

	client.Close()
End Using
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.limilabs.com/blog/oauth-with-imap/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

