[英]How do I get react-native-inappbrowser-reborn to trigger success upon successful Facebook login
I'm trying to setup a manual flow for Facebook login, as per the docs at: https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/我正在尝试根据以下文档为 Facebook 登录设置手动流程: https ://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/
I've got my test Facebook app working as expected, ie, I can login using a private web browser window fine.我的测试 Facebook 应用程序按预期工作,即,我可以使用私人网络浏览器窗口正常登录。 The URL I'm using is:我使用的网址是:
https://facebook.com/v3.3/dialog/oauth?client_id=<app_id>&display=popup&response_type=token&redirect_uri=https://www.facebook.com/connect/login_success.html
Now within my React-Native app, I'm using react-native-inappbrowser-reborn
to present a SFAuthenticationSession
on iOS.现在,在我的 React-Native 应用程序中,我使用react-native-inappbrowser-reborn
在 iOS 上呈现SFAuthenticationSession
。 As per their docs (at https://www.npmjs.com/package/react-native-inappbrowser-reborn ), I'm doing the following:根据他们的文档(位于https://www.npmjs.com/package/react-native-inappbrowser-reborn ),我正在执行以下操作:
const redirectUri = "https://www.facebook.com/connect/login_success.html"
const url = "https://facebook.com/v3.3/dialog/oauth?client_id="+appId+"&display=popup&response_type=token&redirect_uri=https://www.facebook.com/connect/login_success.html"
InAppBrowser.isAvailable()
.then(() => {
InAppBrowser.openAuth(url, redirectUri, {
// iOS Properties
dismissButtonStyle: 'cancel',
// Android Properties
showTitle: false,
enableUrlBarHiding: true,
enableDefaultShare: true,
})
.then((response) => {
// Only gets to this point if user explicitly cancels.
// So this does not trigger upon successful login.
})
// catch handlers follow
Using the above, my app correctly open up an in-app browser and I can login fine using a test user for my test app.使用上面的方法,我的应用程序正确地打开了一个应用程序内浏览器,我可以使用我的测试应用程序的测试用户正常登录。 Upon successful login though, I don't get redirected back to the .then
completion handler.但是,成功登录后,我不会被重定向回.then
完成处理程序。 It just stays in the in-app browser view and I see the same message from Facebook that I see when logging in using a web browser.它只是停留在应用程序内浏览器视图中,我看到来自 Facebook 的消息与使用网络浏览器登录时看到的消息相同。 It says something like "Success. Please treat the url the same as you would a password", or something like that.它说类似“成功。请像对待密码一样对待 url”之类的话。
I may be missing something here, but I thought the purpose of passing redirectUri
as an argument to openAuth
was so that upon redirection to that URI, the completion handler would be triggered.我可能在这里遗漏了一些东西,但我认为将redirectUri
作为参数传递给openAuth
的目的是为了在重定向到该 URI 时触发完成处理程序。
Question: How do I redirect back to the completion handler upon login success?问题:如何在登录成功后重定向回完成处理程序?
I think that you already have a solution but thought it might be useful for someone else facing this issue.我认为您已经有了解决方案,但认为它可能对面临此问题的其他人有用。 If you don't have a solution so far follow my instructions:如果到目前为止您还没有解决方案,请按照我的说明进行操作:
You can't directly redirect back to your application using deep link, since Facebook will not call a link `like myapplicationname://mycustompath´.您不能使用深层链接直接重定向回您的应用程序,因为 Facebook 不会调用“类似 myapplicationname://mycustompath”的链接。 It's only possible to call links using the https-protocol (https://...).只能使用 https 协议 (https://...) 调用链接。
The solution I'd suggest you to use is to redirect using your own API (Facebook -> Your API -> Deep Link Redirection).我建议您使用的解决方案是使用您自己的 API 进行重定向(Facebook -> Your API -> Deep Link Redirection)。 You will understand why this is required in the most of the real world applications at the end of the instructions.在说明的末尾,您将了解为什么大多数现实世界的应用程序都需要这样做。
Starting from your react-native app call the authorize endpoint of Facebook with a redirection to your application and set the global deeplink of your app as redirect uri.从您的本机反应应用程序开始,通过重定向到您的应用程序调用 Facebook 的授权端点,并将您的应用程序的全局深层链接设置为重定向 uri。
InAppBrowser.close();
InAppBrowser.openAuth("https://graph.facebook.com/oauth/authorize?client_id=YOURCLIENTID&redirect_uri=https://YOURDOMAIN:PORT/auth/facebook", "{YOURAPPSDEEPLINKNAME}://{SOMEPATHYOUWANTTOEND}")
.then((response) => {
handleAuthorized(response, LOGINTYPE.FACEBOOK);
});
Now after login you'll be redirected to your API with the authorization code token as query parameter (eg https://YOURDOMAIN:PORT/auth/facebook?code=AVERYLONGCODESENTBYFACEBOOK
)现在登录后,您将被重定向到您的 API,并使用授权代码令牌作为查询参数(例如https://YOURDOMAIN:PORT/auth/facebook?code=AVERYLONGCODESENTBYFACEBOOK
)
Using this code token from the query parameter, you make another API Call to get the access_token for the user使用查询参数中的此代码令牌,您可以进行另一个 API 调用以获取用户的 access_token
{GET}: https://graph.facebook.com/v15.0/oauth/access_token?client_id=YOUR_CLIENT_ID&redirect_uri=https://YOURDOMAIN:PORT/auth/facebook&client_secret=YOUR_CLIENT_SECRET&code=AVERYLONGCODESENTBYFACEBOOK
{GET}: https://graph.facebook.com/v15.0/oauth/access_token?client_id=YOUR_CLIENT_ID&redirect_uri=https://YOURDOMAIN:PORT/auth/facebook&client_secret=YOUR_CLIENT_SECRET&code=AVERYLONGCODESENTBYFACEBOOK
://graph.facebook.com/v15.0/oauth/access_token?client_id=YOUR_CLIENT_ID&redirect_uri=https://YOURDOMAIN:PORT/auth/facebook&client_secret=YOUR_CLIENT_SECRET&code=AVERYLONGCODESENTBYFACEBOOK
Facebook's API will send you an answer as JSON with the access_token inside. Facebook 的 API 将以 JSON 格式向您发送一个答案,其中包含 access_token。 You can make another call using the access token of the user, to get the userId and the username您可以使用用户的访问令牌进行另一个调用,以获取 userId 和用户名
{GET}: https://graph.facebook.com/me?access_token=ACCESS_TOKEN_SENT_BY_FACEBOOK_IN_PREVIOUS_GET_REQUEST
. {获取}: https://graph.facebook.com/me?access_token=ACCESS_TOKEN_SENT_BY_FACEBOOK_IN_PREVIOUS_GET_REQUEST
://graph.facebook.com/me?access_token=ACCESS_TOKEN_SENT_BY_FACEBOOK_IN_PREVIOUS_GET_REQUEST。
If you need the e-mail address for the user you have to make another call.如果您需要用户的电子邮件地址,您必须再打一个电话。 Make sure you'd set the permission to read the e-mail address for your app on the developer portal of facebook.确保您已在 facebook 的开发人员门户上设置读取您的应用程序的电子邮件地址的权限。 The following request will return you the id, name and the email of the user以下请求将返回用户的 ID、姓名和电子邮件
{GET}: https://graph.facebook.com/USERIDFROMPREVIOUSREQUEST?fields=id,name,email&access_token=ACCESSTOKEN
{获取}: https://graph.facebook.com/USERIDFROMPREVIOUSREQUEST?fields=id,name,email&access_token=ACCESSTOKEN
://graph.facebook.com/USERIDFROMPREVIOUSREQUEST?fields=id,name,email&access_token=ACCESSTOKEN
I think you want to save all these information to a database and create a session in order to keep the user logged in and therefore all the requests described will be useful for you in a real application.我认为您想将所有这些信息保存到数据库并创建一个会话以使用户保持登录状态,因此描述的所有请求都将在实际应用程序中对您有用。
After doing all the backend stuff, you're ready for the redirection using deep link.完成所有后端工作后,您就可以使用深度链接进行重定向了。 To do that, set a meta-tag to redirect the inappbrowser to your application:为此,请设置元标记以将 inappbrowser 重定向到您的应用程序:
<meta http-equiv="refresh" content="0; URL={YOURAPPSDEEPLINKNAME}://{SOMEPATHYOUWANTTOEND}" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.