Writing to Google Sheets API using C# .NET and a Services Account

William Chislett
2 min readOct 27, 2018

--

TLDR: Create a Google Service account with the service account user role; download credentials file; add the service account’s email address to write permissions on the sheet; scroll to the bottom of this page for the C# code for connecting/using the Sheets service.

This should work for the rest of the G-Suite APIs too…
  1. Create a new API project at https://console.developers.google.com/.

2. Go to the Google API credentials page, click to “Create credentials” and select Service account key.

3. On the same credentials page, click to manage service accounts (link on right hand side, shown below):

4. Create a new service account if you don’t already have one. For Roles, assign it the Service Account User role:

5. Click to create a new key for this account, select JSON and it should download a JSON credential file for you.

6. Save the credentials file to the root of your project (or subfolder) and in Visual Studio set the property Copy to Output Path to “Copy Always”. Note that you shouldn’t make this file publicly available if it’s being used in a website solution.

7. Copy the service account user’s email address, shown below:

8. Add this email address to the Google sheet that you want to edit via the API; give it write permissions:

9. Add the Google Sheets API Nuget package to your .NET project.

10. Connect to the Google Service and write your data to a sheet using the API methods shown below. Use the sheet ID from the sheet’s URL.

Questions/comments/debates welcome…

--

--