javascript - 如何修复来自 Google Drive API 的“资源正文包含不可直接写入的字段”警告?
问题描述
我正在使用来自Google API Docs v3的示例(这是查看错误的实时示例)。尝试更新文件权限时,出现以下错误:
资源主体包括不可直接写入的字段。
需要更新文件权限以允许任何人访问 URL,因此我们可以将“上传”的文件用作共享工具。如果您删除 ' type ' 选项,脚本就会运行。阅读有关此的其他问题,包括this,但无助于解决此问题。总体而言,目标是使用户从谷歌驱动器选择器中选择的文件可共享。任何指针都会很棒。
下面的代码(与上面的 URL 相同)。
<script src="https://apis.google.com/js/api.js"></script>
<script>
/**
* Sample JavaScript code for drive.permissions.update
* See instructions for running APIs Explorer code samples locally:
* https://developers.google.com/explorer-help/guides/code_samples#javascript
*/
function authenticate() {
return gapi.auth2.getAuthInstance()
.signIn({scope: "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file"})
.then(function() { console.log("Sign-in successful"); },
function(err) { console.error("Error signing in", err); });
}
function loadClient() {
return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/drive/v3/rest")
.then(function() { console.log("GAPI client loaded for API"); },
function(err) { console.error("Error loading GAPI client for API", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function execute() {
return gapi.client.drive.permissions.update({
"fileId": "xxxxxx",
"permissionId": "xxxxxx",
"removeExpiration": false,
"supportsTeamDrives": true,
"transferOwnership": true,
"resource": {
"type": "anyone",
"role": "owner"
}
})
.then(function(response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response);
},
function(err) { console.error("Execute error", err); });
}
gapi.load("client:auth2", function() {
gapi.auth2.init({client_id: YOUR_CLIENT_ID});
});
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="execute()">execute</button>
总而言之,我尝试使用以下内容更新用户角色/类型,但即使没有错误,当前字段也不会更新。
<script src="https://apis.google.com/js/api.js"></script>
<script>
/**
* Sample JavaScript code for drive.permissions.list
* See instructions for running APIs Explorer code samples locally:
* https://developers.google.com/explorer-help/guides/code_samples#javascript
*/
function authenticate() {
return gapi.auth2.getAuthInstance()
.signIn({scope: "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.metadata https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/drive.photos.readonly https://www.googleapis.com/auth/drive.readonly"})
.then(function() { console.log("Sign-in successful"); },
function(err) { console.error("Error signing in", err); });
}
function loadClient() {
return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/drive/v3/rest")
.then(function() { console.log("GAPI client loaded for API"); },
function(err) { console.error("Error loading GAPI client for API", err); });
}
// Make sure the client is loaded and sign-in is complete before calling this method.
function updatePermission(fileId, permissionId, newRole) {
var newRole = "owner";
// First retrieve the permission from the API.
var request = gapi.client.drive.permissions.get({
"fileId": "xxxxx",
'permissionId': "xxxxx",
});
request.execute(function(resp) {
resp.role = newRole;
var updateRequest = gapi.client.drive.permissions.update({
'fileId': "xxxxx",
'permissionId': "xxxxx",
'resource': resp
});
updateRequest.execute(function(resp) {
});
});
}
gapi.load("client:auth2", function() {
gapi.auth2.init({client_id: "xxx"});
});
</script>
<button onclick="authenticate().then(loadClient)">authorize and load</button>
<button onclick="updatePermission()">execute</button>
现在运行,但当您检查它时实际上并没有更新信息
解决方案
如果您检查权限更新帖子正文的文档。你会注意到只有两个字段是可写的
您正在尝试更新一些不可写的列。
"removeExpiration": false,
"supportsTeamDrives": true,
"transferOwnership": true,
尝试做:
/**
* Update a permission's role.
*
* @param {String} fileId ID of the file to update permission for.
* @param {String} permissionId ID of the permission to update.
* @param {String} newRole The value "owner", "writer" or "reader".
*/
function updatePermission(fileId, permissionId, newRole) {
// First retrieve the permission from the API.
var request = gapi.client.drive.permissions.get({
'fileId': fileId,
'permissionId': permissionId
});
request.execute(function(resp) {
resp.role = newRole;
var updateRequest = gapi.client.drive.permissions.update({
'fileId': fileId,
'permissionId': permissionId,
'resource': resp
});
updateRequest.execute(function(resp) { });
});
}
推荐阅读
- selenium - 无法导入 CucumberOptions 和 Cucumber
- android - 如何从具有不同条件的同一张表中获取行
- python - 使用命令 Discord Py 赋予角色的问题
- ios - 在不重定向到应用商店或 iTunes 的情况下为 ios 应用进行应用更新
- reactjs - RN Google Signin 本机模块未正确链接
- android - 为风味中的扩展类替换清单意图过滤器
- java - Java 符号查找错误:/usr/lib64/jvm/java-11-openjdk-11/lib/libnio.so:未定义符号:initInetAddressIDs
- soap - SOAP Web 服务客户端错误,使用服务时
- sql - 获取包含特定值 SQL-Hue 的列名列表
- python - 从 MPG 加载纹理