You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Gmail API删除草稿邮件?调用接口遇‘Not Found’错误求助

Troubleshooting "Not Found" Error When Deleting Gmail Drafts via API

Hey there! Let's figure out why you're hitting that frustrating "Not Found" error when trying to delete a draft with the Gmail API. I've dealt with this exact issue before, so let's walk through the most common fixes step by step.

1. Double-Check Your Draft ID and User ID

This is the most frequent culprit.

  • Draft ID: Ensure the draftId you're using is a valid, active draft ID retrieved directly from the Gmail API (like via users.drafts.list or users.drafts.get). Typos, extra spaces, or using an old ID for a draft that's already been deleted will trigger this error.
  • User ID: Use 'me' to refer to the currently authenticated user (this is the safest and most common choice) or the full email address of the account. Avoid using aliases or incorrect email strings here.

Example of a properly formatted request:

var request = gapi.client.gmail.users.drafts.delete({
  'userId': 'me',
  'id': 'r-1234567890abcdef' // Replace with your valid draft ID
});
request.execute(function(resp) {
  console.log('Delete response:', resp);
});

2. Verify Your OAuth Permissions

Deleting drafts requires specific API scopes. Make sure your OAuth 2.0 authorization flow includes one of these scopes:

  • https://www.googleapis.com/auth/gmail.modify
  • https://www.googleapis.com/auth/gmail.compose

If you initially requested only read-only scopes (like https://www.googleapis.com/auth/gmail.readonly), the API will reject your delete request. You'll need to re-authenticate the user, clearing any existing tokens, to grant the necessary permissions.

3. Confirm the Draft Actually Exists

Before attempting to delete, validate that the draft still exists using the users.drafts.get method. This will confirm if the ID is valid or if the draft was already removed:

gapi.client.gmail.users.drafts.get({
  'userId': 'me',
  'id': draftId
}).execute(function(getResp) {
  if (getResp.error) {
    console.log('Draft not found during validation:', getResp.error);
  } else {
    // Proceed with deletion if the draft exists
    gapi.client.gmail.users.drafts.delete({
      'userId': 'me',
      'id': draftId
    }).execute(function(deleteResp) {
      console.log('Delete successful:', deleteResp);
    });
  }
});

4. Check for API Method Typos

Ensure you're calling the correct API method: users.drafts.delete. The Gmail API doesn't have a users.drafts.remove method, so a typo here will result in a "Not Found" error.

5. Confirm the API Client Is Properly Initialized

Make sure you've fully loaded the Gmail API before making requests. Your initialization code should include something like this:

gapi.load('client:auth2', function() {
  gapi.client.init({
    apiKey: 'YOUR_API_KEY',
    clientId: 'YOUR_CLIENT_ID',
    scope: 'https://www.googleapis.com/auth/gmail.modify'
  }).then(function() {
    // Now make your draft delete request here
  });
});

If the API client isn't fully loaded, requests might be malformed or fail silently.

Start with these checks—9 times out of 10, the issue is a simple ID typo or missing permission. Good luck!

内容的提问来源于stack exchange,提问作者Deepak Verma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:21:23