Updating File Owner with Google Drive API - Permission Error 400 "Field 'Permission Type' is mandatory"

I've been utilizing the Google Drive API within Google Apps Script. This particular script is written in sandboxed server-side Javascript, which restricts the use of the Google Client API for Javascript.

As a Google Apps super admin, I am authenticating as the user through Domain-wide delegation and a service account to impersonate other users successfully making Files:get calls on behalf of different users within the domain.

Currently, my goal is to transfer ownership of a file from user A to user B while also granting user C editing permission by following these steps:

  1. When authenticated as user A, transfer ownership of the file to user B by adding them as an owner.

  2. While authentiated as user A or B, make user C an editor of the file.

I have already completed step 1 successfully.

For transferring ownership (Step 1), I believe I need to utilize the Permissions:insert method. I have drafted the following code snippet:

var url = 'https://www.googleapis.com/drive/v2/files/' + fileId + '/permissions';

 var resource = {
  "type": "user",
  "role": "owner",
  "value": "userBEmail"
 };

 var requestBody = {};
 requestBody.method = 'POST';
 requestBody.headers = { Authorization: 'Bearer ' + service.getAccessToken() };
 requestBody.contentType = 'application/json';
 requestBody.resource = JSON.stringify(resource);
 requestBody.muteHttpExceptions = true;

 try {

   var response = UrlFetchApp.fetch(url, requestBody);

   var result = JSON.parse(response.getContentText());

   Logger.log('result: ' + JSON.stringify(result, null, 2));

 } catch(e) {

   Logger.log(e.message);

 }

The returned result displays the following error message:

{
  "error": {
    "errors": [
      {
        "domain": "global",
        "reason": "required",
        "message": "Permission type field required",
        "locationType": "other",
        "location": "permission.type"
      }
    ],
    "code": 400,
    "message": "Permission type field required"
  }
} 

In light of this, I have several inquiries:

  1. Is the approach taken suitable for transferring file ownership?

  2. Why does it state "Permission type field required" when I have included it in the request?

  3. Upon accomplishing Step 1, is there a way to integrate Step 2 within the same API call? This would enhance performance especially if processing up to 1,000 files.

It's also noteworthy that I was able to effectively transfer ownership using the API explorer located at the bottom of this page, transitioning a file between two accounts with identical file IDs and request body content.

Answer №1

Discovered the solution.

By utilizing UrlFetchApp.getRequest(), I was able to inspect the request being transmitted and identified a missing parameter known as payload.

Modified .resource to .payload which led to a successful execution of the request.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Preventing Javascript array elements from being overwritten: Best practices

My challenge lies with the addToClients() function, which is designed to add a new value to the clients array whenever a button is pressed. The issue I am facing is that each time I press submit, the previous element in the array gets replaced by the new o ...

Interact with multiple databases using the singleton design pattern in a Node.js environment

I need to establish a connection with different databases, specifically MongoDB, based on the configuration set in Redis. This involves reading the Redis database first and then connecting to MongoDB while ensuring that the connection is singleton. Here i ...

Connecting multiple promises using an array

After making an ajax call to retrieve an array of results, I have been attempting to process this data further by making additional ajax calls. However, when using Promise.all() and then continuing with .then(function(moreData){}), I noticed that the moreD ...

It is necessary to render React Native text strings within a text component

Greetings! The React Native code snippet below is responsible for rendering a user interface. However, upon running the code, an error occurred. How can I resolve this issue? The error message indicates that text strings must be rendered within a text comp ...

Re-sorting with _.sortBy() eliminates additional 0s from decimal values (transforming 0.10 to 0.1 and beyond)

