如何用Gmail API删除草稿邮件?调用接口遇‘Not Found’错误求助
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
draftIdyou're using is a valid, active draft ID retrieved directly from the Gmail API (like viausers.drafts.listorusers.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.modifyhttps://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

