Search code examples
c#google-apigoogle-api-dotnet-clientgoogle-search-api

OAuth issues with Google Console API


I was hoping someone could help me get auth'd into the google API.
here is the code i'm trying to use,

   var credentials = await GoogleWebAuthorizationBroker.AuthorizeAsync(secrets, new string[] { WebmastersService.Scope.Webmasters }, "user", CancellationToken.None);

if I try it locally I get an (expected) cannot connect from this localhost/authorize url. If I try it from the deployed site, it times out and gives a 502

Any suggestions? Thanks in advance

I have had success from the OAuth2 playground, but not sure what I'm missing here.


Solution

  • After playing with that for so long, I finally got it working with the following code: Thank you so much for helping me understand what was going on.

     var serviceAccountEmail = "[email protected]";
    
     var certificate = new X509Certificate2(@"C:\Users\key.p12", "notasecret", X509KeyStorageFlags.Exportable);
    
     var credentials = new ServiceAccountCredential(
          new ServiceAccountCredential.Initializer(serviceAccountEmail) {
               Scopes = new[] { WebmastersService.Scope.WebmastersReadonly }
          }.FromCertificate(certificate)
     );