到目前为止,我使用客户端的方式是这样的
client = new OAuth2Client(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
"http://localhost:5000/oauth2callback"
);
client.setCredentials({refresh_token: getRefreshToken(user)});
let url = 'https://www.googleapis.com/gmail/v1/users/me/messages?q=myquery';
client.request({url}).then((response) => {
doSomethingWith(response);
});由于response包含一个消息ids列表,因此我必须使用users.messages.get来获取每条消息的实际数据。我不喜欢仅仅为了一个查询而做数百个单独的请求。有没有一种方法可以批量处理users.messages.get请求?
发布于 2021-02-09 15:15:34
您可以使用Google API的batching requests功能。下面是一个示例函数,它接受消息ID数组和客户端作为参数,然后向users.messages.get端点发出批处理请求。
const batchGetMessages = (messageIds = [], oAuth2Client) => {
const url = 'https://www.googleapis.com/batch/gmail/v1';
const boundary = 'message_batch_demo';
const headers = {
'Content-Type': `multipart/mixed; boundary=${boundary}`
};
let data = '';
for (const messageId of messageIds) {
data += `--${boundary}\r\nContent-Type: application/http\r\n\r\n`;
data += `GET /gmail/v1/users/me/messages/${messageId}`;
data += '\r\n';
}
data += `--${boundary}--`;
return oAuth2Client.request({
url,
headers,
data,
method: 'POST'
});
};https://stackoverflow.com/questions/66056908
复制相似问题