-1

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.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
JBoothUA
  • 3,040
  • 3
  • 17
  • 46

2 Answers2

2

GoogleWebAuthorizationBroker.AuthorizeAsync is designed for use with installed application. It spawns the consent screen on the browser of the machine the code is running on. If you try to deploy this is going to try to open the consent screen on the server which wont work.

service account

This is in response to the other answer on this question.

If you are going to use a service account i recommend using the .json key file rather than the p12 as google is in the process of doing away with the p12 file

GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
{
     credential = GoogleCredential.FromStream(stream).CreateScoped(scopes);
                }

                
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • thank you so much, could you point me in the right direction for calling their API from a C# dotnet core application, I cannot seem to get authenticated. – JBoothUA Jul 25 '20 at 14:00
  • I wish i could i have been trying to get it to work with asp .net core for a while now https://stackoverflow.com/q/62791294/1841839 – Linda Lawton - DaImTo Jul 25 '20 at 14:36
1

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 = "matrixtools-argos@matrixtools-argos.iam.gserviceaccount.com";

 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)
 );
pjpscriv
  • 866
  • 11
  • 20
JBoothUA
  • 3,040
  • 3
  • 17
  • 46