使用 microsoft graph rest api 从邮件中下载附件 [英] download attachments from mail using microsoft graph rest api
问题描述
我已经使用 microsoft graph rest api 成功获取了收件箱中的邮件列表,但我很难理解有关如何从邮件下载附件的文档.
例如:这个问题
我的印象是响应可能包含下载附件的链接,但是响应包含名为 contentBytes 的密钥,我猜它是文件的加密内容.
对于 attachment
资源 的 文件类型 contentBytes
属性返回
base64 编码的文件内容
示例
以下 Node.js 示例演示了如何获取附件属性和附件内容(依赖于 <代码>请求代码>库):
const attach = await getAttachment(用户身份,消息 ID,附件 ID,访问令牌);const fileContent = new Buffer(attachment.contentBytes, 'base64');//...
哪里
const requestAsync = options =>{返回新的承诺((解决,拒绝)=> {请求(选项,(错误,资源,正文)=> {if (!error && res.statusCode == 200) {解决(身体);} 别的 {拒绝(错误);}});});};const getAttachment = (userId, messageId, attachmentId, accessToken) =>{返回请求异步({网址:`https://graph.microsoft.com/v1.0/users/${userId}/messages/${messageId}/attachments/${attachmentId}`,方法:获取",标题:{授权:`Bearer ${accessToken}`,接受:application/json;odata.metadata=none"}}).那么(数据=> {返回 JSON.parse(data);});};
更新
以下示例演示如何在浏览器中将附件下载为文件
尝试{const 附件 = 等待 getAttachment(用户身份,消息 ID,附件 ID,访问令牌);下载(数据:应用程序/pdf;base64,"+attachment.contentBytes,Sample.pdf",application/pdf");}赶上(前){控制台日志(前);}
哪里
异步函数 getAttachment(userId, messageId, attachmentId, accessToken){const res = 等待获取(`https://graph.microsoft.com/v1.0/users/${userId}/messages/${messageId}/attachments/${attachmentId}`,{方法:获取",标题:{授权:`Bearer ${accessToken}`,接受:application/json;odata.metadata=none"}});返回 res.json();}
<块引用>
I've been successfully getting the list of mails in inbox using microsoft graph rest api but i'm having tough time to understand documentation on how to download attachments from mail.
For example : This question stackoverflow answer speaks about what i intend to achieve but i don't understand what is message_id in the endpoint mentioned : https://outlook.office.com/api/v2.0/me/messages/{message_id}/attachments
UPDATE
i was able to get the details of attachment using following endpoint : https://graph.microsoft.com/v1.0/me/messages/{id}/attachments and got the following response.
I was under an impression that response would probably contain link to download the attachment, however the response contains key called contentBytes which i guess is the encrypted content of file.
For attachment
resource of file type contentBytes
property returns
base64-encoded contents of the file
Example
The following Node.js example demonstrates how to get attachment properties along with attachment content (there is a dependency to request
library):
const attachment = await getAttachment(
userId,
mesasageId,
attachmentId,
accessToken
);
const fileContent = new Buffer(attachment.contentBytes, 'base64');
//...
where
const requestAsync = options => {
return new Promise((resolve, reject) => {
request(options, (error, res, body) => {
if (!error && res.statusCode == 200) {
resolve(body);
} else {
reject(error);
}
});
});
};
const getAttachment = (userId, messageId, attachmentId, accessToken) => {
return requestAsync({
url: `https://graph.microsoft.com/v1.0/users/${userId}/messages/${messageId}/attachments/${attachmentId}`,
method: "GET",
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/json;odata.metadata=none"
}
}).then(data => {
return JSON.parse(data);
});
};
Update
The following example demonstrates how to download attachment as a file in a browser
try {
const attachment = await getAttachment(
userId,
mesasageId,
attachmentId,
accessToken
);
download("data:application/pdf;base64," + attachment.contentBytes, "Sample.pdf","application/pdf");
} catch (ex) {
console.log(ex);
}
where
async function getAttachment(userId, messageId, attachmentId, accessToken){
const res = await fetch(
`https://graph.microsoft.com/v1.0/users/${userId}/messages/${messageId}/attachments/${attachmentId}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${accessToken}`,
Accept: "application/json;odata.metadata=none"
}
}
);
return res.json();
}
Dependency:
download.js
library
这篇关于使用 microsoft graph rest api 从邮件中下载附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!