nodejs實(shí)現(xiàn)百度輿情接口應(yīng)用示例
更新時(shí)間:2020年02月07日 11:17:20 作者:李瓊濤
這篇文章主要介紹了nodejs實(shí)現(xiàn)百度輿情接口應(yīng)用,結(jié)合實(shí)例形式分析了node.js調(diào)用百度輿情接口的具體使用技巧,需要的朋友可以參考下
本文實(shí)例講述了nodejs實(shí)現(xiàn)百度輿情接口。分享給大家供大家參考,具體如下:
const URL = require('url'); const http = require('http'); const https = require('https'); const qs = require('querystring'); let trends = exports; trends.getInstance = function () { return new Trends; } function Trends() { this.expireTime = 1800; this.accessKey = 'xxxxxxxx'; this.secretKey = 'xxxxxxxx'; this.userKey = 'xxxxxxxx'; this.userSecret = 'xxxxxxxx'; this.host = 'trends.baidubce.com'; this.timestamp = _.time(); this.utcTimestamp = _.utcTime(); } Trends.prototype.request = async function (method, uri, params) { method = method.toUpperCase(); let token = this.getToken(); let headers = this.getHeaders(method, uri); params = Object.assign({}, params || {}, { 'user_key': this.userKey, 'token': token, 'timestamp': this.timestamp }); let url = `http://${this.host}${uri}`; return await this.httpRequest(method, url, params, headers); } Trends.prototype.getHeaders = function (method, uri) { let authorization = this.getAuthorization(method, uri); return { 'Content-Type': 'application/x-www-form-urlencoded', 'Host': this.host, 'x-bce-date': this.utcTimestamp, 'Authorization': authorization, }; } Trends.prototype.getAuthorization = function (method, uri) { let authString = `bce-auth-v1/${this.accessKey}/${this.utcTimestamp}/${this.expireTime}`; let signinKey = _.hmac(authString, this.secretKey, 'sha256'); let header = { 'host': this.host, 'x-bce-date': _.urlencode(this.utcTimestamp) }; let headerArr = []; for (let name in header) { let val = header[name]; headerArr.push(`${name}:${val}`); } let headerKeyStr = Object.keys(header).sort().join(';'); let requestStr = `${method}\n${uri}\n\n${headerArr.join('\n')}`; let signautre = _.hmac(requestStr, signinKey, 'sha256'); return `${authString}/${headerKeyStr}/${signautre}`; } Trends.prototype.getToken = function () { return _.hmac(this.userKey + this.timestamp, this.userSecret); } Trends.prototype.httpRequest = async function (method, url, params, headers) { let urlObj = URL.parse(url); let protocol = urlObj.protocol; let options = { hostname: urlObj.hostname, port: urlObj.port, path: urlObj.path, method: method, headers: headers, timeout: 10000, }; let postData = qs.stringify(params || {}); return new Promise((resolve, reject) => { let req = (protocol == 'http:' ? http : https).request(options, (res) => { let chunks = []; res.on('data', (data) => { chunks.push(data); }); res.on('end', () => { let buffer = Buffer.concat(chunks); let encoding = res.headers['content-encoding']; if (encoding == 'gzip') { zlib.unzip(buffer, function (err, decoded) { resolve(decoded.toString()); }); } else if (encoding == 'deflate') { zlib.inflate(buffer, function (err, decoded) { resolve(decoded.toString()); }); } else { resolve(buffer.toString()); } }); }); req.on('error', (e) => { _.error('request error', method, url, params, e); resolve(''); }); req.on("timeout", (e) => { _.error('request timeout', method, url, params, e); resolve(''); }) if (method.toUpperCase() == 'POST') { req.write(postData); } req.end(); }); }
module.exports = function () { return new Script; } function Script() {} Script.prototype.run = async function () { let rst = this.getTaskList(); console.log(rst); } Script.prototype.getTaskList = async function () { let params = {}; let method = 'post'; let uri = '/openapi/getTasklist'; let rst = await _.trends.getInstance().request(method, uri, params); return rst; }
希望本文所述對(duì)大家node.js程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- node.js集成百度UE編輯器
- Node.js編寫爬蟲的基本思路及抓取百度圖片的實(shí)例分享
- Node Puppeteer圖像識(shí)別實(shí)現(xiàn)百度指數(shù)爬蟲的示例
- nodejs根據(jù)ip數(shù)組在百度地圖中進(jìn)行定位
- 微信小程序訪問node.js接口服務(wù)器搭建教程
- 詳解基于Node.js的微信JS-SDK后端接口實(shí)現(xiàn)代碼
- Node.js 實(shí)現(xiàn)簡(jiǎn)單的接口服務(wù)器的實(shí)例代碼
- 用NodeJS實(shí)現(xiàn)批量查詢地理位置的經(jīng)緯度接口
- 詳解nodejs微信jssdk后端接口
- 用Node編寫RESTful API接口的示例代碼
- node 文件上傳接口的轉(zhuǎn)發(fā)的實(shí)現(xiàn)
- 配置node服務(wù)器并且鏈接微信公眾號(hào)接口配置步驟詳解
相關(guān)文章
node.js中使用node-schedule實(shí)現(xiàn)定時(shí)任務(wù)實(shí)例
這篇文章主要介紹了node.js中使用node-schedule實(shí)現(xiàn)定時(shí)任務(wù)實(shí)例,包括安裝方法和4種使用例子,需要的朋友可以參考下2014-06-06Node.js中path.join()優(yōu)勢(shì)例舉分析
在本篇文章里小編給大家整理的是一篇關(guān)于Node.js中path.join()優(yōu)勢(shì)例舉分析,有興趣的朋友們可以學(xué)習(xí)下。2021-08-08Node.js Addons翻譯(C/C++擴(kuò)展)
這篇文章主要介紹了Node.js Addons翻譯(C/C++擴(kuò)展) 的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06關(guān)于Node.js的events.EventEmitter用法介紹
本篇文章主要介紹了關(guān)于Node.js的events.EventEmitter用法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04CentOS 8.2服務(wù)器上安裝最新版Node.js的方法
這篇文章主要介紹了CentOS 8.2服務(wù)器上安裝最新版Node.js的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12