cfeteam / qcloudapi-sdk Goto Github PK
View Code? Open in Web Editor NEW腾讯云 API 2.0 SDK for node.js
License: Apache License 2.0
腾讯云 API 2.0 SDK for node.js
License: Apache License 2.0
test中config.json有两个SecretId
The node.js callback time is far away!!
`var Capi = require('qcloudapi-sdk')
//通过构造函数传入的参数将作为默认配置
var capi = new Capi({
// baseHost: 'wenzhi.api.qcloud.com',
SecretId: 'AKID4r2cxZhSLK2Mrtlnl5k3PAACygctvfpu',
SecretKey: '*****************************************',
serviceType: 'account'
})
// 传入配置以覆盖默认项
capi.request({
Region: 'bj',
Action: 'TextSentiment',
content: '我爱洗澡',
type: 1
}, function(error, data) {
if (error) {
console.log(error);
}
console.log(data)
})`
code: 6100, message: '本版本内不支持的接口或者接口已经被废弃,请参考该接口的文档。'
1、new Buffer() is deprecated
2、Use ES6 class
3、The main change is in QcloudApi.prototype.request
4、Use axios instead of request module ( or request-promise-native)
5、The original method "QcloudApi.prototype.request = function(data, opts, callback, extra) " has a problem. If don't pass data, for example , request (opts, callback, extra),the opts parameter will be treated as data.
code :
const axios = require('axios')
const qs = require('querystring')
const dotQs = require('dot-qs')
const crypto = require('crypto')
const packageJSON = require('./package.json')
const baseHost = 'api.qcloud.com'
class QcloudApi {
constructor(defaults) {
this.defaults = Object.assign({
path: '/v2/index.php',
method: 'POST',
protocol: 'https',
baseHost: baseHost
}, defaults)
}
generateUrl(opts) {
opts = opts || {}
const host = this._getHost(opts)
const path = opts.path === undefined ? this.defaults.path : opts.path
return (opts.protocol || this.defaults.protocol) + '://' + host + path
}
generateQueryString(data, opts) {
opts = opts || this.defaults
const defaults = this.defaults
let param = Object.assign({
Region: this.defaults.Region,
SecretId: opts.SecretId || this.defaults.SecretId,
Timestamp: Math.round(Date.now() / 1000),
Nonce: Math.round(Math.random() * 65535),
RequestClient: 'SDK_NODEJS_' + packageJSON.version
}, data)
param = dotQs.flatten(param)
let keys = Object.keys(param)
let qstr = '', signStr
const host = this._getHost(opts)
const method = (opts.method || defaults.method).toUpperCase()
const path = opts.path === undefined ? defaults.path : opts.path
keys.sort()
keys.forEach(function(key) {
const val = param[key]
if(method === 'POST' && val && val[0] === '@'){
return
}
if(key === '') {
return
}
if(val === undefined || val === null || (typeof val === 'number' && isNaN(val))) {
val = ''
}
qstr += '&' + (key.indexOf('_') ? key.replace(/_/g, '.') : key )+ '=' + val
})
qstr = qstr.slice(1)
signStr = this.sign(method + host + path + '?' + qstr, opts.SecretKey || defaults.SecretKey)
param.Signature = signStr
return qs.stringify(param)
}
/**
* request({data: {x: 'x'}, opts: {y: 'y'}, extra: {z:'z'}})
*/
request(options) {
{data ={}, opts = this.defaults, extra} = options
const url = this.generateUrl(opts)
const method = (opts.method || this.defaults.method).toUpperCase()
const dataStr = this.generateQueryString(data, opts)
let option = {url, method}
if(method === 'POST') {
option.data = dataStr
}else{
option.url += '?' + dataStr
}
if(extra) Object.assign(option, extra)
// use .catch or try catch to catch error in outside
return axios(option)
}
/**
* Buffer.from(string[, encoding]), encoding <string> The encoding of string. Default: 'utf8'.
*/
sign(str, secretKey) {
const hmac = crypto.createHmac('sha1', secretKey || '')
return hmac.update(Buffer.from(str)).digest('base64')
}
_getHost(opts) {
let host = opts.host
if(!host) {
host = (opts.serviceType || this.defaults.serviceType) + '.' + (opts.baseHost || this.defaults.baseHost)
}
return host
}
}
module.exports = QcloudApi
目前我们手动修改源码中的request请求函数设置成功了
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.