title: Unable to access google sheets using api key
body: <p>Unable to access google sheets using v4 .net client library specifically using update and append methods.</p>
  
  <p>get methods works fine, i'm able to read values from the sheet.
  but when i try to update or append i am getting as</p>
  
  <p><code>Google.GoogleApiException: 'Google.Apis.Requests.RequestError
  Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. [401]
  Errors [
      Message[Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.] Location[ - ] Reason[unauthorized] Domain[global]
  ]
  '
  </code></p>
  
  <p>i'm running both the code using same service initialization yet i get the error.</p>
  
  <p>here is the code </p>
  
  <pre><code>using Google.Apis.Auth.OAuth2;
  using Google.Apis.Sheets.v4;
  using Google.Apis.Sheets.v4.Data;
  using Google.Apis.Services;
  using Google.Apis.Util.Store;
  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Text;
  using System.Threading;
  using System.Threading.Tasks;
  
  namespace SheetsQuickstart
  {
      class Program
      {
          // If modifying these scopes, delete your previously saved credentials
      // at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json
      static string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };
      static string ApplicationName = "Google Sheets API .NET Quickstart";
      static String spreadsheetId = "{{SpreadSheet_ID}}";
      static void Main(string[] args)
      {
          /*
          UserCredential credential;
  
          using (var stream =
              new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
          {
              string credPath = System.Environment.GetFolderPath(
                  System.Environment.SpecialFolder.Personal);
              credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");
  
              credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                  GoogleClientSecrets.Load(stream).Secrets,
                  Scopes,
                  "user",
                  CancellationToken.None,
                  new FileDataStore(credPath, true)).Result;
              Console.WriteLine("Credential file saved to: " + credPath);
          }
          */
          // Create Google Sheets API service.
          var service = new SheetsService(new BaseClientService.Initializer()
          {
              //HttpClientInitializer = credential,
              ApplicationName = ApplicationName,
              ApiKey = "{{API_KEY}}",
          }
          );
  
  
  
          // Define request parameters.
  
          String range = "A2:E";
          //get request / for reading
          SpreadsheetsResource.ValuesResource.GetRequest request = service.Spreadsheets.Values.Get(spreadsheetId, range);
          IList&lt;IList&lt;Object&gt;&gt; val = GenerateData();
          string newRange = GetRange(service);
  
          //append request 
          SpreadsheetsResource.ValuesResource.AppendRequest arequest = service.Spreadsheets.Values.Append(new ValueRange() { Values = val }, spreadsheetId, newRange);
  
          ValueRange response = request.Execute();
          IList&lt;IList&lt;Object&gt;&gt; values = response.Values;
          if (values != null &amp;&amp; values.Count &gt; 0)
          {
              foreach (var row in values)
              {
                  // Print columns A to E, which correspond to indices 0 to 4.
                  Console.WriteLine("{0}, {1}, {2}, {3}, {4}", row[0], row[1], row[2], row[3], row[4]);
              }
          }
          else
          {
              Console.WriteLine("No data found.");
          }
  
  
          Console.Read();
  
          arequest.InsertDataOption = SpreadsheetsResource.ValuesResource.AppendRequest.InsertDataOptionEnum.INSERTROWS;
          arequest.ValueInputOption = SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum.USERENTERED;
  
          /*
          *
          * Works till here
          *
          */
  
          var aresponse = arequest.Execute();
  
          Console.WriteLine("Inserted");
          Console.Read();
      }
  
      private static IList&lt;IList&lt;Object&gt;&gt; GenerateData()
      {
          List&lt;IList&lt;Object&gt;&gt; objNewRecords = new List&lt;IList&lt;Object&gt;&gt;();
  
          IList&lt;Object&gt; obj = new List&lt;Object&gt;();
  
          obj.Add("Column - 1");
          obj.Add("Column - 2");
          obj.Add("Column - 3");
          obj.Add("Column - 4");
          obj.Add("Column - 5");
          objNewRecords.Add(obj);
  
          return objNewRecords;
      }
  
      protected static string GetRange(SheetsService service)
      {
          // Define request parameters.
          // String spreadsheetId = ;
          String range = "A:E";
  
          SpreadsheetsResource.ValuesResource.GetRequest getRequest =
                     service.Spreadsheets.Values.Get(spreadsheetId, range);
  
          ValueRange getResponse = getRequest.Execute();
          IList&lt;IList&lt;Object&gt;&gt; getValues = getResponse.Values;
  
          int currentCount = getValues.Count() + 2;
  
          String newRange = "A" + currentCount + ":A";
  
          return newRange;
      }
  }
  }
  </code></pre>