Getting user app token using react-native facebook SDK
Asked Answered
S

2

5

I've gotten facebook login working using the new https://github.com/facebook/react-native-fbsdk, however I can't seem to get the user token which is usually returned in the response. Anyone found a way to get this information? The other SDK's https://github.com/magus/react-native-facebook-login returns it on the login request but the new FB sdk doesn't, and the documentation on the github page doesn't mention it anywhere.

Streaky answered 31/8, 2015 at 20:2 Comment(1)
Can you share example code how you got it working? I am getting this problem: github.com/facebook/react-native-fbsdk/issues/10Freese
S
10

Found the answer after some more digging. You need to use the fbsdkcore to access the user token. Heres how you use it.

var FBSDKCore = require('react-native-fbsdkcore');
var {
  FBSDKAccessToken,
} = FBSDKCore;

var Login = React.createClass({
  render: function() {
    return (
      <View>
        <FBSDKLoginButton
          onLoginFinished={(error, result) => {
            if (error) {
              alert('Error logging in.');
            } else {
              if (result.isCanceled) {
                alert('Login cancelled.');
              } else {
                FBSDKAccessToken.getCurrentAccessToken((token) => {
                  console.log(token.tokenString);
                })
              }
            }
          }}
          onLogoutFinished={() => console.log('Logged out.')}
          readPermissions={[]}
          publishPermissions={['publish_actions']}/>
      </View>
    );
  }
});
Streaky answered 31/8, 2015 at 21:46 Comment(3)
In react-native-fbsdk version 0.3.0 this is no longer FBSDKAccessToken but instead AccessToken.Ceballos
I have one question. I need email permission. how should I write. readPermissions={['public_profile', 'email']} ¿Is it okay?Cenobite
I was trying to get console.log(JSON.stringify(result)); But I didn't get any result on login with facebook clickZen
M
2

You don't need a third party module (react-native-fbsdkcore)

The package (react-native-fbsdk) has instructions on it's Github page: https://github.com/facebook/react-native-fbsdk#usage

After you've logged the user in, you can fetch the access credentials as follows:

import { AccessToken } from 'react-native-fbsdk';

AccessToken.getCurrentAccessToken().then(data => {
  console.log(data.accessToken.toString())
})
Mystique answered 10/1, 2021 at 1:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.