Alex Kombo
Alex Kombo

Reputation: 3356

Getting phone number from Facebook Account Kit

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?

Upvotes: 8

Views: 11448

Answers (4)

Donal
Donal

Reputation: 6425

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())")
        }
      }
    }

Upvotes: 5

Aakash
Aakash

Reputation: 23855

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 API - https://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 API - https://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.

Upvotes: 3

ayz4sci
ayz4sci

Reputation: 2248

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.

Upvotes: 19

Jaymassena
Jaymassena

Reputation: 131

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"
   }
}

Upvotes: 5

Related Questions