Issues retrieving fbsdk Access Token and User ID

此生再无相见时 提交于 2019-12-08 12:39:53

问题


I am trying to retrieve the Access Token and User ID of the logged in user in my React Native App. For some reason when I tried to update the fbsdkcore package, it did not exist anymore. So, I tried to resolve it within the general fbsdk package.

I am calling the js file (of which I think retrieves the accesstoken) in the package as:

const AccessToken = require('react-native-fbsdk/js/FBAccessToken');

And subsequently in my code I try to log it so that I can see if it works, simply by:

console.log(AccessToken.getCurrentAccessToken());
console.log(AccessToken.getUserId);

But the log only returns:

2016-05-05 10:22:28.276 [info][tid:com.facebook.React.JavaScript] { _45: 0, _81: 0, _65: null, _54: null }
2016-05-05 10:22:28.277 [info][tid:com.facebook.React.JavaScript] undefined

Which does not seem to be the droids that im looking for.

I inspected the code for the js file in the fbsdk package and the getCurrentAccessToken code looks like this:

  /**
   * Getter for the access token that is current for the application.
   */
  static getCurrentAccessToken(): Promise<?FBAccessToken> {
    return new Promise((resolve, reject) => {
      AccessToken.getCurrentAccessToken((tokenMap) => {
        if (tokenMap) {
          resolve(new FBAccessToken(tokenMap));
        } else {
          resolve(null);
        }
      });
    });
  }

Which of course seems reasonable. But since I get this really weird result when I try to call it, I get worried over that I have done something wrong in the bigger picture. I even modified the resolve(null) part of the code so that I could make sure of what happend. But it still returned the same weird "token".

The log also returns this error when logging in:

2016-05-05 10:22:07.630 AppName[15097:415865] -canOpenURL: failed for URL: "fbauth2:/" - error: "(null)"

But I think that is only because I don't have the facebook app on my xcode simulator.

Can anybody throw me a good guess on what I have done wrong??


回答1:


GetCurrentAccestoken returns a promise.

Maybe you can try:

AccessToken.getCurrentAccessToken().then(
    (data) => {
        console.log(data.accessToken.toString())
    }
)



回答2:


Try this. It worked for me

AccessToken.getCurrentAccessToken().then(
      (data) => {
            console.log(data.accessToken)
            console.log(data.userID);
  });



回答3:


LoginManager.logInWithReadPermissions(['public_profile']).then(
            function (result) {
                if (result.isCancelled) {
                    alert('Login cancelled');
                } else {
                    // alert('Login success with permissions: ' +
                    //     result.grantedPermissions.toString());
                    AccessToken.getCurrentAccessToken().then(
                        (data) => {
                            doLoginViaFb(data.userID, data.accessToken);

                        }
                    );

                    alert(result);
                    console.log(result.toString());
                    console.log(JSON.stringify(result));
                }
            },
            function (error) {
                alert('Login fail with error: ' + error);
            }
        );


来源:https://stackoverflow.com/questions/37045983/issues-retrieving-fbsdk-access-token-and-user-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!