GitHub OAuth:重定向URI不匹配

时间:2018-05-04 07:29:46

标签: github oauth

我正在尝试在我的网络应用中使用GitHub OAuth。 这是我的GitHub开发设置: enter image description here

这是我的后端回调API的代码:

const querystring = require('querystring');

const express = require('express');
const request = require('request');

var app = express();

var githubConfig = {
    client_ID: '66212aa2c8c2293b9020',
    client_Secret: 'aa91b86146ed547109aee****',

    access_token_url: 'https://github.com/login/oauth/access_token'
    user_info_url: 'https://api.github.com/user?',
    redirect_uri: 'https://saltfish666.github.io'
}

app.get("/gitblog/login",async function(req,res){

    res.set('Content-Type','text/json; charset=utf-8');
    let code = req.query.code
    console.log(code)

    await request.post(githubConfig.access_token_url, {
        form:{
            "client_id": githubConfig.client_ID,
            "client_secret": githubConfig.client_Secret,
            "code": code,
            "redirect_uri": githubConfig.redirect_uri}
        },function(error, response, body) {
            let access_token = querystring.parse(body)["access_token"]
            if(access_token == undefined){
                res.send(body)
            }
            res.redirect(302, githubConfig.redirect_uri + "?token="+access_token)
        })

})

app.listen(8099,function(){
    console.log('listening localhost:8099')
})

这是一个处理回调网址的简单代码,但是当我尝试访问

https://github.com/login/oauth/authorize?client_id=66212aa2c8c2293b9020&scope=public_repo

,GitHub的回复显示:

 error=redirect_uri_mismatch&error_description=The+redirect_uri+MUST+match+the+registered+callback+URL+for+this+application.

这只是来自github的文件:

Redirect URI mismatch

If you provide a redirect_uri that doesn't match what you've registered 
with your OAuth App, you'll receive this error message:

{
  "error": "redirect_uri_mismatch",
  "error_description": "The redirect_uri MUST match the registered callback URL for this application.",
  "error_uri": "https://developer.github.com/apps/building- 
  integrations/setting-up-and-registering-oauth-apps/troubleshooting-oauth-app-access-token-request-errors/#redirect-uri-mismatch(2)"
}

To correct this error, either provide a redirect_uri that matches what you registered or leave out this parameter to use the default one registered with your application.

我不明白,“提供与您注册的匹配的redirect_uri”是什么意思?我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以访问Git Developer

注意: 重定向URL的路径必须引用回调URL的子目录。