【问题标题】:Google Drive API v3 & Google Picker: Change the download permission of fileGoogle Drive API v3 & Google Picker:更改文件的下载权限
【发布时间】:2020-07-21 17:43:22
【问题描述】:

我已使用 Google Picker API 检索“我的云端硬盘”视频 URL 并将其发布到我的应用中。所以我想更改他们的sharing permission(公开)并使用copyRequiresWriterPermission 选项禁用下载按钮。

这是我在 Picker 回调函数中更改权限的代码;

function pickerCallback(data, e) {
    if (data.action == google.picker.Action.PICKED) {
        var docs = data[google.picker.Response.DOCUMENTS];
        var type = "anyone";
        var role = "reader";
        var urls = new Array();
        
        for (var d = 0; d < docs.length; d++) {
            var request1 = gapi.client.request({
              'path': '/drive/v3/files/' + docs[d].id + '/permissions',
              'method': 'POST',
              'headers': {
                'Content-Type': 'application/json',
                'Authorization': 'Bearer ' + oauthToken
              },
              'body':{
                'role': role,
                'type': type
              }
            });
            request1.execute(function(resp) {
              console.log(resp);
            });

            urls.push(docs[d].url);
        }

        elements.push({name: 'url', value: urls});

        // continue with an ajax funtion ...
    
    }
};

效果很好,所以我决定添加一个更改下载限制的新请求:

var request2 = gapi.client.request({
    'path': '/drive/v3/files/' + docs[d].id,
    'method': 'POST',
    'headers': {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + oauthToken
    },
    'body':{
        'copyRequiresWriterPermission': true
    }
});
request2.execute(function(resp) {
    console.log(resp);
});

但只有我获得了 404 状态。另外,我尝试过:

'/upload/drive/v3/files/' + docs[d].id

但同样的问题。

我已经在 Drive API Demo 中尝试了带有文件 ID 的请求并且可以正常工作。 request2代码有什么错误?

使用的范围:

var scope = [
    "https://www.googleapis.com/auth/drive.file",
    "https://www.googleapis.com/auth/drive",
    "https://www.googleapis.com/auth/drive.metadata",
    "https://www.googleapis.com/auth/drive.readonly",
];

更新代码

    var request2 = gapi.client.request({
        'path': '/drive/v3/files/' + docs[d].id ,
        'method': 'PATCH',
        'headers': {
            'Content-Type': 'application/json',
            'Authorization': 'Bearer ' + oauthToken
        },
        'body':{copyRequiresWriterPermission: true}
                    
    });
   request2.execute(function(resp) {
    console.log(resp);
   });

这段代码执行没有问题,但没有任何反应,因为我需要取消选中这个参数:

有可能吗?

【问题讨论】:

  • 我不明白你说在哪里需要取消选中该选项。你能解释一下在哪里吗?
  • 我的应用附加了来自 Google Picker API 的视频,因此我需要阻止用户(直接)下载它们。我需要在“我的云端硬盘”中我的视频的共享配置中更改此选项,这样当用户预览视频时,这将禁用下载按钮。
  • 你是说用request2更新文件的请求没有更新文件?
  • 是的。我不确定这是不是正确的方法(如果可能的话)

标签: google-api google-drive-api google-api-client google-picker


【解决方案1】:

如果您想更新驱动器中的文件,请求方法应为PATCH 而不是POST

示例:

var request2 = gapi.client.request({
    'path': '/drive/v3/files/' + docs[d].id,
    'method': 'PATCH',
    'headers': {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + oauthToken
    },
    'body': {
        copyRequiresWriterPermission: true
    }

});
request2.execute(function(resp) {
    console.log(resp);
});

参考资料:

【讨论】:

  • 谢谢,现在显示一个执行,但不要更新下载参数。我会更新我的帖子
  • 我不明白你说的“不要更新下载参数”是什么意思。我认为您使用“copyRequiresWriterPermission”错误,如果您希望用户能够在未经许可的情况下下载它,您可能需要将其设置为false。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-04
  • 1970-01-01
  • 2020-10-23
  • 2023-02-06
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多