使用Koa获取支付宝小程序授权登录信息

下载Koa

babel

安装

npm install --save-dev babel-core babel-register babel-polyfill babel-preset-latest

配置

创建.babelrc

{
  "presets": ["latest"]
}

引用

require("babel-register");
require("babel-polyfill");          //引入这个文件babel-polyfill很重要,否则出现错误
require("./app.js");

获取登录信息

const Koa = require('koa');
const app = new Koa();
import AlipaySdk from 'alipay-sdk';
// response

let auth = () => {
  const code = 'ad300c75c7c34508ab46e71a6880NE28'
  const alipaySdk = new AlipaySdk({
    appId: '2017081908283504',
    privateKey: 'MIIEpAIB2PT6LtndH6QxhwFiwA82W******VMzJzm+v5QX8jk2mw==',
    alipayPublicKey: 'MIIBIjANB*********PHEHZ1Fm8csCZu0QIDAQAB',
  });
  alipaySdk.exec('alipay.system.oauth.token', {
    grantType: 'authorization_code',
    code: code,
    refreshToken: 'token'
  }, {
    // 验签
    validateSign: true,
    // 打印执行日志
    log: null,
  })
    .then(result => {
      console.log(result);
    })
    .catch(err => {
      console.log(err)
      // ...
    })
}

auth()
app.use(ctx => {
  ctx.body = 'Hello Koa';
  
});

app.listen(3000);

你可能感兴趣的:(使用Koa获取支付宝小程序授权登录信息)