Google Sheets API returns “The caller does not have permission” when using server key

后端 未结 5 1180
青春惊慌失措
青春惊慌失措 2020-12-01 03:56

I\'ve generated a server key in the API Manager and attempted to execute the following on my Mac:

curl \'https://sheets.googleapis.com/v4/spreadsheets/MyShee         


        
相关标签:
5条回答
  • 2020-12-01 04:27

    The easiest way is to fix using gcloud cli. More docs here https://cloud.google.com/pubsub/docs/quickstart-cli#before-you-begin

    install gcloud

    sudo apt-get install google-cloud-sdk
    

    then call

    gcloud init
    

    then check your active project and credentials

    gcloud config configurations list
    

    If it is not ok, make sure you are authenticated with the correct account:

    gcloud auth list
    * account 1
      account 2
    

    Change to the project's account if not:

    gcloud config set account `ACCOUNT`
    

    Depending on the account, the project list will be different:

    gcloud projects list
    
    - project 1
    - project 2...
    

    Switch to intended project:

    gcloud config set project `PROJECT NAME`
    

    Then Create Application Default Credentials with gcloud auth application-default login, and then google-cloud will automatically detect such credentials.

    0 讨论(0)
  • 2020-12-01 04:31

    I know it is a little late to answer but for other people struggling with the same issue.
    Just change the permission of the sheet to public on your drive so it can be accessed without authentication via API calls.

    To change access:

    1. Open sheet in google drive
    2. On top right corner, click share
    3. On bottom of prompt window, click advanced
    4. Change permission to public or people with link (no signin required)

    Send API request to fetch data from sheets without authentication.

    Note: if the sheet contains sensitive data then it is not safe to make it public and rather do it with Authenticated access.

    0 讨论(0)
  • 2020-12-01 04:35

    Make sure to pay attention to @KishanPatel's comment:

    Also, you can share this sheet with specific email Ex. your service account (project) email. "client_email": "XXXXX@northern-gasket-XXXX.iam.gserviceaccount.com", This will allow to access sheet by your script.

    0 讨论(0)
  • 2020-12-01 04:43

    My 10 cents... A simple example to read the sheet using Java.

        private Credential getCredentials() throws IOException {
                final InputStream accessKey = new ByteArrayInputStream("<credential json>");
                final GoogleCredential credential = GoogleCredential.fromStream(accessKey)
                        .createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS_READONLY));
                return credential;
            }
    
        private HttpTransport httpTransport() {
                try {
                    return GoogleNetHttpTransport.newTrustedTransport();
                } catch (GeneralSecurityException | IOException e) {
                    throw new SpreadSheetServiceException(e);
                }
            }
    
    
        Sheets service = new Sheets.Builder(httpTransport(), JSON_FACTORY, getCredentials())
                        .setApplicationName("app-name")
                        .build();
                ValueRange response = service.spreadsheets().values()
                        .get("<spread_sheet_id>", "A1:A")
                        .execute();
    
    0 讨论(0)
  • 2020-12-01 04:48

    To solve this issue, try to:

    1. Create a service account: https://console.developers.google.com/iam-admin/serviceaccounts/
    2. In options, create a key: this key is your usual client_secret.json - use it the same way
    3. Make the role owner for the service account (Member name = service account ID = service account email ex: thomasapp@appname-201813.iam.gserviceaccount.com
    4. Copy the email address of your service account = service account ID
    5. Simply go in your browser to the Google sheet you want to interact with
    6. Go to SHARE on the top right of your screen
    7. Go to advanced settings and share it with email address of your service account ex: thomasapp@appname-201813.iam.gserviceaccount.com

    It worked for me :)

    0 讨论(0)
提交回复
热议问题