node.js - Facebook:URL 被阻止此重定向失败,因为重定向 URI 未在应用程序的客户端 OAuth 设置中列入白名单
问题描述
最近我在我的网站上使用了 Facebook 登录选项。我已经编写了所有需要的 API,并在使用“ localhost ”作为域时对它们进行了彻底的测试。在 Facebook 开发者帐户中配置我的 APP 中的设置时,我已经设置了所有必要的设置,例如提供 Oauth 重定向 URL、在基本设置中添加域名等。那时一切都很好。因此,我请求了所需的应用权限,例如 pages_manage_posts、pages_read_enagagment、pages_show_list 并申请了它们。Facebook 在应用审查中批准了他们。我在 Facebook 中使用的重定向 URL(“https://execute.app/#/socialmedia/management/”)正确放入 Facebook Oauth 重定向 URL 路径,如下图所示。
我已经将服务器端 API 用于 Facebook 登录和图形 API。我使用 Oauth2 进行 Facebook 登录。你可以看到下面的代码
var OAuth2 = require('oauth').OAuth2;
var oauth2 = new OAuth2(CONSTANTS.FB_APP_Key,
CONSTANTS.FB_APP_Secret,
"", "https://www.facebook.com/dialog/oauth",
"https://graph.facebook.com/oauth/access_token",
null);
app.get('/api/document/facebook/auth', function (req, res) {
var redirect_uri = "https://execute.app/#/socialmedia/management/";
console.log("redirect_uri ", redirect_uri);
var params = { 'redirect_uri': redirect_uri, 'scope': 'email,public_profile,pages_manage_posts,pages_show_list,pages_read_engagement' };
var authUrl = oauth2.getAuthorizeUrl(params);
res.send({
"status": true,
"message": "login url generated successfully",
"url": authUrl
});
});
Scenario-1: When there is and existing active Facebook session in browser i.e, when some user is already logged into Facebook in facebook.com or developers.facebook.com and when we try to login into Facebook from our website, Oauth Authentication API gets called and returns Facebook login URL with status code 200 and the url gets opened in a new tab, its works fine, we don't need to enter Facebook login credentials again, we can just click on "**Continue as USER**" button and then we get the login code, with which we can get user access token. After getting token everything works as planned.
Scenario-2: But if no user is already logged into Facebook in browser and when I click on **login to Facebook** button, API call is made and it returns login URL, but the response status code sent by Oauth login API is 304. A new Facebook login tab is opened, but there is a warning displaying a message saying "URL blocked.
此重定向失败,因为重定向 URI 未在应用的客户端 OAuth 设置中列入白名单。确保客户端和 Web OAuth 登录已打开,并将您的所有应用程序域添加为有效的 OAuth 重定向 URI。”
但是您可以看到我已经在 Facebook 中添加了正确的重定向 URL。如上所述,它适用于场景 1,但不适用于其他场景。
注意:无论状态码是 200 还是 304,Oauth Authentication API 返回的 Facebook 登录 URL 都是一样的。它是“ https://www.facebook.com/dialog/oauth?redirect_uri=https%3A%2F%2Fexecute.app%2F%23%2Fsocialmedia%2Fmanagement%2F&scope=email%2Cpublic_profile%2Cpages_manage_posts%2Cpages_show_list%2Cpages_read_engagement&client_id= 88XXXXXXX663"
请帮我解决这个问题,在此先感谢
解决方案
OAuth RFC声明重定向 URI :
端点 URI 不得包含片段组件。
它可能是 Facebook 中的一个错误,它适用于某些场景而不适用于其他场景,但实际上最好避免使用带有片段组件的 URI。如果 Facebook 的文档声明您可以使用带有片段的重定向 URI,我会尝试联系他们询问为什么这在某些情况下不起作用。
推荐阅读
- app-store-connect - App Store Connect:我是否需要创建另一个应用程序以通过 TestFlight 共享具有不同捆绑 ID 的 beta 版本?
- gitlab-ci - 使用 gitlab ci 添加魅力趋势
- python-3.x - 访问 Azure Auto ML 的 Azure 数据存储时出现 SSL 错误
- simulation - 多重插补的 Ancova 模拟
- python - 使用 Python 和 Pandas 访问 JSON 元素
- apache-spark - 基于数据框条件在 Spark 中创建自定义计数器
- python - C4.5 决策树的 ROC 曲线
- batch-file - 如何运行 localhost 服务器然后在一个批处理文件中打开浏览器?
- javascript - 创建新的唯一值集,但从同一记录中复制另一个值以创建新的对象数组
- javascript - Leaflet js在本地主机上工作,但从硬盘打开时不起作用