在我的React Native应用中,我想使用Facebook登录我的应用。我正在将wordpress用作我的应用程序后端。
通过以下代码,在通过Facebook SDK成功登录后,我可以获得toekn
。我也可以通过另一个对Facebook graph api的api调用来获取facebook username, id
,但仍然无法获得用户email
。
要与我的wordpress后端进行交互,这里应该采用什么方法?我是否应该将电子邮件(我还未收到)和其他用户信息存储到wp_users
表中?
如何验证令牌?
我的示例代码:
import React, { Component } from "react";
import { View } from "react-native";
import { LoginButton, AccessToken } from "react-native-fbsdk";
class FBLoginButton extends Component {
initUser(token) {
fetch(
"https://graph.facebook.com/v2.5/me?fields=email,name,friends&access_token=" +
token
)
.then(response => response.json())
.then(json => {
console.log(json); //found only id, name
})
.catch(() => {
reject("ERROR GETTING DATA FROM FACEBOOK");
});
}
render() {
return (
<View>
<LoginButton
publishPermissions={["email"]}
onLoginFinished={(error, result) => {
if (error) {
alert("Login failed with error: " + error.message);
} else if (result.isCancelled) {
alert("Login was cancelled");
} else {
alert(
"Login was successful with permissions: " +
result.grantedPermissions
);
AccessToken.getCurrentAccessToken().then(data => {
const { accessToken } = data;
initUser(accessToken);
});
}
}}
onLogoutFinished={() => alert("User logged out")}
/>
</View>
);
}
}
export default FBLoginButton;