Facebook graph api 如何将图形API与react native fbsdk一起使用?
我读了文件,无论是在网上还是在网上Facebook graph api 如何将图形API与react native fbsdk一起使用?,facebook-graph-api,react-native,Facebook Graph Api,React Native,我读了文件,无论是在网上还是在网上 只有样品,没有更多。没有API文档 发出图形API请求的代码是 const infoRequest=new GraphRequest( “/我”, 无效的 这个._responseInfo回调, ); 以及回调 \u responseInfo回调(错误:对象,结果:对象){ 如果(错误){ 警报('获取数据时出错:'+Error.toString()); }否则{ 警报('成功获取数据:'+result.toString()); } } 下面是生成图形AP
只有样品,没有更多。没有API文档
发出图形API请求的代码是
const infoRequest=new GraphRequest(
“/我”,
无效的
这个._responseInfo回调,
);
以及回调\u responseInfo回调(错误:对象,结果:对象){
如果(错误){
警报('获取数据时出错:'+Error.toString());
}否则{
警报('成功获取数据:'+result.toString());
}
}
下面是生成图形API请求的函数testRequestGraphAPI(){
const infoRequest=新GraphRequest(
“/我”,
无效的
这个._responseInfo回调,
);
新建GraphRequestManager().addRequest(infoRequest).start();
}
然而,我找不到任何进一步的文件。我不知道每个参数的作用上述代码的结果如下。
我也不知道如何得到结果 但是,当我尝试将“\me”修改为“me”fields=id,name”时,失败了。 虽然我已经请求允许了
{
如果(错误){
警报(“登录有错误:+result.error”);
}否则如果(结果被取消){
警报(“登录被取消”);
}否则{
AccessToken.getCurrentAccessToken()。然后(
(数据)=>{
meow_accesstoken=data.accesstoken
警报(meow\u accesstoken.toString())
}
)
}
}
}
onLogoutFinished={()=>警报(“注销”)}/>
但它并没有打印出什么错误,只是对象 所以,问题是我不理解Facebook提供的示例代码,没有任何解释。
这是我的问题,我真的需要你帮助我:
首先,请检查我目前正在查看的
如何在react native fbsdk中使用图形API检索一些用户信息(例如:全名)并成功显示它(使用警报)
GraphRequest()中的每个参数的作用是什么
\u responseinfo callback中的错误对象和结果对象的结构是什么 解决方案
感谢@Samuel answer,我已经更新了我的代码
testRequestGraphAPI:function(){
const infoRequest=新GraphRequest(
“/我”,
{
参数:{
字段:{
字符串:“电子邮件、姓名、名字、中间名、姓氏”//您想要得到什么
},
访问令牌:{
string:meow\u accesstoken.toString()//将accesstoken放在这里
}
}
},
这是.\u responseInfo回调//确保在同一个类中定义了\u responseInfo回调
);
新建GraphRequestManager().addRequest(infoRequest).start();
}
回拨电话呢
\u responseInfo回调:函数(错误:对象,结果:对象){
警惕(“喵喵回应”);
如果(错误){
警报('获取数据时出错:'+Error.toString());
console.log(Object.keys(error));//打印所有可枚举项
console.log(error.errorMessage);//打印错误消息
//错误。在这种情况下,toString()将无法正常工作
//所以让我们使用JSON.stringify()
meow_json=json.stringify(error);//error object=>json
console.log(meow_json);//打印json
}否则{
警报('成功获取数据:'+result.toString());
console.log(Object.keys(result));
meow_json=json.stringify(result);//result=>json
console.log(meow_json);//打印json
}
}
*注意:对于console.log(),您需要使用“远程调试JS”,然后打开Chrome developer tools查看日志。不幸的是,react原生fbsdk文档没有更新,示例也不能正常工作 我也遇到了同样的问题,我通过反复尝试解决了它 要解决您的问题,您需要更改GraphRequest,向其中添加参数和字段,如下所示:
{
如果(错误){
警报(“登录有错误:+result.error”);
}否则如果(结果被取消){
警报(“登录被取消”);
}否则{
AccessToken.getCurrentAccessToken()。然后(
(数据)=>{
让accessToken=data.accessToken
警报(accessToken.toString())
const responseInfo回调=(错误,结果)=>{
如果(错误){
console.log(错误)
警报('获取数据时出错:'+Error.toString());
}否则{
console.log(结果)
警报('成功获取数据:'+result.toString());
}
}
const infoRequest=新GraphRequest(
“/我”,
{
accessToken:accessToken,
参数:{
字段:{
字符串:“电子邮件、姓名、名、中名、姓”
}
}
},
响应信息回调
);
//启动图形请求。
新建GraphRequestManager().addRequest(infoRequest).start()
}
)
}
}
}
onLogoutFinished={()=>警报(“注销”)}/>
您需要启用远程JS调试以查看控制台.log()
信息。
您可能需要获得一些权限才能获得比姓名和电子邮件更多的信息,因此最好查看Facebook Graph API文档:
参考:
如果您想制作一个自定义按钮,这里有一个自定义按钮的示例:) 谢谢你,塞缪尔 我终于成功地从Facebook logi获取了用户信息
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);
});
}
<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.")}/>
import { GraphRequest, GraphRequestManager } from 'react-native-fbsdk';
export const GetInfoUSer = () => {
return new Promise((resolve, reject) => {
const infoRequest = new GraphRequest('/me', null, ((error, result) => {
if (error) {
reject(error)
} else {
resolve(result)
}
}))
new GraphRequestManager().addRequest(infoRequest).start();
})
}
onLoginConFacebook = () => {
LoginManager.logInWithReadPermissions(['public_profile']).then(result => {
if (result.isCancelled) {
console.log(':(')
} else {
AccessToken.getCurrentAccessToken().then((data) => {
let myAccessToken = data.accessToken.toString();
GetInfoUSer().then(response => {
console.log(response)
}).catch(error => {
console.log(error)
})
}
).catch(error => {
console.log(':(')
})
}
})
}
LoginManager.logInWithPermissions(['public_profile']).then(...)
LoginManager.logInWithPermissions(['public_profile', 'email']).then(...)
loginWithFacebook = () => {
LoginManager.logInWithPermissions(['public_profile', 'email']).then(
login => {
if (login.isCancelled) {
console.log('Login Cancelado');
} else {
AccessToken.getCurrentAccessToken().then(
(data) => {
const accessToken = data.accessToken.toString()
this.getInfoFromToken(accessToken)
})
}
},
error => {
console.log('Erro no login ', console.error(error)
)
}
)
}
getInfoFromToken = token => {
const PROFILE_REQUEST_PARAMS = {
fields: {
string: 'id, name, first_name, last_name, birthday, email'
},
}
const profileRequest = new GraphRequest('/me', { token, parameters: PROFILE_REQUEST_PARAMS },
(error, result) => {
if (error) {
console.log('Login Info has an error:', error)
} else {
console.log(result)
}
},
)
new GraphRequestManager().addRequest(profileRequest).start()
}