0
votes

I try to update rows in Google Sheets, but have error 403

Request had insufficient authentication scopes. [403] Errors [Message[Request had insufficient authentication scopes.] Location[ - ] Reason[forbidden] Domain[global]]

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,
        });

        List<object> list1 = new List<object>() { "Item", "Cost", "Stocked", "Ship Date" };
        List<object> list2 = new List<object>() { "Wheel", "$20.50", "4", "3/1/2016" };
        List<object> list3 = new List<object>() { "Door", "$15", "2", "3/15/2016" };
        List<object> list4 = new List<object>() { "Engine", "$100", "1", "30/20/2016" };
        List<object> list5 = new List<object>() { "Totals", "=SUM(B2:B4)", "=SUM(C2:C4)", "=MAX(D2:D4)" };
        IList<IList<Object>> list = new List<IList<Object>>() { list1, list2, list3, list4, list5 };

        ValueRange VRange = new ValueRange();
        VRange.Range = range;
        VRange.Values = list;


        SpreadsheetsResource.ValuesResource.UpdateRequest upd = service.Spreadsheets.Values.Update(VRange, spreadsheetId, range);
        UpdateValuesResponse response = upd.Execute();
        Console.WriteLine(response.UpdatedRows);
2

2 Answers

2
votes

Google caches the validation so once you approve the client via the browser pop up, it won't appear again. However, when you change the scopes it doesn't check, it just continues to use the already approved (stored) credential, whatever those previous permissions may be.

The sample code does mention this in a comment:

If modifying these scopes, delete your previously saved credentials at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json

Since you're writing to System.Environment.SpecialFolder.Personal, it'll show up in your /Documents folder, under .credentials/sheets.googleapis.com-dotnet-quickstart.json per the Path.Combine. Delete that file (or the folder if you like) and make sure to use static string[] Scopes = { SheetsService.Scope.Spreadsheets };, it will prompt you again now and you should be all set.

0
votes

I can't see the line where you define Scopes in your code, but make sure you're not requesting the READONLY scope