我试图通过facebook sdk获取信息,但到目前为止,我只得到了用户的ID和名称。我确实已经批准了预授权,但电子邮件仍然像姓名和 ID 一样弹出。我几乎什么都试过了。
任何帮助将不胜感激。
提前致谢
import React, { Component } from 'react'
import { View, TouchableOpacity, Text } from 'react-native';
import { LoginButton, AccessToken, LoginManager, GraphRequest,
GraphRequestManager } from 'react-native-fbsdk';
export default class App extends Component {
constructor() {
super();
this._fbAuth = this._fbAuth.bind(this);
}
_fbAuth = () => {
var that = this;
LoginManager.logInWithReadPermissions(['public_profile',
'email']).then(function (result) {
if (result.isCancelled) {
console.log("Login Cancel");
alert("Login Cancel")
//debugger
}
else {
console.log("Login AccessToken");
alert("Login AccessToken");
AccessToken.getCurrentAccessToken().then(
(data) => {
// alert("Get AccessToken");
//debugger
let accessToken = data.accessToken;
alert(accessToken.toString());
const responseInfoCallback = (error, result) => {
setTimeout(() => {
if (error) {
alert('Errorrrrrrrrr');
}
else {
if (result.email == undefined) {
alert("Error ,Email address is required");
// stuck at email. result.name and result.id gives correct results
}
else {
alert(result.email);
}
}
}, 200
);
}
const infoRequest = new GraphRequest(
'/me',
{
accessToken: accessToken,
prameters: {
fields: {
string: 'email , name,first_name, middle_name, last_name'
}
}
},
responseInfoCallback
);
new GraphRequestManager().addRequest(infoRequest).start();
})
}
},
function (error) {
console.log("some error occured!!", error);
})
}
render() {
return (
<View>
<TouchableOpacity onPress={() => this._fbAuth()}>
<Text> Press me</Text>
</TouchableOpacity>
</View>
);
}
}
result.email 不应该是未定义的。
您可以使用fbsdk
提供的accessToken
。只需获取accessToken
,一旦收到accessToken
,就可以向facebook图形api发出请求,以使用accessToken
接收与该用户相关的数据。
https://developers.facebook.com/docs/graph-api/using-graph-api
这是完整的文档,它还包括访问令牌。它会帮助你。我也这样做。