React Native:Facebook 权限总是被拒绝
React Native: Facebook permissions are always declined
所以我尝试使用 redux
和 react-native-fbsdk
包来登录用户,但不管我怎么做,权限总是被拒绝,即使在授予它们之后登录屏幕。控制台日志如下所示:
在这里你可以看到我对 authFailure 的操作:
export function authFailure(authError) {
return {
type: AUTH_FAILURE,
action.authError.message
}
}
这是在 onPress 上执行的函数,它调度操作 authStarted()
,然后调用处理 fbsdk 的函数 _fbAuthAPI()
。这可以在下面看到。
export function _fbAuth() {
return (dispatch) => {
dispatch(authStarted())
const values = []
_fbAuthAPI().then((result) => {
values.concat(result.accessToken)
return _getUserInformationAPI(result.accessToken)
}).then((profile) => {
values.concat(profile)
dispatch(authSuccess(...values))
}).catch((error) => {
dispatch(authFailure(error))
setTimeout(() => {
dispatch(authFailureRemove())
}, 4000)
})
}
}
export function _fbAuthAPI() {
return new Promise((resolve, reject) => {
LoginManager.logInWithReadPermissions(['public_profile', 'email']).then((result) => {
if (result.isCancelled) {
throw new Error('Login was cancelled')
} else if (result.declinedPermissions) {
throw new Error('Permissions were declined')
} else {
return AccessToken.getCurrentAccessToken()
}
}).then((result) => {
resolve(result)
}).catch((error) => {
reject(error)
})
})
}
关于减速器:
export default function authReducer(state = initialState, action) {
switch (action.type) {
case AUTH_STARTED:
return Object.assign({}, state, {
authenticating: true
})
break
case AUTH_SUCCESS:
return Object.assign({}, state, {
authenticating: false,
authError: false,
facebookToken: facebookToken,
facebookProfile: facebookProfile
})
break
case AUTH_FAILURE:
return Object.assign({}, state, {
authenticating: false,
authError: authError
})
break
...
default:
return state
}
}
设置:
- React Native 0.45.1
- React Native FBSDK“^0.6.1”
- Redux“^3.7.1”
- MacOS 塞拉利昂 10.12.6
我无法帮助 Facebook SDK 和身份验证,但未定义 authError 是因为在本节中,authError 确实未定义
case AUTH_FAILURE:
return Object.assign({}, state, {
authenticating: false,
authError: authError // Where is this coming from?
})
break
我想你想要的是authError: action.authError
好的,所以我设法解决了这个问题。
基本上,当您对 logInWithReadPermissions
的结果调用 declinedPermissions
时,它总是 returns true,因为它是一个数组。然后,即使您没有任何被拒绝的权限,它也需要这是真的。
绕过它的简单方法就是查看数组中的内容并根据它确定要做什么:
// Returns an empty array, therefore evaluates to true
if (result.declinedPermissions) {
throw new Error('Permissions were declined')
}
// The first index of the array is empty
if (result.declinedPermissions[0] === "") {
throw new Error('Permissions were declined')
}
所以我尝试使用 redux
和 react-native-fbsdk
包来登录用户,但不管我怎么做,权限总是被拒绝,即使在授予它们之后登录屏幕。控制台日志如下所示:
在这里你可以看到我对 authFailure 的操作:
export function authFailure(authError) {
return {
type: AUTH_FAILURE,
action.authError.message
}
}
这是在 onPress 上执行的函数,它调度操作 authStarted()
,然后调用处理 fbsdk 的函数 _fbAuthAPI()
。这可以在下面看到。
export function _fbAuth() {
return (dispatch) => {
dispatch(authStarted())
const values = []
_fbAuthAPI().then((result) => {
values.concat(result.accessToken)
return _getUserInformationAPI(result.accessToken)
}).then((profile) => {
values.concat(profile)
dispatch(authSuccess(...values))
}).catch((error) => {
dispatch(authFailure(error))
setTimeout(() => {
dispatch(authFailureRemove())
}, 4000)
})
}
}
export function _fbAuthAPI() {
return new Promise((resolve, reject) => {
LoginManager.logInWithReadPermissions(['public_profile', 'email']).then((result) => {
if (result.isCancelled) {
throw new Error('Login was cancelled')
} else if (result.declinedPermissions) {
throw new Error('Permissions were declined')
} else {
return AccessToken.getCurrentAccessToken()
}
}).then((result) => {
resolve(result)
}).catch((error) => {
reject(error)
})
})
}
关于减速器:
export default function authReducer(state = initialState, action) {
switch (action.type) {
case AUTH_STARTED:
return Object.assign({}, state, {
authenticating: true
})
break
case AUTH_SUCCESS:
return Object.assign({}, state, {
authenticating: false,
authError: false,
facebookToken: facebookToken,
facebookProfile: facebookProfile
})
break
case AUTH_FAILURE:
return Object.assign({}, state, {
authenticating: false,
authError: authError
})
break
...
default:
return state
}
}
设置:
- React Native 0.45.1
- React Native FBSDK“^0.6.1”
- Redux“^3.7.1”
- MacOS 塞拉利昂 10.12.6
我无法帮助 Facebook SDK 和身份验证,但未定义 authError 是因为在本节中,authError 确实未定义
case AUTH_FAILURE:
return Object.assign({}, state, {
authenticating: false,
authError: authError // Where is this coming from?
})
break
我想你想要的是authError: action.authError
好的,所以我设法解决了这个问题。
基本上,当您对 logInWithReadPermissions
的结果调用 declinedPermissions
时,它总是 returns true,因为它是一个数组。然后,即使您没有任何被拒绝的权限,它也需要这是真的。
绕过它的简单方法就是查看数组中的内容并根据它确定要做什么:
// Returns an empty array, therefore evaluates to true
if (result.declinedPermissions) {
throw new Error('Permissions were declined')
}
// The first index of the array is empty
if (result.declinedPermissions[0] === "") {
throw new Error('Permissions were declined')
}