0

目前我正在Chrome擴展我想特別是電子郵件(即MESSAGE_ID = 1543c2a6347d984c)附件數據獲取Gmail附件。我得到了email message_id也獲得了附件文件名(即abc.zip),但是如何獲得該附件並使用Ajax發送回我的服務器。如何使用JavaScript通過GET請求之前發送

function getAttachments(userId, message, callback) { 
    var parts = message.payload.parts; 
    for (var i = 0; i < parts.length; i++) { 
    var part = parts[i]; 
    if (part.filename && part.filename.length > 0) { 
     var attachId = part.body.attachmentId; 
     var request = gapi.client.gmail.users.messages.attachments.get({ 
     'id': attachId, 
     'messageId': message.id, 
     'userId': userId 
     }); 
     request.execute(function(attachment) { 
     callback(part.filename, part.mimeType, attachment); 
     }); 
    } 
    } 
} 

調用

var userId="[email protected]" 
    var message_id="1543c2a6347d984d"; 

    getAttachments(userId,message_id,function callback(filename,minetype,attachment){ 

console.log('File Name is '+filename); 
console.log('MimeType is '+minetype); 
}); 

錯誤: main.js:15遺漏的類型錯誤:無法讀取的未定義的屬性 '零件'(功能getAttachments 2號線)

回答

2

你MESSAGE_ID是字符串,然後你調用getAttachments方法並將其作爲第二個參數傳遞,在getAttachments中,你調用message.payload.parts,這意味着你想得到一個屬性​​,當然是undefined

拿着贓物Users.messages.getUsers.messages.attachments.get,您需要先收到消息,然後從中獲取附件。

function getAttachments(userId, message, callback) { 
    var parts = message.payload.parts; 
    for (var i = 0; i < parts.length; i++) { 
    var part = parts[i]; 
    if (part.filename && part.filename.length > 0) { 
     var attachId = part.body.attachmentId; 
     var request = gapi.client.gmail.users.messages.attachments.get({ 
     'id': attachId, 
     'messageId': message.id, 
     'userId': userId 
     }); 
     request.execute(function(attachment) { 
     callback(part.filename, part.mimeType, attachment); 
     }); 
    } 
    } 
} 

function getMessage(userId, messageId, callback) { 
    var request = gapi.client.gmail.users.messages.get({ 
    'userId': userId, 
    'id': messageId 
    }); 
    request.execute(function(message) { 
     callback(userId, message); 
    }); 
} 

var userId="[email protected]" 
var message_id="1543c2a6347d984d"; 

getMessage(userId, message_id, function(userId, message) { 
    getAttachments(userId, message, function(filename,minetype,attachment) { 
     console.log('File Name is '+filename); 
     console.log('MimeType is '+minetype); 
    }); 
}); 
+0

究竟什麼是該解決方案?拉特的看到這[鏈接](https://developers.google.com/gmail/api/v1/reference/users/messages/attachments/get#examples) –

+1

@DivyeshKanzariya,看到我的答案。 –

+0

如何申請許可https://mail.google.com/ https://www.googleapis.com/auth/gmail.modify https://www.googleapis.com/auth/gmail.readonly該豪宅 –