Working with Google Sheets API

Desbrina 1 Reputation point
2021-06-29T11:14:43.87+00:00

I'm trying to read a google sheet in my app. The sheet itself is public and i'm trying to access it using an API key rather than OAuth2 as theres no requirement to write to it.

Initially i'm just trying to get a list of all sheets available in that particular spreadsheet id

I have the below code, but it results in an error 400 The request URL is invalid.

class GoogleAPI
    {
        static string ApplicationName = "home-Timetable";
        private static readonly string API_KEY = "AIzaSyDHBvpqlZ5MM04ZrsuGRzpPp_WDI7mS5D0"; // Test Key
        static readonly string spreadsheetId = "19c3dGbBHhU7EtrCJBa7-bKMn7Jlg3R9xMXOP9HBZDPc"; // Test Sheet

        public static void GetSheets()
        {
            var sheetsService = new SheetsService(new BaseClientService.Initializer()
            {
                ApplicationName = ApplicationName,
                ApiKey = API_KEY,
                HttpClientFactory = new ProxySupportedHttpClientFactory(),
            });

            SpreadsheetsResource.GetRequest request = sheetsService.Spreadsheets.Get(spreadsheetId);
            Data.Spreadsheet response = request.Execute();
            Console.WriteLine(JsonConvert.SerializeObject(response));
        }
    }

The correct url for the sheet is: https://sheets.googleapis.com/v4/spreadsheets/19c3dGbBHhU7EtrCJBa7-bKMn7Jlg3R9xMXOP9HBZDPc?key=AIzaSyDHBvpqlZ5MM04ZrsuGRzpPp_WDI7mS5D0
This gives the expected response.

how do i successfully get the response expected?

Developer technologies ASP.NET Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,096 Reputation points
    2021-06-30T06:14:20.31+00:00

    Hi @Desbrina ,
    As far as I think,a Google Sheet Error 400 typically indicates that the credentials used in the integration are invalid. Try removing and re-adding the integration, updating the credentials if they have been changed at all for the Google Sheets account being used.
    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.