nodejs typescript怎么发送get、post请求,如何获取网易云通信token

nodejs typescript怎么发送get、post请求,如何获取网易云通信token

yarn add jshashes
yarn add superagent
检查语法
yarn lint
=======================

user.imToken = await setImToken(user.id);

export async function setImToken(userid: string) {
const imAppKey: string = config.get('imAppKey');
const imAppSecret: string = config.get('imAppSecret');
const imUrl: string = config.get('imUrl');
const nonce: string = Math.random().toString(36).substr(2, 15);
const timestamp: number = Date.parse(new Date().toString())/1000;
const request = require('request');
const Hashes = require('jshashes');
// 进行SHA1哈希计算,转化成16进制字符 这里用的库为jshashes
const sha1Str = imAppSecret + nonce + timestamp;
const SHA1 = new Hashes.SHA1().hex(sha1Str); const headers = {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
'AppKey': imAppKey,
'Nonce': nonce,
'CurTime': timestamp,
'CheckSum': SHA1
};
const data = {accid: userid};
let imtoken: string;
imtoken = '';
console.log('pre get:' + imUrl);
const res: superagent.Response = await remoteUrl(imUrl, {accid: userid}, headers); if (!res.error) {
console.log('do update');
console.log('userid:' + userid);
const text = JSON.parse(res.text);
console.log('code:' + text.code);
if (text.code === 200) {
const user: UserDocument | null = await UserModel.findById(userid);
if (user) {
// await user.update({imToken: 'test'});
console.log('imToken:' + text.info.token);
imtoken = text.info.token;
await user.update({imToken: text.info.token});
}
} else {
console.log('code:' + text.code);
console.log('error:' + res.text);
} }
return imtoken;
} export async function remoteUrl(url: string, data: object, headers: object) {
return new Promise<superagent.Response>((resolve, reject) => {
superagent.post(url)
.set(headers)
.send(data)
.end((err: Error, res: superagent.Response) => {
// console.log('end url:' + url);
if (!err) {
// const info = JSON.parse(res.text);
// console.log('code:' + info.code);
resolve(res);
} else{
console.log('err:' + err);
reject(err);
}
});
});
}
user.imToken = await setImToken(user.id);

export async function setImToken(userid: string) {
const imAppKey: string = config.get('imAppKey');
const imAppSecret: string = config.get('imAppSecret');
const imUrl: string = config.get('imUrl');
const nonce: string = Math.random().toString(36).substr(2, 15);
const timestamp: number = Date.parse(new Date().toString())/1000;
const request = require('request');
const Hashes = require('jshashes');
// 进行SHA1哈希计算,转化成16进制字符 这里用的库为jshashes
const sha1Str = imAppSecret + nonce + timestamp;
const SHA1 = new Hashes.SHA1().hex(sha1Str); const headers = {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
'AppKey': imAppKey,
'Nonce': nonce,
'CurTime': timestamp,
'CheckSum': SHA1
};
const data = {accid: userid};
let imtoken: string;
imtoken = '';
console.log('pre get:' + imUrl);
const res: superagent.Response = await remoteUrl(imUrl, {accid: userid}, headers); if (!res.error) {
console.log('do update');
console.log('userid:' + userid);
const text = JSON.parse(res.text);
console.log('code:' + text.code);
if (text.code === 200) {
const user: UserDocument | null = await UserModel.findById(userid);
if (user) {
// await user.update({imToken: 'test'});
console.log('imToken:' + text.info.token);
imtoken = text.info.token;
await user.update({imToken: text.info.token});
}
} else {
console.log('code:' + text.code);
console.log('error:' + res.text);
} }
return imtoken;
} export async function remoteUrl(url: string, data: object, headers: object) {
return new Promise<superagent.Response>((resolve, reject) => {
superagent.post(url)
.set(headers)
.send(data)
.end((err: Error, res: superagent.Response) => {
// console.log('end url:' + url);
if (!err) {
// const info = JSON.parse(res.text);
// console.log('code:' + info.code);
resolve(res);
} else{
console.log('err:' + err);
reject(err);
}
});
});
}

=======================

import { expect } from 'chai';
import { setImToken } from '../../app/controllers/userController';
import { connectMongoDatabase } from '../../app/db/mongo';
import config from 'config';
const mongoUri: string = config.get('mongoUri'); describe('getImtoken test', () => {
it('Should getImtoken', async () => {
// setTimeout(done, 15000);
connectMongoDatabase(mongoUri);
const rs: string = await setImToken('5b29fcca37d16537806b1bf4');
console.log('rs=' + rs);
expect(rs).equal('');
}).timeout(8000);
});

