Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Linda Lawton LindaLawton

🎯
Focusing
View GitHub Profile
@LindaLawton
LindaLawton / GoogleAnalyticsOauth2
Last active Apr 15, 2019
How to connect to Google Analytics with Oauth2 and the .net client library
View GoogleAnalyticsOauth2
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 Jul 20, 2015
Connect to Google Analytics with a service account and the Google .net Client library
View GoogleAnalyitcsServiceAccount
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 Jul 20, 2015
Creates a Analytics service with either an OAuth" credential or a service account credentials
View GoogleAnalyticsService
var service = new AnalyticsService(new BaseClientService.Initializer() { HttpClientInitializer = credential,
ApplicationName = "Analytics API Sample",});
@LindaLawton
LindaLawton / AnalyticsServicePublic
Created Jul 20, 2015
Create a Google Analytics service using a Public API key
View AnalyticsServicePublic
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 Jul 21, 2015
How to access Google sheet with C# and a service account
View GoogleSheetsCsharp
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 Jul 22, 2015
OAuth2 Authentication to google Drive with C#
View GoogleDriveOauth2Csharp
//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 Jul 22, 2015
accessing google drive with a service account and C#
View GoogleDriveServiceAccountcsharp
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));
View DriveServicecsharp
var service = new DriveService(new BaseClientService.Initializer() {HttpClientInitializer = credential,
ApplicationName = "Drive API Sample",});
View DriveServicecsharpPublic
var services = new DriveService(new BaseClientService.Initializer(){
ApiKey = "[API key]", // from https://console.developers.google.com (Public API access)
ApplicationName = "Drive API Sample",
});
View Google Oauth email csharp
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;