make folder shared google drive api v3?

2019-02-25 13:59发布

I can make a folder using this code

var request = gapi.client.request({
       'path': '/drive/v3/files/',
       'method': 'POST',
       'headers': {
           'Content-Type': 'application/json'

           //'Authorization': 'Bearer ' + token             
       },
       'body':{
           "name" : "copy",
           "mimeType" : "application/vnd.google-apps.folder",

       }
   });


   request.execute(function(resp) { 
       console.log(resp); 
       //document.getElementById("info").innerHTML = "Created folder: " + resp.title;
   });

but I cant figure out for the life of me how to make the folder shared to all, I seen in the documentation to put type:anyone but I cant figure out how to do it in the code, thank you for your time

1条回答
一纸荒年 Trace。
2楼-- · 2019-02-25 14:45

You create a permission for a file or folder with this REST function:

POST https://www.googleapis.com/drive/v3/files/fileId/permissions

So you can do:

var fileId = File Id;
var request = gapi.client.request({
   'path': '/drive/v3/files/' + fileId + '/permissions',
   'method': 'POST',
   'headers': {
       'Content-Type': 'application/json'
       //'Authorization': 'Bearer ' + token             
   },
   'body':{
        'role': 'reader', // owner, writer, commenter
        'type': 'anyone'
   }
});

If it's successful, it'll give you a Permissions resource as a result:

{
    kind: "drive#permission",
    id: Unique identifier,
    type: string,
    emailAddress: string,
    domain: string,
    role: string,
    allowFileDiscovery: boolean,
    displayName: string,
    photoLink: string
}

I give you links to the reference, but I haven't found any example there:

查看更多
登录 后发表回答