Facebook accountkit gives error when exchanging tokens from nodeJS server

373 views Asked by At

I have been integrating fb AccounKit with my ionic application (NodeJS server). Front-end part has been done and I'm able to send and receive OTPs and success status.

But while getting client token from authorization code, I keep getting ""Error verifying the token in the \'access_token\'"' error. I followed the same procedure mentioned in their official docs.

This is my code :

var me_endpoint_base_url = 'https://graph.accountkit.com/v1.0/me';
token_exchange_base_url='https://graph.accountkit.com/v1.0/access_token';

var params = {
  grant_type: 'authorization_code',
  code: request.body.code,
  access_token: app_access_token
 };
}

// exchange tokens
console.log(Querystring.stringify(params))
var token_exchange_url = token_exchange_base_url + '?' + Querystring.stringify(params);
Request.get({url: token_exchange_url, json: true}, function(err, resp, respBody) {
  console.log(respBody);
  var view = {
    user_access_token: respBody.access_token,
    expires_at: respBody.expires_at,
    user_id: respBody.id,   
  };
  var me_endpoint_url = me_endpoint_base_url + '?access_token=' + respBody.access_token;
  Request.get({url: me_endpoint_url, json:true }, function(err, resp, respBody) {
    console.log(respBody);
    if (respBody.phone) {
      view.method = "SMS"
      view.identity = respBody.phone;
    } else if (respBody.email) {
      view.method = "Email"
      view.identity = respBody.email.address;
    }
  });
});

Please help?

1

There are 1 answers

1
Pouya Larjani On BEST ANSWER

When making a sever-to-server call to exchange a code for a token, you need to supply your Account Kit App Secret in the access token you're sending. So the access token should look like:

'AA|{app_id}|{app_secret}'

For ex.

var app_access_token = ['AA', app_id, app_secret].join('|');

You can find your app secret from the Account Kit dashboard. Go on "Account Kit" page under products section of your app on developers.facebook.com and click on the "Show" button next to the box for "Account Kit App Secret" to see your app secret.

Also remember that you should never include your app secret on any javascript code that runs on the client side. This secret is to be used only from your server-side node.js code and no one else should be able to see it