wingman007

GoogleApiServer2ServerConsole

Nov 30th, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Google.Apis.Auth.OAuth2;
  6. using System.Threading;
  7. using Google.Apis.Util.Store;
  8. using Google.Apis.Calendar.v3;
  9. using Google.Apis.Services;
  10. using System.IO;
  11. using System.Security.Cryptography.X509Certificates;
  12.  
  13. namespace GoogleApiServer2ServerConsole
  14. {
  15.     class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.             // we have followed http://www.daimto.com/google-calendar-api-authentication-with-c/
  20.             // you need to run PM> Install-Package Google.Apis.Calendar.v3
  21.             // add the file abtained from Google Developers Console
  22.             string serviceAccountEmail = "[email protected]";
  23.             string keyFilePath = @"C:\Users\Ani\Documents\visual studio 2015\Projects\GoogleApiServer2ServerConsole\GoogleApiServer2ServerConsole\Server2Server-3de7c7534a5d.json";
  24.  
  25.             // check the file exists
  26.             if (!File.Exists(keyFilePath))
  27.             {
  28.                 Console.WriteLine("An Error occurred - Key file does not exist");
  29.                 return;
  30.             }
  31.  
  32.             string[] scopes = new string[] {
  33.                  CalendarService.Scope.Calendar, // Manage your calendars
  34.                 CalendarService.Scope.CalendarReadonly // View your Calendars
  35.              };
  36.  
  37.             var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
  38.  
  39.             ServiceAccountCredential credential = new ServiceAccountCredential(
  40.                  new ServiceAccountCredential.Initializer(serviceAccountEmail)
  41.                  {
  42.                      Scopes = scopes
  43.                  }.FromCertificate(certificate));
  44.         }
  45.     }
  46. }
Add Comment
Please, Sign In to add comment