=======================

简化版的

export async function getIMToken(accid: string) {
const { AppKey, AppSecret, AppUrl } = config.get('imCfg');
const Endpoint = '/user/create.action';
const Nonce: string = Math.random().toString(36).substr(2, 15);
const CurTime: number = Math.floor(Date.now() / 1000);
const CheckSum: string = crypto.createHash('sha1').update(AppSecret + Nonce + CurTime).digest('hex'); const headers = { AppKey, Nonce, CurTime, CheckSum };
const resp = await rp.post(AppUrl + Endpoint, { form: { accid }, headers, json: true });
try {
if (resp.code === 200 && resp.info) {
return resp.info.token;
} else {
console.warn(resp);
return null;
}
} catch (e) {
console.warn(e);
return null;
}
}
---------------------
import { getIMToken } from '../../app/controllers/userController'; describe('getIMToken test', () => {
it('Should get IMToken', async () => {
const accid = '5b2a2ba4f032150023ceec46';
const token = await getIMToken(accid);
console.log(accid, token);
});
});

测试版的:

export async function getImToken(userid: string) {
const imAppKey: string = config.get('imAppKey');
const imAppSecret: string = config.get('imAppSecret');
const imUrl: string = config.get('imUrl');
const nonce: string = Math.random().toString(36).substr(2, 15);
const timestamp: number = Date.parse(new Date().toString())/1000;
const request = require('request');
const Hashes = require('jshashes');
// 进行SHA1哈希计算,转化成16进制字符 这里用的库为jshashes
const sha1Str = imAppSecret + nonce + timestamp;
const SHA1 = new Hashes.SHA1().hex(sha1Str); const options = {
// url: imUrl,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
'AppKey': imAppKey,
'Nonce': nonce,
'CurTime': timestamp,
'CheckSum': SHA1
},
form: {
accid: userid
}
};
const headers = {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
'AppKey': imAppKey,
'Nonce': nonce,
'CurTime': timestamp,
'CheckSum': SHA1
};
const data = {accid: userid};
let imtoken: string;
imtoken = '';
console.log('pre get:' + imUrl);
// const res: superagent.Response = await remoteUrl(imUrl, {accid: userid}, headers);
const response: superagent.Response = await new Promise<superagent.Response>((resolve, reject) => {
superagent.post(imUrl)
.set(headers)
.send(data)
.end((err: Error, res: superagent.Response) => {
console.log('end url:' + imUrl);
if (err) {
console.log('err:' + err);
imtoken = 'error noimtoken';
reject(err);
} else {
const info = JSON.parse(res.text);
if (info.code === 200) {
imtoken = info.info.token;
} else {
imtoken = info.desc;
}
console.log('code:' + info.code);
console.log('res:' + res.text);
resolve(res);
}
});
});
console.log('back res:' + response.text);
/*
const promise = new Promise<string>((resolve, reject) => {
console.log('promise:' + imUrl);
request(imUrl, options, (err: any, response: any, body: any ) => {
console.log('got:' + imUrl);
if (!err) {
body = body.toString();
console.log('bytes:' + body.length)
imtoken = body;
resolve(body);
} else {
console.log(err);
imtoken = 'noimtoken';
reject(err);
}
});
});
*/
/*
const _res: any = await new Promise((resolve, reject) => {
request(options, (parameters: { error: any, response: any, body: any }) => {
const {error: error, response, body} = parameters;
if (!error && response.statusCode === 200) {
const info = JSON.parse(body);
console.log(info);
imtoken = info.info.token;
// resolve(info);
} else {
// reject(error);
imtoken = 'noimtoken';
console.log(error);
}
});
});
*/
return imtoken;
} export async function remoteUrl(url: string, data: object, headers: object) {
return new Promise<superagent.Response>((resolve, reject) => {
superagent.post(url)
.set(headers)
.send(data)
.end((err: Error, res: superagent.Response) => {
console.log('end url:' + url);
if (!err) {
const info = JSON.parse(res.text);
console.log('code:' + info.code);
console.log('res:' + res.text);
resolve(res);
} else{
console.log('err:' + err);
reject(err);
}
});
});
}
上一篇:Oracle12c中性能优化&功能增强新特性之重大突破——内存列存储新特性


下一篇:Oracle12c中性能优化&功能增强新特性之全局索引DROP和TRUNCATE 分区的异步维护