Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.IO;
- using System.Threading;
- using Google.Apis.Calendar.v3;
- using Google.Apis.Calendar.v3.Data;
- using Google.Apis.Auth.OAuth2;
- using Google.Apis.Auth.OAuth2.Flows;
- using Google.Apis.Auth.OAuth2.Web;
- using Google.Apis.Services;
- using Google.Apis.Util.Store;
- namespace GoogleCalendarV3
- {
- public partial class GoogleCalendarV3 : System.Web.UI.Page
- {
- CalendarService service;
- private const string ApplicationName = "DFW Install Calendar App";
- // Application logic should manage users authentication. This sample works with only one user. You can change
- // it by retrieving data from the session.
- private const string UserId = "user-id";
- static string gFolder = System.Web.HttpContext.Current.Server.MapPath("/App_Data/MyGoogleStorage");
- protected void Page_Load(object sender, EventArgs e)
- {
- Response.Write(gFolder);
- }
- public static GoogleClientSecrets GetClientConfiguration()
- {
- using (var stream = new FileStream(gFolder + @"\client_secrets.json", FileMode.Open, FileAccess.Read))
- {
- return GoogleClientSecrets.Load(stream);
- }
- }
- protected void btnAuthenticate_Click(object sender, EventArgs e)
- {
- IAuthorizationCodeFlow flow =
- new GoogleAuthorizationCodeFlow(
- new GoogleAuthorizationCodeFlow.Initializer
- {
- ClientSecrets = GetClientConfiguration().Secrets,
- DataStore = new FileDataStore(gFolder),
- Scopes = new[] { CalendarService.Scope.Calendar }
- });
- var uri = Request.Url.ToString();
- var code = Request["code"];
- if (code != null)
- {
- var token = flow.ExchangeCodeForTokenAsync(UserId, code,
- uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result;
- // Extract the right state.
- var oauthState = AuthWebUtility.ExtracRedirectFromState(
- flow.DataStore, UserId, Request["state"]).Result;
- Response.Redirect(oauthState);
- }
- else
- {
- var result = new AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId,
- CancellationToken.None).Result;
- if (result.RedirectUri != null)
- {
- // Redirect the user to the authorization server.
- Response.Redirect(result.RedirectUri);
- }
- else
- {
- // The data store contains the user credential, so the user has been already authenticated.
- service = new CalendarService(new BaseClientService.Initializer
- {
- ApplicationName = "Calendar API Sample",
- HttpClientInitializer = result.Credential
- });
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment