Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone,
I need to gain access to a google sheet from a winforms program. What I want is to have a google service account gain access to said sheet without the computer user ever having to interact with the program. I believe the service account is capable of this, but, if not, please let me know.

I have code(below) for accessing the sheet with the client ID, but not the service account, and the code gave me errors when I tried to run it as the service account. My question is, can anyone show me code/point me in the right direction as to how I could access data from the spreadsheet with the service account? Thank you so much, and have a great day! Let me know if you need clarifcation,

Code:
<pre lang="c#"><pre>
   private void sheetsSetup()
        {
           // sheetID = computerID;
            printLine("sheets setup");
            UserCredential credential;

            using (var stream =
                new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
            //get client secret file from <bin>
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");
                //get creds

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
                //get more creds
            }
            var service = new SheetsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

          
            String range = "A2:S";   //take values from A to Z, excluding 1st row
            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)
            {
                int i = 0;
                string code15 = "";
                foreach (var column in values)
                {
                    //IMPORTANT: This code gets the wilkyScript
                    /*  availableCodes[i] = (column[4].ToString());
                      richTextBox1.Text += availableCodes[i];
                      printLine(column[16].ToString());
                      i++;
                      */
                    i++;
                    if (column[0].ToString() == sheetID.ToString())
                    {
                        //printLine("found it at row:" + i +" with code:"+ column[codeColumn]);
                        code15 = column[codeColumn].ToString();
                        exeStringA = code15;
                        // exeStringB = exeStringA.Split('`');
                    }

                }
            }


What I have tried:

As I stated above, I've tried the client ID, but not the service account. Honestly, I have no clue where to start to convert this to the service application.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900