How to login to Google API with Service Account in C# - Invalid Credentials

后端 未结 5 2349
孤独总比滥情好
孤独总比滥情好 2021-02-02 08:58

I\'m beating myself bloody trying to get a simple service acccount login to work in C#, to Google API and Google Analytics. My company is already getting data into Analytics, an

5条回答
  •  旧巷少年郎
    2021-02-02 09:25

    Another option is to use GoogleCredential.GetApplicationDefault(). I believe this is the currently (Oct. 2018) recommended approach. Here's some F#, but it's more or less the same in C# modulo syntax:

    let projectId = ""
    let creds =
      GoogleCredential.GetApplicationDefault()
        .CreateScoped(["https://www.googleapis.com/auth/cloud-platform"])
    use service =
      new CloudBuildService(
        BaseClientService.Initializer(HttpClientInitializer=creds))
    let foo = service.Projects.Builds.List(projectId).Execute()
    

    Now, just make sure that you set the GOOGLE_APPLICATION_CREDENTIALS to point to the file with the credentials JSON file, eg. GOOGLE_APPLICATION_CREDENTIALS=creds.json dotnet run.

提交回复
热议问题