Facebook API - different user ID's for each app?

若如初见. 提交于 2019-12-30 08:15:45

问题


I'm currently working on a mobile app, and as such I need to have a new app specifically for this purpose. The stupid thing it seems, is that Facebook have decided to use different "user ids" for each app. Apart from not seeing the point in this what-so-ever, I'm baffled as to how I can extract the same userId for a given user, to match it up with my DB. I'm using oauth.io to process the login, and return the values:

$('#facebook-login').click(function() {
    $('#ajax_spinner_wrapper').show();
    OAuth.initialize('CmRGYdnRMWM2ZrOb7M1Nf1_T57o');
    OAuth.popup('facebook').done(function(result) {
        //console.log(result)
        result.get('/me').done(function(data) {
            //todo with data
            console.log(data);

            $.post('https://steampunkjunkies.net/cgi-bin/mobi_login.cgi', { action: 'store_session', what: 'facebook', user: data.id }, function(result) {
                $('#ajax_spinner_wrapper').hide();
            });
        }).fail(function(err) {
            //todo with err
            console.log("Hit an error?");
        });


    })
    .fail(function (err) {
        alert("oops, we got an error!");
        console.log(err);
        $('#ajax_spinner_wrapper').hide();
    });

    return false;
});

The problem though, is that it gives me totally different Id's in each app. Here is what I get with my web-based one:

'id' => '838605583',

..and then using the other app in my oauth.io (https://oauth.io) setup:

id  "10155167132970584"

Both for exactly the same user.

Is there some way to correlate these Id's, so I can match them together? Its amazing that Facebook didn't think that something like this would be an issue!

NOTE: A bit of info from Facebook, regarding this change:

https://developers.facebook.com/docs/apps/upgrading#upgrading_v2_0_user_ids

Facebook will begin to issue app-scoped user IDs when people first log into an instance of your app coded against v2.0 of the API. With app-scoped IDs, the ID for the same user will be different between apps.

No matter what version they originally used to sign up for your app, the ID will remain the same for people who have already logged into your app. This change is backwards-compatible for anyone who has logged into your app at any point in the past.

If you're not mapping IDs across apps, then no code changes should be required. If you need to map the same user IDs across multiple apps or run cross-app promotions, we've added a new API called the Business Mapping API. This lets you map a logged-in user's IDs across apps as long as those apps are all owned by the same business. Learn more about implementing cross-promotions.

For users who have not logged into your app, the user ID may change depending on the version of the API that you call. In v1.0 of the API users who have not logged into your app will be referred to by their original Facebook user ID, whereas in v2.0 they will be referred to by an app-scoped ID.

We've added new API endpoints that allow you to tag and invite friends who don't use your app. These APIs return tokens which may be used to generate custom interfaces for tagging and invitations. Those tokens aren't meant to be cachable and we make no guarantees that they will be stable beyond 24 hours from the time the API response containing them was received. They aren't the same as either the IDs used on data for people not logged into your app nor the same as the app-scoped IDs.

Users can check their app-scoped IDs for each app they've installed in the app's Edit Settings dialog. Make sure users of your app know how to find it when they contact you with support queries

I really don't get their logic. It makes much more sense having a unique identifier for each user, which can be used to verify it against any number of apps. In my case, someone logs into our web-based app, then they also want to log into the mobile one - but its got a different user ID, so there is no way to link the 2 together! Eugh


回答1:


Well, this is very old news. It's been like that since April 30th 2014. With the introduction of the graph API v2.0, FB also introduced the Business Mapping API, which is designed for your use case.

You won't receive one user id, but you'll receive a token_for_business:

This returns a string which is the same for this person across all the apps managed by the same Business Manager.

See

  • https://developers.facebook.com/docs/apps/for-business#field
  • https://developers.facebook.com/docs/apps/for-business#api
  • https://developers.facebook.com/docs/apps/business-manager#create-business



回答2:


LoginManager.logInWithReadPermissions(['email']).then((result) => {
          if (result.isCancelled){
            //console.warn("Login is canceled");
          } else {
             AccessToken.getCurrentAccessToken().then((data) =>{
             const responseInfoCallback = (error, result) => {
                if (error) {

                } else {
                    //you can pick data.userID  and data.applicationID
                    //To getting unique id of user by application
                    //I think you can use (data.userId + data.applicationId) as the id
                    //For getting profile you can use result.name, result.id (it likes data.userId)
                    //Token is available in data.accessToken

                }
              }
              const infoRequest = new GraphRequest(
                '/me',
                {
                  accessToken: data.accessToken,
                  parameters: {
                    fields: {
                      string: 'email, name, gender, picture,cover, id'
                    }
                  }
                },
                responseInfoCallback
              );
              new GraphRequestManager().addRequest(infoRequest).start()
        })
          }
        }, (error) => {
          console.warn(error)
        });


来源:https://stackoverflow.com/questions/28087051/facebook-api-different-user-ids-for-each-app

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