nodejs 交互示例
~~~
var request = require('request')
var md5 = require("md5")
var crypto = require("crypto-js")
var GSDataAPI_APP_KEY = 'xxxx'
var GSDataAPI_APP_ID = 'xxxxx'
var GSDataAPI_BaseURL = 'api.gsdata.cn'
function getJsonKeySort (JSON) {
var keyList = []
var sortFun = {}
for (item in JSON) {
keyList.push(item)
}
keyList.sort()
for (k in keyList) {
sortFun[keyList[k]] = JSON[keyList[k]]
}
return sortFun
}
function getQueryStr (JSON) {
var QueryStr = ''
for (item in JSON) {
QueryStr += (item + '=' + JSON[item])
QueryStr += '&'
}
QueryStr = QueryStr.substring(0, QueryStr.length - 1)
return QueryStr
}
function getNowFormatDate (date, type) {
var day = date;
var Year = 0;
var Month = 0;
var Day = 0;
var CurrentDate = "";
Year= day.getFullYear();
Month= day.getMonth()+1;
Day = day.getDate();
CurrentDate += Year;
if (Month >= 10 ){
CurrentDate += Month;
}
else{
CurrentDate += "0" + Month;
}
if (Day >= 10 ){
CurrentDate += Day ;
}
else{
CurrentDate += "0" + Day ;
}
var UTCstr = 'T'
var Hours = day.getHours()
var Minutes = day.getMinutes()
var Seconds = day.getSeconds()
if (Hours >= 10 ){
UTCstr += Hours ;
}
else{
UTCstr += "0" + Hours ;
}
if (Minutes >= 10 ){
UTCstr += Minutes ;
}
else{
UTCstr += "0" + Minutes ;
}
if (Seconds >= 10 ){
UTCstr += Seconds ;
}
else{
UTCstr += "0" + Seconds ;
}
UTCstr += 'Z'
return type != 'UTC' ? CurrentDate : CurrentDate + UTCstr;
}
function getSignatureKey (Crypto, key, dateStamp, serviceName) {
var kDate = Crypto.HmacSHA256(dateStamp, "GSDATA" + key);
var kService = Crypto.HmacSHA256(serviceName, kDate);
var kSigning = Crypto.HmacSHA256("gsdata_request", kService);
return kSigning;
}
function getHeaders (method, canonicalURI, canonicalQueryString, parameters) {
var headers = {}
var t = new Date()
var gsDate = getNowFormatDate(t, 'UTC')
headers['host'] = GSDataAPI_BaseURL
headers['x-gsdata-date'] = gsDate
var canonicalHeaders= 'host:' + GSDataAPI_BaseURL + '\n' + 'x-gsdata-date:' + gsDate
var signedHeaders = 'host;x-gsdata-date'
var requestPayload = method == 'GET' ? crypto.SHA256(new Buffer('').toString('utf8')).toString() : crypto.SHA256(new Buffer(parameters).toString('utf8')).toString()
var canonicalRequest = method.toUpperCase() + '\n' + canonicalURI + '\n' + encodeURI(canonicalQueryString) + '\n' + canonicalHeaders + '\n' + signedHeaders +'\n'+ requestPayload
var hashedCanonicalRequest = crypto.SHA256(new Buffer(canonicalRequest).toString('utf8')).toString()
var stringToSign = 'GSDATA-HMAC-SHA256' + '\n' + gsDate + '\n' + hashedCanonicalRequest
var dateStamp = getNowFormatDate(t, 'YYYYMMDD')
var signature = crypto.HmacSHA256(stringToSign, getSignatureKey(crypto, GSDataAPI_APP_KEY, dateStamp, canonicalURI)).toString()
var authorization = 'GSDATA-HMAC-SHA256 ' + 'AppKey=' + GSDataAPI_APP_ID + ', ' + 'SignedHeaders=' + signedHeaders + ', ' + 'Signature=' + signature
headers['Authorization'] = authorization
return headers
}
function callGetGSData (service, request_parameters, cb) {
var method = 'GET'
var canonicalQueryString = getQueryStr(getJsonKeySort(request_parameters))
var option = {
url: 'http://' + GSDataAPI_BaseURL + service + (canonicalQueryString.length == 0 ? '' : '?' + canonicalQueryString),
method: method,
headers: getHeaders(method, service, canonicalQueryString, {})
}
// console.log(option)
request(option, function (error, response, body) {
// console.log(body)
cb(error, response, body)
})
}
function callPostGSData (service, request_parameters, cb) {
var method = 'POST'
var parameters = getQueryStr(getJsonKeySort(request_parameters))
var headers = getHeaders(method, service, '', parameters)
headers['Content-type'] = 'application/x-www-form-urlencoded'
var option = {
url: 'http://' + GSDataAPI_BaseURL + service,
method: method,
headers: headers,
body: parameters
}
// console.log(option)
request(option, function (error, response, body) {
// console.log(body)
// console.log(JSON.stringify(response))
cb(error, response, body)
})
}
function callDeleteGSData (service, request_parameters, cb) {
var method = 'DELETE'
var parameters = getQueryStr(getJsonKeySort(request_parameters))
var headers = getHeaders(method, service, '', parameters)
headers['Content-type'] = 'application/x-www-form-urlencoded'
var option = {
url: 'http://' + GSDataAPI_BaseURL + service,
method: method,
headers: headers,
body: parameters
}
// console.log(option)
request(option, function (error, response, body) {
// console.log(body)
// console.log(JSON.stringify(response))
cb(error, response, body)
})
}
exports.callGetGSData = callGetGSData
exports.callPostGSData = callPostGSData
exports.callDeleteGSData = callDeleteGSData
~~~