0
votes

I trying to create a spreadsheet using the google spreadsheets v4. I generated a new server to server credential json on console api. All appears to work, but when I try to access the generated url, I got a screen saying to request permission. The credential was generated from my own service account, associated with my own google account, so I understand that I already have this permission. What could I do to access the generated sheet on browser? Above my code to generate the spreadsheet and the screen I getting.

public class Program { static string[] Scopes = { SheetsService.Scope.Spreadsheets, SheetsService.Scope.Drive, SheetsService.Scope.DriveFile }; static string ApplicationName = "Google Sheets API .NET Quickstart";

    public static void Main(string[] args)
    {
        AppendData();
    }

    public static void AppendData()
    {           
        // the downloaded jsonn file with private key

        var credential = GoogleCredential.FromStream(new FileStream(Environment.CurrentDirectory + "/ApiKey/api_key.json", FileMode.Open)).CreateScoped(Scopes);

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

        });

        Spreadsheet requestBody = new Spreadsheet()
        {
            Properties = new SpreadsheetProperties()
            {
                Title = "Calculo Projeção",

            },

        };

        SpreadsheetsResource.CreateRequest request = service.Spreadsheets.Create(requestBody);
    }
}

enter image description here

1

1 Answers

1
votes

So, the GSheet you've created was created using the service account. This means that the service account is the owner of the GSheet.

Depending on what your end goal is, and what type of Google Account you're using you may have some options here.

If you're using a G Suite account, the solution is relatively simple. It's to use impersonation with the service account. You're then basically using the service account to impersonate a user to perform the actions. If you impersonate your own account while creating the GSheet, the owner will be your own account. The documentation talks about the specifics here.

If you're NOT using a G Suite account, but a 'normal' Google account, it becomes a bit more tricky. I can think of some options here though:

  1. Quick and dirty: Create the GSheet with your personal account first and give the service account access to it.
  2. Have the service account create the GSheet and use the API/library to give your personal account access.
  3. Create the GSheet not with the service account, but using OAuth and your own credentials. There's several ways to go about this. A good starting point for this would be here...

I may have missed one or two here, but you should understand what's actually happening now.

NOTE: One very important thing you have to take into account. If you delete the service account, any and all Google Drive items (including Sheets, Docs, Forms, etc) get deleted without any way of getting them back.