How to use graph API with react-native-fbsdk?

后端 未结 5 1082
花落未央
花落未央 2021-01-30 17:49

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

5条回答
  •  感动是毒
    2021-01-30 18:23

    Thank you @Samuel.

    I finally succeed to get user information from Facebook login because of your help!

    But I struggled to figure out how can I get username and email literally from the result object cause I am a newbie in React & Javascript.

    P.S. result["name"] is the point because it is object!!

    So I added some code to yours for other people like me.

    If you don't like using your code, just tell me that.

     {
          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.")}/>
    

提交回复
热议问题