nodjs调用企业微信临时素材接口示例
时间: 2023-05-20 09:06:57 浏览: 316
以下是一个使用 Node.js 调用企业微信临时素材接口的示例代码:
```javascript
const request = require('request');
const fs = require('fs');
const corpId = 'YOUR_CORP_ID';
const corpSecret = 'YOUR_CORP_SECRET';
const agentId = 'YOUR_AGENT_ID';
const mediaId = 'YOUR_MEDIA_ID';
const getTokenUrl = `https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corpId}&corpsecret=${corpSecret}`;
const getMediaUrl = `https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=${mediaId}`;
request(getTokenUrl, (err, res, body) => {
if (err) {
console.error(err);
return;
}
const accessToken = JSON.parse(body).access_token;
const getMediaUrlWithToken = getMediaUrl.replace('ACCESS_TOKEN', accessToken);
request.get(getMediaUrlWithToken)
.on('response', (response) => {
const contentType = response.headers['content-type'];
const contentDisposition = response.headers['content-disposition'];
const filenameMatch = contentDisposition.match(/filename="(.+)"/);
const filename = filenameMatch ? filenameMatch[1] : 'unknown';
response.pipe(fs.createWriteStream(filename))
.on('finish', () => {
console.log(`Downloaded ${filename}`);
});
});
});
```
请将 `YOUR_CORP_ID`、`YOUR_CORP_SECRET`、`YOUR_AGENT_ID` 和 `YOUR_MEDIA_ID` 替换为你自己的企业微信的相关信息。这个示例代码会下载指定 `media_id` 对应的临时素材,并将其保存到本地文件系统中。
阅读全文