Click here to Skip to main content
15,867,780 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey everyone,
I’m trying to write a simple web application using .Net 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 lists him with his unread emails.
This is my code:
C#
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 = "???WHER DO I GET IT FROM???",
       };

UserCredential credential = new UserCredential(new GoogleAuthorizationCodeFlow(new  
      GoogleAuthorizationCodeFlow.Initializer
      {
              ClientSecrets = new ClientSecrets
              {
                     ClientId = "MY CLIENT ID AS RECEIVED FROM GOOGLE",
                     ClientSecret = "MY SECRET AS RECEIVED 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 development approuch. Any help will be highly appreciated!

What I have tried:

reading Google developer community - Gmail APIs and searching for sample code.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900