Last active
September 16, 2015 17:47
-
-
Save LindaLawton/3b01f1c044bb364c7313 to your computer and use it in GitHub Desktop.
WebMaster Tools API authentcation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Authenticate to Google Using Oauth2 | |
/// Documentation https://developers.google.com/accounts/docs/OAuth2 | |
/// </summary> | |
/// <param name="clientId">From Google Developer console https://console.developers.google.com</param> | |
/// <param name="clientSecret">From Google Developer console https://console.developers.google.com</param> | |
/// <param name="userName">A string used to identify a user.</param> | |
/// <returns></returns> | |
public static WebmastersService AuthenticateOauth(string clientId, string clientSecret, string userName) | |
{ | |
string[] scopes = new string[] { WebmastersService.Scope.Webmasters}; // View analytics data | |
try | |
{ | |
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData% | |
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret } | |
, scopes | |
, userName | |
, CancellationToken.None | |
, new FileDataStore("Daimto.GoogleWebMasters.Auth.Store")).Result; | |
WebmastersService service = new WebmastersService(new BaseClientService.Initializer() | |
{ | |
HttpClientInitializer = credential, | |
ApplicationName = "WebMasters API Sample", | |
}); | |
return service; | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.InnerException); | |
return null; | |
} | |
} | |
/// <summary> | |
/// Authenticating to Google using a Service account | |
/// Documentation: https://developers.google.com/accounts/docs/OAuth2#serviceaccount | |
/// </summary> | |
/// <param name="serviceAccountEmail">From Google Developer console https://console.developers.google.com</param> | |
/// <param name="keyFilePath">Location of the Service account key file downloaded from Google Developer console https://console.developers.google.com</param> | |
/// <returns></returns> | |
public static WebmastersService AuthenticateServiceAccount(string serviceAccountEmail, string keyFilePath) | |
{ | |
// check the file exists | |
if (!File.Exists(keyFilePath)) | |
{ | |
Console.WriteLine("An Error occurred - Key file does not exist"); | |
return null; | |
} | |
string[] scopes = new string[] { WebmastersService.Scope.Webmasters}; // View WebMasters data | |
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable); | |
try | |
{ | |
ServiceAccountCredential credential = new ServiceAccountCredential( | |
new ServiceAccountCredential.Initializer(serviceAccountEmail) | |
{ | |
Scopes = scopes | |
}.FromCertificate(certificate)); | |
// Create the service. | |
WebmastersService service = new WebmastersService(new BaseClientService.Initializer() | |
{ | |
HttpClientInitializer = credential, | |
ApplicationName = "WebMasters API Sample", | |
}); | |
return service; | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.InnerException); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment