Javascript 无法在facebook登录中获取所需的重定向uri

Javascript 无法在facebook登录中获取所需的重定向uri,javascript,node.js,reactjs,express,facebook-authentication,Javascript,Node.js,Reactjs,Express,Facebook Authentication,我正在尝试使用授权代码流在我的express/NodeJS应用程序中实现Facebook OAuth。我正在使用react facebook登录节点模块获取授权码。在我的react应用程序中,我可以成功获取授权代码。但在服务器端,我无法从Facebook API请求访问令牌,因为我收到一条错误消息“redirect_uri与您在OAuth对话框请求中使用的uri不同” 我的react应用程序中的代码 facebookLogin = async (signedRequest) => {

我正在尝试使用授权代码流在我的express/NodeJS应用程序中实现Facebook OAuth。我正在使用react facebook登录节点模块获取授权码。在我的react应用程序中,我可以成功获取授权代码。但在服务器端,我无法从Facebook API请求访问令牌,因为我收到一条错误消息“redirect_uri与您在OAuth对话框请求中使用的uri不同”

我的react应用程序中的代码

facebookLogin = async (signedRequest) => {
    return fetch('/api/auth/facebook', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ signedRequest }),
    }).then((res) => {
      if (res.ok) {
        return res.json();
      } else {
        return Promise.reject(res);
      }
    });
  };

  responseFacebook = async (response) => {
    try {
      if (response['signedRequest']) {
        const userProfile = await this.facebookLogin(response['signedRequest']);
        console.log(userProfile);
      } else {
        throw new Error(response.error);
      }
    } catch (err) {
      console.log(err);
    }
  };

render() {
    <FacebookLogin
            appId={process.env.FACEBOOK_CLIENT_ID}
            fields="name,email"
            responseType="code"
            redirectUri="http://localhost:3000/"
            callback={this.responseFacebook}
    />
facebookouth.js看起来像这样

const fetch = require('node-fetch');

const getData = async (userId, accessToken) => {
    const userData = await fetch(`https://graph.facebook.com/${userId}?fields=name,email&access_token=${accessToken}`, {
        method: 'GET'
    }).then((res) => {
        return res.json();
    }).then((userData) => {
        return userData;
    });

    return userData;
};

exports.getProfile = async (signedRequest) => {
    const decodedSignedRequest = JSON.parse(Buffer.from((signedRequest.split(".")[1]), 'base64').toString());
    const profile = await fetch(`https://graph.facebook.com/oauth/access_token?client_id=${process.env.FACEBOOK_CLIENT_ID}&redirect_uri=${encodeURIComponent('http://localhost:3000/')}&client_secret=${process.env.FACEBOOK_CLIENT_SECRET}&code=${decodedSignedRequest.code}`, {
        method: 'GET'
    }).then((res) => {
        return res.json();
    }).then((token) => {
        console.log(token);
        const userData = getData(decodedSignedRequest.user_id, token.access_token);
        return userData;
    }).catch((err) => {
        console.log(err);
        return err;
    });

    return profile;
}
我得到的是这个错误

"error": {
      message: 'Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request',
      type: 'OAuthException',
      code: 100,
      error_subcode: 36008,
      fbtrace_id: 'A-YAgSqKbzPR94XL8QjIyHn'
}
我认为问题在于我的重定向。显然,我从FacebookAuth对话框获得的重定向uri与我在服务器端传递给FacebookAPI的重定向uri不同(http://localhost:3000/)

我相信这与重定向uri的origin参数有关。初始身份验证对话框请求uri指示其原始参数值类似于“origin=localhost:3000/f370b6cb4b5a9c”。我不知道为什么要在origin参数的末尾添加某种尾随值


我试着到处找,但没有运气。任何人对此都有线索,非常感谢。

您是否使用中间件来解析正文?如果您不知道,代码可能在这里未定义

const facebookouth=require('./config/facebookouth');
//facebook oauth路线
app.post(“/api/auth/facebook”),异步(req,res)=>{
试一试{
常量代码=请求主体代码;
const profile=wait facebookouth.getProfile(代码);
控制台日志(概要文件);
res.send({profile});
}捕捉(错误){
控制台日志(err);
res.status(401.send();
}
});

是的,我尝试过这种方法,但没有成功。你确定你配置的应用程序与协议、主机和端口匹配吗?是的,我在facebook应用程序中添加了有效的oauth重定向uri。你仔细检查了你的客户端密则吗?我只是仔细检查了一下,这是正确的。而且,我在facebook应用程序仪表板中添加了有效的oauth重定向uri。仅供参考,要在react中使用存在于最终捆绑包中的环境变量,您需要在它们前面加上
react\u APP_uuu
Sure,谢谢您,如果我使用客户端的访问令牌和用户id而不是授权代码,这将完全起作用。我想知道为什么它不工作,因为我们应该使用身份验证代码,而不是从客户端访问令牌。
"error": {
      message: 'Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request',
      type: 'OAuthException',
      code: 100,
      error_subcode: 36008,
      fbtrace_id: 'A-YAgSqKbzPR94XL8QjIyHn'
}