Home » Android » android – Getting phone number from Facebook Account Kit

android – Getting phone number from Facebook Account Kit

Posted by: admin June 15, 2020 Leave a comment

Questions:

The Account Kit documentation states that if your began the login session with AccountKitActivity.ResponseType.TOKEN, it’s possible to access the Account Kit ID, phone number and email of the current account via a call to getCurrentAccount().

Is it possible to get the user’s phone number if you began with AccountKitActivity.ResponseType.CODE just like the way Saavn does it?

How to&Answers:

Yes, it’s possible provided you use LoginType.PHONE in your configuration.

    AccountKit.getCurrentAccount(new AccountKitCallback<Account>() {
        @Override
        public void onSuccess(final Account account) {
            String accountKitId = account.getId();
            PhoneNumber phoneNumber = account.getPhoneNumber();
            String phoneNumberString = phoneNumber.toString();
        }

        @Override
        public void onError(final AccountKitError error) {
            // Handle Error
        }
    });

This is your phone number: phoneNumberString; but, account.getEmail() will return null if LoginType.PHONE was used in your configuration.
Vice versa if you use LoginType.EMAIL in your configuration.

Answer:

The purpose of using CODE instead of TOKEN is to shift the token request to your application server. The server uses the Graph api to submit the auth token and if the auth token is valid, the call returns the access token which is then used to verify the user’s identity for subsequent API calls.

A graph call to validate the access token returns the account kit id plus additional metadata like the associated phone number and/or email.

{  
   "id":"12345",
   "phone":{  
      "number":"+15551234567"
      "country_prefix": "1",
      "national_number": "5551234567"
   }
}

Answer:

You can fetch Account ID,Email and Phone number using below code:

    let accountKit = AKFAccountKit(responseType: AKFResponseType.accessToken)
    accountKit.requestAccount { (account, error) in
     if(error != nil){
        //error while fetching information
      }else{
        print("Account ID  \(account?.accountID)")
        if let email = account?.emailAddress,email.characters.count > 0{
             print("Email\(email)")
        }else if let phoneNum = account?.phoneNumber{
             print("Phone Number\(phoneNum.stringRepresentation())")
        }
      }
    }

Answer:

If you have access code/token

On server or client, you can exchange access token for mobile number and country code with this FB AccountKit APIhttps://graph.accountkit.com/v1.1/me/?access_token=xxxxxxxxxxxx. Here xxxxxxxxxx is your Access Token.

If you have auth code/token instead…

You can first exchange the access code for an access token on the server side (because it contains the App Secret) with this APIhttps://graph.accountkit.com/v1.1/access_token?grant_type=authorization_code&code=xxxxxxxxxx&access_token=AA|yyyyyyyyyy|zzzzzzzzzz. Here xxxxxxxxxx, yyyyyyyyyy and zzzzzzzzzz are the auth code, app id and app secret respectively. Once you have the access token with it, you can get the mobile number with the above mentioned API.

Good Luck.