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()
{
printLine("sheets setup");
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);
}
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
String range = "A2:S";
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)
{
i++;
if (column[0].ToString() == sheetID.ToString())
{
code15 = column[codeColumn].ToString();
exeStringA = code15;
}
}
}
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.