英文:
Invalid User ID Specified When Trying to Remove Message
问题
以下是您要翻译的内容:
这是我第一次使用Apps Scripts。我只在我的个人电子邮件帐户上使用它。我正在尝试使用我在StackOverflow上找到的一个脚本,该脚本应该永久删除我的垃圾邮件文件夹中包含特定单词的电子邮件。我已经测试了代码,它似乎能够根据我指定的文本正确找到电子邮件。
我的问题是我无法让它执行实际的电子邮件删除操作。我认为我没有正确指定我的电子邮件地址。在我的代码示例中,我使用了 me。我还尝试过硬编码我的电子邮件,但没有成功。
当我尝试执行代码时,它给出了以下错误消息:“GoogleJsonResponseException: API call to gmail.users.messages.delete failed with error: Invalid user id specified in request/Delegation denied at deleteForever(Code:17:29)”。
引用我的个人电子邮件帐户的正确方式是什么?
function setTrigger() {
ScriptApp.newTrigger('deleteForever')
.timeBased()
.everyMinutes(1)
.create()
}
function deleteForever() {
var threads = GmailApp.search("in:spam");
for (var i = 0; i < threads.length; i++) {
var msg = threads[i].getMessages()[0];
console.log(i)
if (msg.getPlainBody().match(/truck|car|(you won)/gi) !== null){
console.log('Found It!')
Gmail.Users.Messages.remove(me, threads[i].getId.toString());
}
}
}
请注意,代码中的 me 部分需要替换为您的实际电子邮件地址。
英文:
This is my first time to use Apps Scripts. I am only using this on my personal email account. I am trying to use a script that I came across on StackOverflow that is supposed to permanently delete emails with specific words that are in my spam folder. I have tested the code and it seems to correctly find the emails based on the text that I specify.
My problem is that I cannot get it perform the actual deletion of the emails. I think that I am not specifying my email address correctly. In my code example, I used me. I have also tried hard coding my email, but that did not work.
When I try to execute the code, it gives me the following error: "GoogleJsonResponseException: API call to gmail.users.messages.delete failed with error: Invalid user id specified in request/Delegation denied at deleteForever(Code:17:29)
What is the proper way to reference my personal email account?
function setTrigger() {
ScriptApp.newTrigger('deleteForever')
.timeBased()
.everyMinutes(1)
.create()
}
function deleteForever() {
var threads = GmailApp.search("in:spam");
for (var i = 0; i < threads.length; i++) {
var msg = threads[i].getMessages()[0];
console.log(i)
if (msg.getPlainBody().match(/truck|car|(you won)/gi) !== null){
console.log('Found It!')
Gmail.Users.Messages.remove(me, threads[i].getId.toString());
}
}
}
答案1
得分: 1
以下是翻译好的部分:
建议
根据您的代码,这里有一些问题:
- 在您的
Gmail.Users.Messages.remove()
中,您正在使用 me 作为一个不存在的变量,因此导致了无效的用户ID
错误。 - 您还需要使用
message
ID 而不是threads[i]
ID,这是 users.messages.delete 方法的id
参数所需的。
调整后的脚本
function deleteForever() {
var threads = GmailApp.search("in:spam");
for (var i = 0; i < threads.length; i++) {
var msg = threads[i].getMessages()[0];
console.log(i)
if (msg.getPlainBody().match(/truck|car|(you won)/gi) !== null){
console.log('找到了!')
Gmail.Users.Messages.remove('me', msg.getId());
}
}
}
演示
运行脚本后:
英文:
SUGGESTION
Based on your code, here are the problems:
- You are using me in your
Gmail.Users.Messages.remove()
as a variable that is non-existing, thus causing theInvalid user id
error. - You also need to use the
message
ID instead of thethreads[i]
ID as required on theid
parameter of the users.messages.delete method.
Tweaked Script
function deleteForever() {
var threads = GmailApp.search("in:spam");
for (var i = 0; i < threads.length; i++) {
var msg = threads[i].getMessages()[0];
console.log(i)
if (msg.getPlainBody().match(/truck|car|(you won)/gi) !== null){
console.log('Found It!')
Gmail.Users.Messages.remove('me', msg.getId());
}
}
}
Demo
After running the script:
答案2
得分: 0
在你的脚本中,变量 me
是 undefined
。而 undefined
不是一个有效的 "userId"。me
应该是字符串 "me"
。
Gmail.Users.Messages.remove("me", threads[i].getId.toString());
英文:
In your script, the variable me
is undefined
. And undefined
is not a valid "userId". me
should be the string "me"
.
Gmail.Users.Messages.remove("me", threads[i].getId.toString());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论