如何使用图形API与react-native-fbsdk? [英] How to use graph API with react-native-fbsdk?
问题描述
我在
I也不知道如何得到结果。
但是,当我尝试修改'\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 = {()=>警报( 注销。)} />
但是它没有打印出来什么错误,只是对象Object。
所以,问题是我不明白Facebook提供的示例代码没有解释。
这是我的问题,我真的需要你帮助我:
首先,请检查 javascript代码
如何在 react-native-fbsdk 中使用图形API 来检索一些用户信息(例如:全名)并成功显示它(使用警报)?
GraphRequest()中的每个参数?
_responseInfoCallback 中的错误对象和结果对象的结构是什么?
解决方案
感谢@Samuel答案,我已更新我的代码
testRequestGraphAPI:function(){
const infoRequest = new GraphRequest(
'/ me',
{
参数:{
字段:{
string:'email,name,first_name,middle_name,last_name'//你想要获得
},
access_token:{
string: meow_accesstoken.toString()//将你的accessToken放在这里
}
}
},
this._responseInfoCallback //确保你在同一个类
中定义_responseInfoCallback;
new GraphRequestManager()。addRequest(infoRequest).start();
}
而回调
< pre class =lang-js prettyprint-override>
_responseInfoCallback:function(error:?Object,result:?Object){
alert(meow response);
if(error){
alert('提取数据时出错:'+ error.toString());
console.log(Object.keys(error)); //打印所有枚举
console.log(error.errorMessage); // print error message
// error.toString()在这种情况下将无法正常工作
//所以让使用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); //打印JSON
}
}
*注意:对于console.log (),您需要使用远程调试JS,然后打开Chrome开发人员工具查看日志。
不幸的是, -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()然后(
(data)=> {
let accessToken = data.accessToken
alert accessToken.toString())
const responseInfoCallback =(error,result)=> {
if(error){
console.log(error)
alert ('提取数据时出错'+ error.toString());
} else {
console.log(result)
alert('Success fetching data:'+ result.toString() );
}
}
const infoRequest = new GraphRequest(
'/ me',
{
accessToken:accessToken,
参数:{
fields:{
string:'email,name,first_name,middle_name,last_name'
}
}
},
responseInfoCallback
);
//启动图表请求。
new GraphRequestManager()。addRequest(infoRequest).start()
}
)
}
}
}
onLogoutFinished = {()=>警报( 注销。)} />
您需要启用远程JS调试来查看 console.log()
info。
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
I read the document, both on github and Facebook developers docs.
There is only sample, nothing more. No API document.
The code to make a Graph API request is
const infoRequest = new GraphRequest(
'/me',
null,
this._responseInfoCallback,
);
And the callback
_responseInfoCallback(error: ?Object, result: ?Object) {
if (error) {
alert('Error fetching data: ' + error.toString());
} else {
alert('Success fetching data: ' + result.toString());
}
}
And here is the function to make a Graph API request
testRequestGraphAPI(){
const infoRequest = new GraphRequest(
'/me',
null,
this._responseInfoCallback,
);
new GraphRequestManager().addRequest(infoRequest).start();
}
However, I can't find any further document. I have no idea what each parameters do.
The result for these codes above is this.
I also don't know how to get the result.
However, when I try to modify '\me' to 'me?fields=id,name', It failed. Although I have asked for permission
<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.")}/>
But it does not print out what error, just object Object.
So, the problem is that I don't understand the sample code which Facebook provide with no explanation.
Here is my question that I really need you help me:
First at all, please check the javascript code that I currently looking at?
How to use graph API in react-native-fbsdk to retrieve some user information (example: full name) and successfully display it (use alert) ?
What each parameters in GraphRequest() do ?
What is the structure of error object and result object in _responseInfoCallback ?
SOLUTION
Thanks to @Samuel answer, I have updated my code
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();
}
And the callback
_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
}
}
*Note: For console.log(), you need to use "Debug JS remotely" then open Chrome developer tools to see the log.
Unfortunately the react-native-fbsdk documentation is not updated and the examples do not work well.
I got the same problem and I solved it by try and error.
To solve your problem you'll need to change your GraphRequest adding params and fields to it like this:
<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.")}/>
You'll need to enable the Remote JS Debug to see the console.log()
info.
https://facebook.github.io/react-native/docs/debugging.html
And probably you need to get some permissions to get more info than names and email so it's a good idea to look the Facebook Graph API Documentation: https://developers.facebook.com/docs/graph-api/overview/
Reference:
https://github.com/facebook/react-native-fbsdk/issues/105#issuecomment-206501550
这篇关于如何使用图形API与react-native-fbsdk?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!