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

后端 未结 5 1040
花落未央
花落未央 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:35

    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:

       {
            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

提交回复
热议问题