Skip to content

Instantly share code, notes, and snippets.

View LindaLawton's full-sized avatar
🎯
Focusing

Linda Lawton LindaLawton

🎯
Focusing
View GitHub Profile
@LindaLawton
LindaLawton / GoogleAnalyticsOauth2
Last active April 15, 2019 09:08
How to connect to Google Analytics with Oauth2 and the .net client library
string[] scopes = new string[] {
AnalyticsService.Scope.Analytics, // view and manage your Google Analytics data
AnalyticsService.Scope.AnalyticsEdit, // Edit and manage Google Analytics Account
AnalyticsService.Scope.AnalyticsManageUsers, // Edit and manage Google Analytics Users
AnalyticsService.Scope.AnalyticsReadonly}; // View Google Analytics Data
var clientId = "[Client ID]"; // From https://console.developers.google.com
var clientSecret = "xxx"; // From https://console.developers.google.com
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId,
@LindaLawton
LindaLawton / GoogleAnalyitcsServiceAccount
Created July 20, 2015 09:49
Connect to Google Analytics with a service account and the Google .net Client library
string[] scopes = new string[] {AnalyticsService.Scope.Analytics}; // view and manage your Google Analytics data
var keyFilePath = @"c:\file.p12" ; // Downloaded from https://console.developers.google.com
var serviceAccountEmail = "xx@developer.gserviceaccount.com"; // found https://console.developers.google.com
//loading the Key file
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccountEmail) {
Scopes = scopes}.FromCertificate(certificate));
@LindaLawton
LindaLawton / GoogleAnalyticsService
Created July 20, 2015 10:23
Creates a Analytics service with either an OAuth" credential or a service account credentials
var service = new AnalyticsService(new BaseClientService.Initializer() { HttpClientInitializer = credential,
ApplicationName = "Analytics API Sample",});
@LindaLawton
LindaLawton / AnalyticsServicePublic
Created July 20, 2015 10:26
Create a Google Analytics service using a Public API key
AnalyticsService services = new AnalyticsService(new BaseClientService.Initializer()
{
ApiKey = "[API key]", // from https://console.developers.google.com (Public API access)
ApplicationName = "Analytics API Sample",
});
@LindaLawton
LindaLawton / GoogleSheetsCsharp
Created July 21, 2015 12:28
How to access Google sheet with C# and a service account
var certificate = new X509Certificate2(@"c:\Diamto Test Everything Project.p12", "notasecret", X509KeyStorageFlags.Exportable);
const string user = "XXX@developer.gserviceaccount.com";
var serviceAccountCredentialInitializer = new ServiceAccountCredential.Initializer(user) {Scopes = new[] { "https://spreadsheets.google.com/feeds" }
}.FromCertificate(certificate);
var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer);
if (!credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result)
@LindaLawton
LindaLawton / GoogleDriveOauth2Csharp
Created July 22, 2015 07:15
OAuth2 Authentication to google Drive with C#
//Scopes for use with the Google Drive API
string[] scopes = new string[] { DriveService.Scope.Drive,
DriveService.Scope.DriveFile};
var clientId = "[Client ID]"; // From https://console.developers.google.com
var clientSecret = "xxx"; // From https://console.developers.google.com
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId,
ClientSecret = clientSecret},
scopes,
Environment.UserName,
@LindaLawton
LindaLawton / GoogleDriveServiceAccountcsharp
Created July 22, 2015 07:22
accessing google drive with a service account and C#
string[] scopes = new string[] {DriveService.Scope.Drive}; // Full access
var keyFilePath = @"c:\file.p12" ; // Downloaded from https://console.developers.google.com
var serviceAccountEmail = "xx@developer.gserviceaccount.com"; // found https://console.developers.google.com
//loading the Key file
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccountEmail) {
Scopes = scopes}.FromCertificate(certificate));
var service = new DriveService(new BaseClientService.Initializer() {HttpClientInitializer = credential,
ApplicationName = "Drive API Sample",});
var services = new DriveService(new BaseClientService.Initializer(){
ApiKey = "[API key]", // from https://console.developers.google.com (Public API access)
ApplicationName = "Drive API Sample",
});
@LindaLawton
LindaLawton / Google Oauth email csharp
Created August 12, 2015 13:00
Google oauth2 email.
string _client_id = "[From Google Developer Console]";
string _client_secret = "[From Google Developer Console]";
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = _client_id, ClientSecret = _client_secret },
new string[] {"email" },
Environment.UserName ,
CancellationToken.None,
new FileDataStore("Daimto.GooglePlus.Auth.Store")).Result;