获取云开发调用凭证
微信官方文档
auth.getAccessToken
- 安装
axios
npm install axios --save
- 创建数据库操作接口文件
databaseapi.js
const axios = require('axios')
const qs = require('querystring')
const result = require('./handle.js')
let url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET'
class getToken {
constructor () {
}
async gettoken() {
try {
let token = await axios.get(url)
console.log(token)
if (token.status === 200) {
return token.data.access_token
} else {
throw '获取 token 错误'
}
} catch (e) {
console.log(e)
throw new result(e, 500)
}
}
}
module.exports = {getToken}
- 调用数据库操作api接口
const {getToken} = require('../../config/databaseapi.js')
new getToken().gettoken()