微信公衆號受權登陸,提示「redirect_uri 參數錯誤」

作微信公衆號開發受權登陸的時候遇到的坑...node

後臺服務用node,index.js相關代碼以下:api

const oauth = new OAuth(conf.appid, conf.appsecret)
router.get('/wxAuthorize', async ctx => {
    const state = ctx.query.id
    redirectUrl = ctx.href
    redirectUrl = redirectUrl.replace('wxAuthorize', 'wxCallback')
    const scope = 'snsapi_userinfo' //受權類型
    const url = oauth.getAuthorizeURL(redirectUrl, state, scope)
    console.log('url:', url)
    ctx.redirect(url)
})
router.get('/wxCallback', async ctx => {
    const code = ctx.query.code // 受權碼
    console.log('wxCallback.....', code)
    const token = await oauth.getAccessToken(code)
    const accessToken = token.data.access_token
    const openid = token.data.openid
    console.log('accessToken', accessToken)
    console.log('openid', openid)
    ctx.redirect('/?openid=' + openid)
})

以前一直覺得是這裏的問題,其實不是,最後發現是微信url配置的問題。微信

配置地址:https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/indexapp

把這裏的 "http://" 去掉就能夠了。async

相關文章
相關標籤/搜索