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:
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);
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.