Here is an array that needs to be sorted: var baseBetAmount = [ { val: 'OtherBaseBet', text: 'Other' }, { val: 0.10, text: '$0.10' }, { val: 0.20, text: '$0.20' }, { val: 0.50, text: ...

Angular: A guide to binding to the required/ngRequired attribute

There is a directive that may or may not be required, and it can be used in two different ways. <my-foo required></my-foo> or <my-foo ng-required="data.value > 10"></my-foo> Even though require and ngRequire are essentially t ...

Is there a way to link dynamic server fields with VueJS?

How can I bind data posted by the server with Vue.js in order to display the data instead of field names? <script> module.exports = { data: function () { return { filedNameFromServer: ['{{filed1}}' ...

Do commas at the end of JSON objects pose a risk of breaking

After diving into the proposed JavaScript features, one that caught my attention is the idea of supporting trailing commas in object literals and arrays. When it comes to parameters, trailing commas are not relevant, so let's put that aside for now. ...

Exploring the Art of Navigation with Ionic and Angular

Trying to integrate Angular, Meteorjs, and Ionic Framework has been a smooth process, thanks to the urigo:angular and urigo:ionic packages. However, I'm facing difficulties in configuring ionic links and angular routing to make it work seamlessly. Des ...

Issue encountered when importing async function: Invalid hook call. Hooks are designed to be called only within the body of a function component

All I desire is the ability to access logic from my geolocationApi file in my react-native components without using a hook. Instead, I prefer normal asynchronous functions. The geolocationApi file employs a custom hook for handling mobx state updates, whic ...

Tips for handling a variable that could potentially be an array or a single React component

I'm diving into React for the first time and working on creating a user survey. I'm currently dealing with checkboxes in this component, which I find a bit challenging. My goal is to have the user select 2 options and then separate them with a co ...

Utilize the cjson Lua module in Openresty to handle JSON parsing with omitted fields

My goal is to parse a JSON payload sent through a POST request to an NGINX/Openresty location. I achieved this by utilizing Openresty's content_by_lua_block along with its cjson module in the following manner: # other locations above location /test { ...

Incorporate a button within a listview on Kendoui to trigger the opening of a modal window

I am seeking assistance on how to insert a button on each element of a Listview (PHP/Json) result. When clicked, this button should open a modal window where the customer can input reservation details such as Date, Adults, and Children. Below is the JavaSc ...

I'm having trouble getting Stripe checkout to function properly with Angular 1.x

Utilizing the Stripe payment system for processing payments, I referenced a project on GitHub and a helpful blog. Incorporating nested views and routers in my project, the structure appears as follows: src app views controllers directives ...

Tips for ensuring the HTML checkbox element is fully loaded before programmatically selecting it

When a user accesses the page, I want certain checkboxes to be automatically checked. To achieve this, I am storing the IDs of the HTML checkbox elements in a service. Upon entering the page, the service is utilized to retrieve an array containing these ID ...

Having trouble removing items from the data grid list in react material-ui, any thoughts on what might be causing the issue?

I'm facing an issue with deleting items from a basic list of customers rendered using material UI DataGrid. Even though I can retrieve the specific customer id when logging to the console, I am unable to delete the item from the list. You can view my ...

How can I send parameters to an HTML file using Node.js?

In my current code res.sendfile('./home.html',{user:req.user}), I need to figure out a way to access the user parameter directly in the HTML file without relying on a template engine. Can anyone suggest how this can be achieved? ...

Using JSF 2.1 for Ajax autocomplete with server search triggered only when user pauses typing

Currently, I am working on developing an autocomplete feature that involves database search based on user input events (specifically onkeyup) in a <h:inputText />. There are two specific preconditions that need to be met for the application to perfo ...

Is there a more efficient approach to displaying a list of elements and sharing state in React with TypeScript?

Check out this code sample I'm attempting to display a list with multiple elements and incorporate a counter on the main element that updates every time one of the buttons is clicked. I'm uncertain if this approach is optimal, as I am transition ...

How can I ensure the correct order when parsing JSON and performing actions with it?

After fetching JSON data from YQL and converting it into a list, my goal is to highlight specific rows in the list based on their contents. Currently, I can achieve highlighting and toggling separately using .click, but I prefer to skip that step and have ...