Assaf Stern
Assaf Stern

Reputation: 11

How to accesses Gmail with a web app using OAuth and .Net

I’m trying to write a simple web application using ASO .Net and C# which shows a user a simple web page with a single button. By clicking on the button the user authenticates his Gmail account using OAuth2 and then the page list him with his unread emails. For the authentication part I’m using the following code:

OAuth2Parameters parameters = new OAuth2Parameters() 
{
   ClientId = "MY CLIENT ID AS RECEIVED FROM GOOGLE",
   ClientSecret = "MT CLIENT CECRET AS RECEIVED FROM GOOGLE",
   RedirectUri = "http://localhost:16615/GoogleOAuthCallback.aspx",
   Scope = "https://www.googleapis.com/auth/gmail.readonly",
   State = "email1",                
   AccessType = "offline"
};

parameters.AccessCode = Request.QueryString["code"];
Google.GData.Client.OAuthUtil.GetAccessToken(parameters);
GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory(null, " Web client 1", parameters);
Google.Apis.Auth.OAuth2.Responses.TokenResponse token = new Google.Apis.Auth.OAuth2.Responses.TokenRespons
{
     RefreshToken = "??????????????????",
};

UserCredential credential = new UserCredential(new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
       ClientSecrets = new ClientSecrets
       {
          ClientId = "MY CLIENT ID FROM GOOGLE",
          ClientSecret = "MY SECRET FROM GOOGLE",
       },
 }), "user", token);


// Create Gmail API service.
GmailService service = new GmailService(new BaseClientService.Initializer()
{
       HttpClientInitializer = credential,
       ApplicationName = “Web client 1”,
});

I have no idea how to get the refresh token and if this is the right way to write the code for my app. Any help will be highly appreciated!

Upvotes: 1

Views: 865

Answers (1)

Mr.Rebot
Mr.Rebot

Reputation: 6791

If you take a look at the OAuth 2.0 document under the API Client Library for .NET, you'll see:

User credentials

UserCredential is a thread-safe helper class for using an access token to access protected resources. An access token typically expires after 1 hour, after which you will get an error if you try to use it.

UserCredential and AuthorizationCodeFlow take care of automatically "refreshing" the token, which simply means getting a new access token. This is done using a long-lived refresh token, which you receive along with the access token if you use the access_type=offline parameter during the authorization code flow.

In most applications, it is advisable to store the credential's access token and refresh token in persistent storage. Otherwise, you will need to present the end user with an authorization page in the browser every hour, because the access token expires an hour after you've received it.

To make sure the access and refresh tokens persist, you can provide your own implementation of IDataStore, or you can use one of the following implementations provided by the library:

  • FileDataStore for .NET ensures that the credential will be persistent in a file.
  • StorageDataStore for Windows and Widows Phone ensures that the credential will be persistent using Windows StorageFolder.

For further understanding the flow here is the Google's repository to check the codes on OAuth handling. I hope this helps. :)

Upvotes: 1

Related Questions