gpt4 book ai didi

javascript - 如何修复来自 Google Drive API 的 'The resource body includes fields which are not directly writable' 警告?

转载 作者:行者123 更新时间:2023-11-30 06:18:42 27 4
gpt4 key购买 nike

我正在使用 Google API Docs v3 中的示例(这是一个查看错误的实例)。尝试更新文件权限时,出现以下错误:

The resource body includes fields which are not directly writable.

需要更新文件权限以允许任何人访问该 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>

总而言之,我尝试使用以下内容更新用户 Angular 色/类型,但即使没有错误,当前字段也不会更新。

<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>

这现在可以运行,但当您根据 this 检查它时,它实际上并没有更新信息。

最佳答案

如果您查看 permission update 的文档帖子正文。你会注意到只有两个字段是可写的

enter image description here

您正在尝试更新一些不可写的列。

  "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) { });
});
}

关于javascript - 如何修复来自 Google Drive API 的 'The resource body includes fields which are not directly writable' 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54648272/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com