<?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>Lesnikowski Blog &#187; OAuth</title>
	<atom:link href="http://www.lesnikowski.com/blog/tag/oauth/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lesnikowski.com/blog</link>
	<description>EMAIL IMAP POP3 SMTP FTP FTPS barcode components blog</description>
	<lastBuildDate>Sun, 05 Feb 2012 14:10:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>OAuth with Gmail</title>
		<link>http://www.lesnikowski.com/blog/oauth-with-gmail/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=oauth-with-gmail</link>
		<comments>http://www.lesnikowski.com/blog/oauth-with-gmail/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 17:27:39 +0000</pubDate>
		<dc:creator>Pawel Lesnikowski</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.lesnikowski.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. The key advantage of this method is that it allows an application to access user email without knowing user&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.lesnikowski.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. 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">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">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 Maill.dll and appropriate namespaces.</p>
<pre class="brush: csharp;">
// C#

using Lesnikowski.Client.IMAP;
using Lesnikowski.Client.Authentication;
using Lesnikowski.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;);

Process.Start(url);
// You can use Response.Redirect(url) in ASP.NET

string oauthVerifier = HttpUtility.UrlDecode(Console.ReadLine());
// You can use Request[&quot;oauth_verifier&quot;].ToString() in ASP.NET

oauth.GetAccessToken(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.lesnikowski.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:</p>
<p><img src="http://www.lesnikowski.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.lesnikowski.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 Lesnikowski.Client.IMAP
Imports Lesnikowski.Client.Authentication
Imports Lesnikowski.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;)

Process.Start(url)
' You can use Response.Redirect(url) in ASP.NET

Dim oauthVerifier As String = HttpUtility.UrlDecode(Console.ReadLine())
' You can use Request[&quot;oauth_verifier&quot;].ToString() in ASP.NET

oauth.GetAccessToken(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.lesnikowski.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.lesnikowski.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.lesnikowski.com/blog/2-legged-oauth-with-gmail/#comments</comments>
		<pubDate>Fri, 21 Oct 2011 16:31:02 +0000</pubDate>
		<dc:creator>Pawel Lesnikowski</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.lesnikowski.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.lesnikowski.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">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">http://code.google.com/apis/gmail/oauth/protocol.html</a></p>
<p>Remember to add reference to Maill.dll and appropriate namespaces.</p>
<pre class="brush: csharp;">
// C#

using Lesnikowski.Client.IMAP;
using Lesnikowski.Client.Authentication;
using Lesnikowski.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(TestConstants.GmailImapServer);

    string oauthImapKey = oauth.GetXOAuthKeyForImap(email);

    client.LoginOAUTH(oauthImapKey);

    //...

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

Imports Lesnikowski.Client.IMAP
Imports Lesnikowski.Client.Authentication
Imports Lesnikowski.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(TestConstants.GmailImapServer)

	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.lesnikowski.com/blog/wp-content/uploads/2011/01/2LeggedOAuth1.png"><img src="http://www.lesnikowski.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.lesnikowski.com/blog/wp-content/uploads/2011/01/2LeggedOAuth2.png"><img src="http://www.lesnikowski.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.lesnikowski.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.lesnikowski.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.lesnikowski.com/blog/2-legged-oauth-with-imap/#comments</comments>
		<pubDate>Wed, 12 Jan 2011 20:09:42 +0000</pubDate>
		<dc:creator>Pawel Lesnikowski</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.lesnikowski.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 Maill.dll and appropriate namespaces. // C# using Lesnikowski.Client.Authentication; using Lesnikowski.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.lesnikowski.com/blog/2-legged-oauth-with-gmail/">2-legged OAuth authentication with Gmail</a></strong></p>
<p>Remember to add reference to Maill.dll and appropriate namespaces.</p>
<pre class="brush: csharp;">
// C#

using Lesnikowski.Client.Authentication;
using Lesnikowski.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 Lesnikowski.Client.Authentication
Imports Lesnikowski.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.lesnikowski.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.lesnikowski.com/blog/oauth-with-imap/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=oauth-with-imap</link>
		<comments>http://www.lesnikowski.com/blog/oauth-with-imap/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 19:44:43 +0000</pubDate>
		<dc:creator>Pawel Lesnikowski</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.lesnikowski.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.lesnikowski.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 Maill.dll and appropriate namespaces.</p>
<p>1.</p>
<pre class="brush: csharp;">
// C#

using Lesnikowski.Client.Authentication;
using Lesnikowski.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.lesnikowski.com/blog/wp-content/uploads/2010/06/OAuth_Login.png"><img src="http://www.lesnikowski.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.lesnikowski.com/blog/wp-content/uploads/2010/06/OAuth_Allow.png"><img src="http://www.lesnikowski.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.lesnikowski.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.lesnikowski.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(TestConstants.GmailImapServer);

    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 Lesnikowski.Client.Authentication
import Lesnikowski.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.lesnikowski.com/blog/wp-content/uploads/2010/06/OAuth_Login.png"><img src="http://www.lesnikowski.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.lesnikowski.com/blog/wp-content/uploads/2010/06/OAuth_Allow.png"><img src="http://www.lesnikowski.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.lesnikowski.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.lesnikowski.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(TestConstants.GmailImapServer)

	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.lesnikowski.com/blog/oauth-with-imap/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

