How to add 'field' property to a Google Drive API v3 call in JavaScript?

tl;dr I am new to JavaScript and Google Apps Script and I have no idea how to add the 'fields' property to a Google Drive v3 API call.

I am trying to modify file permissions in a G Suite domain using Google Apps Script, a service account, and the OAuth 2 sample from Google. I wrote a function for Drive API v3 to replace Drive API v2 getIdForEmail, but API v3 requires the 'fields' query parameter to request specific fields.

The error given when I run the script is:

Request failed for https://www.googleapis.com/drive/v3/about returned code 400. Truncated server response: { "error": { "errors": [ { "domain": "global", "reason": "required", "message": "The 'fields' parameter is required for this meth...

I found the answer in a different programming language but can't translate it to Google Apps Script / JavaScript. See Fields on the previous answer: Google Drive API v3 Migration. How do I add the 'fields' property to request 'permissionId'?

    function getPermissionIdForEmail(userEmail) {
      var service = getService(userEmail);
      if (service.hasAccess()) {
        var url = 'https://www.googleapis.com/drive/v3/about';
        var options = {
          'method': 'get',
          'contentType': 'application/json'
        };

        var response = UrlFetchApp.fetch(url, {
          headers: {
            Authorization: 'Bearer ' + service.getAccessToken()
          }
        });
        var result = JSON.parse(response.getContentText());
        Logger.log('getPermissionIdForEmail result: %s', JSON.stringify(result, null, 2));
      } else {
        Logger.log('getPermissionIdForEmail getLastError: %s', service.getLastError());
      }
     }

Edit: Thank you Cameron Roberts for the help. The solution I used is

var url = 'https://www.googleapis.com/drive/v3/about' + '?fields=user/permissionId';

Answers:

Answer

I can't recall offhand if Google will accept a POST request here, if they will this could be passed as a request payload:

var response = UrlFetchApp.fetch(url, {
  headers: {
     Authorization: 'Bearer ' + service.getAccessToken()
  },
  payload: {
     fields: 'kind,user,storageQuota' 
  }
});

Or if it must be a GET request you can append the parameters directly to the url:

url = url+'?fields=kind,user,storageQuota'
var response = UrlFetchApp.fetch(url, {
  headers: {
    Authorization: 'Bearer ' + service.getAccessToken()
  }
});

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.