Bookmark and Share BitCoin Donate: 13t8gAWVpHP2ddxMp88d1NFpZjnFJC6UwK

Howto Server to Server integration with Google Sheets API

using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;

.....

public void Test(){ 
            //Create service account 
            //Download service account json
            //Add to app
            //Share sheet with service account id
            //Get sheet ID
            string[] Scopes = {
             SheetsService.Scope.SpreadsheetsReadonly
            };
            string ApplicationName = "Application Name";

            GoogleCredential credential = GoogleCredential.FromStream(new FileStream("APPCREDENTIALS.json",
             FileMode.Open, FileAccess.Read)).CreateScoped(Scopes);

            var service = new SheetsService(new BaseClientService.Initializer() {
             HttpClientInitializer = credential,
              ApplicationName = ApplicationName
            });


            string spreadSheetId = "10UpfCOrgtz282OfpnjkVQiwciHJO4BQwpsIl3Uby-Vw";
            string range = "A1:B4";

            SpreadsheetsResource.ValuesResource.GetRequest request = service.Spreadsheets.Values.Get(spreadSheetId, range);

            ValueRange response = request.Execute();
            IList < IList < Object >> values = response.Values;
            if (values != null && values.Count > 0) {
             foreach(var row in values) {
              Console.WriteLine("{0}, {1}", row[0], row[1]);
             }
            } else {
             Console.WriteLine("No data found.");
            }
}