0

I am accessing google spread sheets using c#.

Actually they are 3 ways to access the spread from https://console.developers.google.com/ .

I am using the service account credentials to access the google spread sheet.I have created a project in https://console.developers.google.com/, I enabled the sheet API and I also created new service account credentials , P-12 file is downloaded to my machine. At the particular point of code shown below

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

        ValueRange response = request.Execute();

When I try to pull data from Google Spread sheet, it goes through the authorization process, which appears to work properly. Then it fails with:

An unhandled exception of type 'Google.GoogleApiException' occurred in Google.Apis.dll Additional information: Google.Apis.Requests.RequestError The caller does not have permission [403] Errors [ Message[The caller does not have permission] Location[ - ] Reason[forbidden] Domain[global] ]

I've been struggling with this for a few hours. I've double checked that the credentials is being used, and is authorized on Google Spread sheets. Am I missing anything ?

wonea
  • 4,783
  • 17
  • 86
  • 139
prasanthi
  • 562
  • 1
  • 9
  • 25
  • Since you are using service account, make sure that you have the right scope. Also, the script must also have the rights set in the admin console. You may also refer in this [post](http://stackoverflow.com/a/17714696/5832311) which suggests to check if you're providing the correct ID. – abielita Apr 20 '17 at 16:16
  • Thank you for suggestion . I have given a right scope. – prasanthi Apr 21 '17 at 05:22
  • Is it compulsory to have the rights set in admin console.Is there any other way to do this? @abielita – prasanthi Apr 21 '17 at 05:24

2 Answers2

1

This trouble is caused by incorrect scopes of authorization, I pretty much sure. In many cases which guided by official API documentation, you can see this code

            const string credPath = "token.json";
            credential = GoogleWebAuthorizationBroker
                .AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true))
                .Result;

In those examples, you're specifying the last parameter to save some auth data to the folder defined by the credPath variable. Note that if you want to change your scopes after you run the app once, this auth data won't be overwritten so you have to:

  1. delete this existing data every time before running the app
  2. change credPath
  3. or simply remove this parameter, but in this case, you'll have to confirm your auth every time you start the app.
Murrelken
  • 43
  • 6
0

You need to assign permission to your newly created credential account.

simply click on sheet share button and add the email id of your credential account with edit permission.

google sheet api

Kashif
  • 40
  • 2