无法删除消息时指定的无效用户ID。

huangapple go评论65阅读模式
英文:

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(&#39;deleteForever&#39;)
  .timeBased()
  .everyMinutes(1)
  .create()
}

function deleteForever() {

  var threads = GmailApp.search(&quot;in:spam&quot;);

  for (var i = 0; i &lt; threads.length; i++) {
    var msg = threads[i].getMessages()[0];
    console.log(i)
    if (msg.getPlainBody().match(/truck|car|(you won)/gi) !== null){
        console.log(&#39;Found It!&#39;)
       Gmail.Users.Messages.remove(me, threads[i].getId.toString()); 
    }

  }
}

答案1

得分: 1

以下是翻译好的部分:

建议

根据您的代码,这里有一些问题:

  1. 在您的 Gmail.Users.Messages.remove() 中,您正在使用 me 作为一个不存在的变量,因此导致了 无效的用户ID 错误。
  2. 您还需要使用 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()); 
    }
  
  }
}

演示

运行脚本后:

无法删除消息时指定的无效用户ID。

英文:

SUGGESTION

Based on your code, here are the problems:

  1. You are using me in your Gmail.Users.Messages.remove() as a variable that is non-existing, thus causing the Invalid user id error.
  2. You also need to use the message ID instead of the threads[i] ID as required on the id parameter of the users.messages.delete method.

Tweaked Script

function deleteForever() {

  var threads = GmailApp.search(&quot;in:spam&quot;);

  for (var i = 0; i &lt; threads.length; i++) {
    var msg = threads[i].getMessages()[0];
    console.log(i)
    if (msg.getPlainBody().match(/truck|car|(you won)/gi) !== null){
        console.log(&#39;Found It!&#39;)
       Gmail.Users.Messages.remove(&#39;me&#39;, msg.getId()); 
    }

  }
}

Demo

After running the script:

>无法删除消息时指定的无效用户ID。

答案2

得分: 0

在你的脚本中,变量 meundefined。而 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 &quot;me&quot;.

Gmail.Users.Messages.remove(&quot;me&quot;, threads[i].getId.toString()); 

huangapple
  • 本文由 发表于 2023年4月20日 10:10:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76060033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定