使用 microsoft graph rest 从邮件下载附件 api
download attachments from mail using microsoft graph rest api
我已经使用 microsoft graph rest api 成功获取了收件箱中的邮件列表,但我很难理解有关如何从邮件下载附件的文档。
例如:这个问题 谈到了我打算实现的目标,但我不明白提到的端点中的 message_id 是什么:https://outlook.office.com/api/v2.0/me/messages/{message_id}/attachments
更新
我能够使用以下端点获取附件的详细信息:https://graph.microsoft.com/v1.0/me/messages/{id}/attachments 并得到以下响应。
我的印象是响应可能包含 link 以下载附件,但响应包含名为 contentBytes 的密钥,我猜这是文件的加密内容。
对于attachment
resource of file typecontentBytes
属性returns
base64-encoded contents of the file
例子
以下 Node.js 示例演示了如何获取附件属性以及附件内容(对 request
library 有依赖性):
const attachment = await getAttachment(
userId,
mesasageId,
attachmentId,
accessToken
);
const fileContent = new Buffer(attachment.contentBytes, 'base64');
//...
哪里
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);
});
};
更新
以下示例演示如何在浏览器中将附件下载为文件
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);
}
其中
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
我不知道这是否有帮助,但您只需在请求末尾添加 /$value 即可:
https://graph.microsoft.com/v1.0/me/messages/{message_id}/attachments/{attachment_id}/$value
我已经使用 microsoft graph rest api 成功获取了收件箱中的邮件列表,但我很难理解有关如何从邮件下载附件的文档。
例如:这个问题 https://outlook.office.com/api/v2.0/me/messages/{message_id}/attachments
更新
我能够使用以下端点获取附件的详细信息:https://graph.microsoft.com/v1.0/me/messages/{id}/attachments 并得到以下响应。
我的印象是响应可能包含 link 以下载附件,但响应包含名为 contentBytes 的密钥,我猜这是文件的加密内容。
对于attachment
resource of file typecontentBytes
属性returns
base64-encoded contents of the file
例子
以下 Node.js 示例演示了如何获取附件属性以及附件内容(对 request
library 有依赖性):
const attachment = await getAttachment(
userId,
mesasageId,
attachmentId,
accessToken
);
const fileContent = new Buffer(attachment.contentBytes, 'base64');
//...
哪里
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);
});
};
更新
以下示例演示如何在浏览器中将附件下载为文件
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);
}
其中
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
我不知道这是否有帮助,但您只需在请求末尾添加 /$value 即可:
https://graph.microsoft.com/v1.0/me/messages/{message_id}/attachments/{attachment_id}/$value