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

后端 未结 5 2344
孤独总比滥情好
孤独总比滥情好 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:49

    The invalid credentials error is happening because the scopes you specified aren't actually getting sent with your credentials. I made the same mistake and only realized after I debugged and still saw 0 scopes on the credential after the CreateScoped call.

    A GoogleCredential is immutable so CreateScoped creates a new instance with the specified scopes set.

    Reassign your credentials variable with the scoped result like so and it should work:

      if (_cred.IsCreateScopedRequired) {
        _cred = _cred.CreateScoped(AnalyticsService.Scope.Analytics);
      }
    

    The accepted answer works because it's achieving the same thing in a more difficult way.

提交回复
热议问题