How to get the Facebook user id using the access token

后端 未结 8 2298
深忆病人
深忆病人 2020-11-28 02:19

I have a Facebook desktop application and am using the Graph API. I am able to get the access token, but after that is done - I don\'t know how to get the user\'s ID.

<
相关标签:
8条回答
  • 2020-11-28 02:48

    in FacebookSDK v2.1 (I can't check older version). We have

    NSString *currentUserFBID = [FBSession activeSession].accessTokenData.userID;
    

    However according to the comment in FacebookSDK

    @discussion This may not be populated for login behaviours such as the iOS system account.

    So may be you should check if it is available, and then whether use it, or call the request to get the user id

    0 讨论(0)
  • 2020-11-28 02:50

    With the newest API, here's the code I used for it

    /*params*/
    NSDictionary *params = @{
                             @"access_token": [[FBSDKAccessToken currentAccessToken] tokenString],
                             @"fields": @"id"
                             };
    /* make the API call */
    FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
                                  initWithGraphPath:@"me"
                                  parameters:params
                                  HTTPMethod:@"GET"];
    
    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
                                          id result,
                                          NSError *error) {
        NSDictionary *res = result;
        //res is a dict that has the key
        NSLog([res objectForKey:@"id"]);
    
    0 讨论(0)
  • 2020-11-28 02:53

    You just have to hit another Graph API:

    https://graph.facebook.com/me?access_token={access-token}
    

    It will give your e-mail Id and user Id (for Facebook) also.

    0 讨论(0)
  • 2020-11-28 02:54

    The facebook acess token looks similar too "1249203702|2.h1MTNeLqcLqw__.86400.129394400-605430316|-WE1iH_CV-afTgyhDPc"

    if you extract the middle part by using | to split you get

    2.h1MTNeLqcLqw__.86400.129394400-605430316

    then split again by -

    the last part 605430316 is the user id.

    Here is the C# code to extract the user id from the access token:

       public long ParseUserIdFromAccessToken(string accessToken)
       {
            Contract.Requires(!string.isNullOrEmpty(accessToken);
    
            /*
             * access_token:
             *   1249203702|2.h1MTNeLqcLqw__.86400.129394400-605430316|-WE1iH_CV-afTgyhDPc
             *                                               |_______|
             *                                                   |
             *                                                user id
             */
    
            long userId = 0;
    
            var accessTokenParts = accessToken.Split('|');
    
            if (accessTokenParts.Length == 3)
            {
                var idPart = accessTokenParts[1];
                if (!string.IsNullOrEmpty(idPart))
                {
                    var index = idPart.LastIndexOf('-');
                    if (index >= 0)
                    {
                        string id = idPart.Substring(index + 1);
                        if (!string.IsNullOrEmpty(id))
                        {
                            return id;
                        }
                    }
                }
            }
    
            return null;
        }
    

    WARNING: The structure of the access token is undocumented and may not always fit the pattern above. Use it at your own risk.

    Update Due to changes in Facebook. the preferred method to get userid from the encrypted access token is as follows:

    try
    {
        var fb = new FacebookClient(accessToken);
        var result = (IDictionary<string, object>)fb.Get("/me?fields=id");
        return (string)result["id"];
    }
    catch (FacebookOAuthException)
    {
        return null;
    }
    
    0 讨论(0)
  • 2020-11-28 02:56

    If you want to use Graph API to get current user ID then just send a request to:

    https://graph.facebook.com/me?access_token=...
    
    0 讨论(0)
  • 2020-11-28 03:01

    You can use below code on onSuccess(LoginResult loginResult)

    loginResult.getAccessToken().getUserId();

    0 讨论(0)
提交回复
热议问题