我在github和Facebook developers docs上阅读了文档。
只有样本,没有更多。无API文档。
发出Graph API请求的代码是
const infoRequest = new GraphRequest(
'/me',
null,
this._responseInfoCallback,
);
和回调
_responseInfoCallback(error: ?Object, result: ?Object) {
if (error) {
alert('Error fetching data: ' + error.toString());
} else {
alert('Success fetching data: ' + result.toString());
}
}
下面是发出Graph API请求的函数
testRequestGraphAPI(){
const infoRequest = new GraphRequest(
'/me',
null,
this._responseInfoCallback,
);
new GraphRequestManager().addRequest(infoRequest).start();
}
但是,我找不到任何进一步的文档。我不知道每个参数是做什么的。
上述代码的结果如下所示。
我也不知道如何得到结果。
但是,当我尝试将'\me‘修改为'me?fields=id,name’时,它失败了。尽管我已经请求允许
<LoginButton
publishPermissions={["publish_actions,user_birthday, user_religion_politics, user_relationships, user_relationship_details, user_hometown, user_location, user_likes, user_education_history, user_work_history, user_website, user_managed_groups, user_events, user_photos, user_videos, user_friends, user_about_me, user_status, user_games_activity, user_tagged_places, user_posts, user_actions.video, user_actions.news, user_actions.books, user_actions.music, user_actions.fitness, public_profile, basic_info"]}
onLoginFinished={
(error, result) => {
if (error) {
alert("login has error: " + result.error);
} else if (result.isCancelled) {
alert("login is cancelled.");
} else {
AccessToken.getCurrentAccessToken().then(
(data) => {
meow_accesstoken = data.accessToken
alert(meow_accesstoken.toString())
}
)
}
}
}
onLogoutFinished={() => alert("logout.")}/>
但它并没有打印出什么错误,只是对象对象。
所以,问题是我不理解Facebook提供的没有解释的示例代码。
这是我的问题,我真的需要你的帮助:
首先,请检查一下我当前正在查看的javascript code?
如何在react-native-fbsdk中使用图形接口来检索一些用户信息警报(示例:)并成功显示 it (使用警报)?
GraphRequest()中的每个参数都有什么作用?
_responseInfoCallback中的error对象和result对象的结构是什么?
解决方案
感谢@Samuel answer,我已经更新了我的代码
testRequestGraphAPI: function(){
const infoRequest = new GraphRequest(
'/me',
{
parameters: {
fields: {
string: 'email,name,first_name,middle_name,last_name' // what you want to get
},
access_token: {
string: meow_accesstoken.toString() // put your accessToken here
}
}
},
this._responseInfoCallback // make sure you define _responseInfoCallback in same class
);
new GraphRequestManager().addRequest(infoRequest).start();
}
和回调
_responseInfoCallback: function(error: ?Object, result: ?Object) {
alert("meow response");
if (error) {
alert('Error fetching data: ' + error.toString());
console.log(Object.keys(error));// print all enumerable
console.log(error.errorMessage); // print error message
// error.toString() will not work correctly in this case
// so let use JSON.stringify()
meow_json = JSON.stringify(error); // error object => json
console.log(meow_json); // print JSON
} else {
alert('Success fetching data: ' + result.toString());
console.log(Object.keys(result));
meow_json = JSON.stringify(result); // result => JSON
console.log(meow_json); // print JSON
}
}
*注意:对于console.log(),您需要使用[远程调试JS ],然后打开Chrome开发者工具查看日志。
发布于 2016-05-24 08:01:54
不幸的是,react-native-fbsdk文档没有更新,示例也不能很好地工作。
我得到了同样的问题,我通过尝试和错误解决了它。
要解决您的问题,您需要更改您的GraphRequest,将参数和字段添加到其中,如下所示:
<LoginButton
onLoginFinished={
(error, result) => {
if (error) {
alert("login has error: " + result.error);
} else if (result.isCancelled) {
alert("login is cancelled.");
} else {
AccessToken.getCurrentAccessToken().then(
(data) => {
let accessToken = data.accessToken
alert(accessToken.toString())
const responseInfoCallback = (error, result) => {
if (error) {
console.log(error)
alert('Error fetching data: ' + error.toString());
} else {
console.log(result)
alert('Success fetching data: ' + result.toString());
}
}
const infoRequest = new GraphRequest(
'/me',
{
accessToken: accessToken,
parameters: {
fields: {
string: 'email,name,first_name,middle_name,last_name'
}
}
},
responseInfoCallback
);
// Start the graph request.
new GraphRequestManager().addRequest(infoRequest).start()
}
)
}
}
}
onLogoutFinished={() => alert("logout.")}/>
您需要启用Remote JS调试才能查看console.log()
信息。https://facebook.github.io/react-native/docs/debugging.html
您可能需要获得一些权限才能获得比姓名和电子邮件更多的信息,因此查看Facebook Graph API文档是个好主意:https://developers.facebook.com/docs/graph-api/overview/
参考资料:
https://github.com/facebook/react-native-fbsdk/issues/105#issuecomment-206501550
发布于 2017-05-04 21:15:34
下面是一个自定义按钮的示例,如果您想创建一个按钮的话:)
FbLoginButton() {
LoginManager
.logInWithReadPermissions(['public_profile'])
.then(function (result) {
if (result.isCancelled) {
alert('Login cancelled');
} else {
AccessToken
.getCurrentAccessToken()
.then((data) => {
let accessToken = data.accessToken
alert(accessToken.toString())
const responseInfoCallback = (error, result) => {
if (error) {
console.log(error)
alert('Error fetching data: ' + error.toString());
} else {
console.log(result)
alert('Success fetching data: ' + result.toString());
}
}
const infoRequest = new GraphRequest('/me', {
accessToken: accessToken,
parameters: {
fields: {
string: 'email,name,first_name,middle_name,last_name'
}
}
}, responseInfoCallback);
// Start the graph request.
new GraphRequestManager()
.addRequest(infoRequest)
.start()
})
}
}, function (error) {
alert('Login fail with error: ' + error);
});
}
发布于 2018-08-30 23:35:34
谢谢你@Samuel。
多亏了你的帮助,我终于成功地从Facebook登录中获得了用户信息!
但我努力弄清楚如何从结果对象中获取用户名和电子邮件,因为我是React & Javascript的新手。
P.S.结果“name”是点,因为它是object!!
所以我在你的代码中添加了一些代码,供其他像我一样的人使用。
如果你不喜欢使用你的代码,那就告诉我。
<LoginButton
onLoginFinished={
(error, result) => {
if (error) {
alert("login has error: " + result.error);
} else if (result.isCancelled) {
alert("login is cancelled.");
} else {
AccessToken.getCurrentAccessToken().then(
(data) => {
let accessToken = data.accessToken
alert(accessToken.toString())
const responseInfoCallback = (error, result) => {
if (error) {
console.log(error)
alert('Error fetching data: ' + error.toString());
} else {
console.log(result)
// Here's my code
alert('Success fetching data: ' + result["name"].toString() +
", " + result["email"].toString());
/*
if(your DB already got this email or something unique) {
// SignIn()
}
// when your DB doesn't have this email
else {
// Do signUp() with this infomation and SignIn()
}
*/
}
}
const infoRequest = new GraphRequest(
'/me',
{
accessToken: accessToken,
parameters: {
fields: {
string: 'email,name,first_name,middle_name,last_name'
}
}
},
responseInfoCallback
);
// Start the graph request.
new GraphRequestManager().addRequest(infoRequest).start()
}
)
}
}
}
onLogoutFinished={() => alert("logout.")}/>
https://stackoverflow.com/questions/37383888
复制相似问题