title: How to login to Google API with Service Account in C# - Invalid Credentials
body: <p>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, and I can query information with their Query Explorer, but getting started in .Net is not going anywhere. I am using a Google-generated  json file with PKI, as the documentation says that such a service account is the proper way for computer-to-computer communication with Googla API. Code snipet:</p>
  
  <pre><code>public static GoogleCredential _cred;
  public static string _exePath;
  
  static void Main(string[] args) {
      _exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Replace(@"file:\", "");
      var t = Task.Run(() =&gt; Run());
      t.Wait();
  }
  
  private static async Task Run() {
      try {
          // Get active credential
          using (var stream = new FileStream(_exePath + "\\Default-GASvcAcct-508d097b0bff.json", FileMode.Open, FileAccess.Read)) {
              _cred = GoogleCredential.FromStream(stream);
          }
          if (_cred.IsCreateScopedRequired) {
          _cred.CreateScoped(new string[] { AnalyticsService.Scope.Analytics });
          }
          // Create the service
          AnalyticsService service = new AnalyticsService(
              new BaseClientService.Initializer() {
                  HttpClientInitializer = _cred,
              });
          var act1 = service.Management.Accounts.List().Execute(); // blows-up here
  </code></pre>
  
  <p>It all compiles fine, but when it hit the Execute() statement, a <code>GoogleApiException</code> error is thrown:</p>
  
  <blockquote>
    <p>[Invalid Credentials] Location[Authorization - header] Reason[authError] Domain[global]</p>
  </blockquote>
  
  <p><strong><em>What am I missing?</em></strong></p>