google-api-dotnet-client icon indicating copy to clipboard operation
google-api-dotnet-client copied to clipboard

Error in updating rows Google Sheets API

Open bezobraz1e opened this issue 10 years ago • 5 comments

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]]

`

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

        var range = "Sheet1!A1:D5";
        ValueRange VRange = new ValueRange();
        VRange.Range = range;
        VRange.Values = list;
        VRange.MajorDimension = "ROWS";


        SpreadsheetsResource.ValuesResource.UpdateRequest upd = service.Spreadsheets.Values.Update(VRange, spreadsheetId, range);
        upd.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.RAW;
        var response = upd.Execute();

    Console.WriteLine(response.UpdatedRows);`

bezobraz1e avatar Jul 22 '16 15:07 bezobraz1e

So what is Scopes here? Please note that we (the people likely to read this issue) are not Sheets API experts, so much as experts in the code generation for the .NET client libraries. If this ends up in the subtleties of which scopes are required, you may end up being better off asking in a Sheets-specific community such as https://developers.google.com/google-apps/spreadsheets/

jskeet avatar Jul 22 '16 15:07 jskeet

Not an issue with the client.

@bezobraz1e make sure you use the 'SheetsService.Scope.Spreadsheets' scope instead of 'SheetsService.Scope.SpreadsheetsReadOnly'. If you've already authenticated with the read only scope, you have to go to the folder where you stored the token and delete it to re-authenticate in the new scope (in your case "Documents/.credentials/sheets.googleapis.com-dotnet-quickstart.json").

DrewJordan avatar Aug 08 '16 14:08 DrewJordan

This should be closed.

steveoh avatar Sep 01 '16 17:09 steveoh

Closing - but @bezobraz1e, if you still have problems (and you think it is a .NET API issue), feel free to ping me to reopen.

jskeet avatar Sep 01 '16 17:09 jskeet

Please remove FileDataStore .

credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None //, //new FileDataStore(credPath, true) ).Result;

just do and then its working .

SapanModi avatar May 31 '18 07:05 SapanModi