hubert17

Google Calendar v3 Authentication (WORKING!)

Nov 21st, 2014
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. using System.IO;
  9. using System.Threading;
  10.  
  11. using Google.Apis.Calendar.v3;
  12. using Google.Apis.Calendar.v3.Data;
  13. using Google.Apis.Auth.OAuth2;
  14. using Google.Apis.Auth.OAuth2.Flows;
  15. using Google.Apis.Auth.OAuth2.Web;
  16. using Google.Apis.Services;
  17. using Google.Apis.Util.Store;
  18.  
  19. namespace GoogleCalendarV3
  20. {
  21.     public partial class GoogleCalendarV3 : System.Web.UI.Page
  22.     {
  23.         CalendarService service;
  24.         private const string ApplicationName = "DFW Install Calendar App";
  25.  
  26.         // Application logic should manage users authentication. This sample works with only one user. You can change
  27.         // it by retrieving data from the session.
  28.         private const string UserId = "user-id";
  29.         static string gFolder = System.Web.HttpContext.Current.Server.MapPath("/App_Data/MyGoogleStorage");
  30.  
  31.         protected void Page_Load(object sender, EventArgs e)
  32.         {
  33.             Response.Write(gFolder);
  34.         }
  35.  
  36.         public static GoogleClientSecrets GetClientConfiguration()
  37.         {
  38.             using (var stream = new FileStream(gFolder + @"\client_secrets.json", FileMode.Open, FileAccess.Read))
  39.             {
  40.                 return GoogleClientSecrets.Load(stream);
  41.             }
  42.         }
  43.  
  44.         protected void btnAuthenticate_Click(object sender, EventArgs e)
  45.         {
  46.             IAuthorizationCodeFlow flow =
  47.                new GoogleAuthorizationCodeFlow(
  48.                    new GoogleAuthorizationCodeFlow.Initializer
  49.                    {
  50.                        ClientSecrets = GetClientConfiguration().Secrets,
  51.                        DataStore = new FileDataStore(gFolder),
  52.                        Scopes = new[] { CalendarService.Scope.Calendar }
  53.                    });
  54.  
  55.             var uri = Request.Url.ToString();
  56.             var code = Request["code"];
  57.             if (code != null)
  58.             {
  59.                 var token = flow.ExchangeCodeForTokenAsync(UserId, code,
  60.                     uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result;
  61.  
  62.                 // Extract the right state.
  63.                 var oauthState = AuthWebUtility.ExtracRedirectFromState(
  64.                     flow.DataStore, UserId, Request["state"]).Result;
  65.                 Response.Redirect(oauthState);
  66.             }
  67.             else
  68.             {
  69.                 var result = new AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(UserId,
  70.                     CancellationToken.None).Result;
  71.                 if (result.RedirectUri != null)
  72.                 {
  73.                     // Redirect the user to the authorization server.
  74.                     Response.Redirect(result.RedirectUri);
  75.                 }
  76.                 else
  77.                 {
  78.                     // The data store contains the user credential, so the user has been already authenticated.
  79.                     service = new CalendarService(new BaseClientService.Initializer
  80.                     {
  81.                         ApplicationName = "Calendar API Sample",
  82.                         HttpClientInitializer = result.Credential
  83.                     });
  84.                 }
  85.             }
  86.         }
  87.     }
  88. }
Add Comment
Please, Sign In to add comment