gpt4 book ai didi

javascript - React Native FBSDK 处理身份验证拒绝

转载 作者:行者123 更新时间:2023-11-28 21:03:20 25 4
gpt4 key购买 nike

我需要在 react-native 中处理 Facebook 登录错误。

在我的应用程序全新安装后尝试登录会向用户显示弹出窗口:

"Facebook. Application would like to access your basic profile info and list of friends."

有两个选项:“不允许”和“确定”。在正常流程中,我得到了一个合适的 token 。但是,如果用户在 onLoginFinished 中拒绝,我会得到以下结果:

{
"isCancelled": true,
"grantedPermissions": null,
"declinedPermissions": null
}

没有关于取消原因的信息。拒绝是使用 'system_account' 获得 "isCancelled": true 的唯一原因吗?

之后,所有连续尝试使用 'system_account' 登录都立即失败,并出现以下错误:

{
"code": "FacebookSDK",
"domain": "com.facebook.sdk.login",
"framesToPop": 1,
"nativeStackIOS": [...],
"userInfo": {
"NSLocalizedDescription": "Access has not been granted to the Facebook account. Verify device settings.",
"com.facebook.sdk:FBSDKErrorLocalizedDescriptionKey": "Access has not been granted to the Facebook account. Verify device settings."
}
}

它给了我可以向用户显示的错误,但由于本地化,我无法可靠地解析它。所以问题是我如何才能可靠地检测到用户何时未授予权限?

我的代码:

import React from 'react'
import {AccessToken, LoginManager} from 'react-native-fbsdk'

class Login extends React.Component {
login = ()=>{
LoginManager.setLoginBehavior('system_account')
LoginManager.logInWithReadPermissions()
.then(this.onLoginFinished)
.catch(this.onLoginError)
}
onLoginFinished = (result)=>{
if(result.isCancelled) {
//
} else {
AccessToken.getCurrentAccessToken().then((data)=>{
this.props.onLogin(data.accessToken.toString())
})
}
}
onLoginError = (error)=>{
// Want to handle this case but
//can only show a message to the user =(
}
render() {
return (
<View>
<TouchableHighlight onPress={this.login}>
<View><Text>Facebook</Text></View>
</TouchableHighlight>
</View>
)
}
}

据我所知,我只能在收到 'system_account' 的取消后切换到 'native' 登录行为并单独处理它的行为是吗唯一的办法?

最佳答案

使用 Facebook 登录时,有几种情况需要处理。

  1. 用户授予所有权限并登录
  2. 用户授予了一些权限并登录
  3. 用户未授予任何权限并已登录
  4. 用户取消登录尝试

如果你勾选LoginManager.logInWithReadPermissions它使用 native 代码(适用于 iOS) FBSDKLoginManagerLoginResult来自 iOS SDK。在 documentation of iOS SDK isCancelled 是一个 BOOL,它显示“登录是否被用户取消。”

因此,如果您返回的结果中的isCanceled 为真,则表示用户已手动取消登录请求。这可能是因为用户改变了主意或误按了按钮,或者只是因为当他/她看到您要求的权限列表等时改变了主意。当您检查并看到 isCanceled 值为 true 那么您可以假设用户不想使用 facebook 登录,您可以这样继续 (重定向到另一个登录方法或显示用户应该登录的弹出窗口等).

如果用户完成 facebook 登录但未选中某些权限请求(如生日或喜欢等),权限将显示在 declinedPermissions 中。您可以检查此属性的 lenght 并根据需要进行处理。您可以创建一个对象,其中包含在任何权限被拒绝时需要显示的所有错误消息。

示例

const permissions = [
{
permissionName: 'user_likes',
errorMessage: 'To give you better results we need your likes'
},
{
permissionName: 'user_birthday',
errorMessage: 'Would you like to receive a gift from us on your birthday?'
},
{
permissionName: 'user_friends',
errorMessage: 'We can\'t match you without knowing your friends'
}
];



// ....
login = ()=>{
LoginManager.setLoginBehavior('system_account')
LoginManager.logInWithReadPermissions(permissions.map((permission) => (permission.permissionName)))
.then(this.onLoginFinished)
.catch(this.onLoginError)
}

onLoginFinished = (result)=>{
if(result.isCancelled) {
// user canceled login manually
} else {
if (result.declinedPermissions && result.declinedPermissions.lenght > 0) {
// handle declined permissions
result.declinedPermissions.forEach((declinedPermission) => {
// match the declinedPermission with the permission object
}
} else {
// user gave all the permissions
}
AccessToken.getCurrentAccessToken().then((data)=>{
this.props.onLogin(data.accessToken.toString())
})
}
}

关于javascript - React Native FBSDK 处理身份验证拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47105862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com