Explain many inner-most loops in FB.login to get the access token?

225 views Asked by At

My friend called this like lambda-calculus, even though I know some lambas -- I am still confused how the authorization really works to get the access token.

So please explain this code, hopefully line-by-line. Source of the example is here.

 FB.login(function(response) {
   if (response.authResponse) {
     console.log('Welcome!  Fetching your information.... ');
     FB.api('/me', function(response) {
       console.log('Good to see you, ' + response.name + '.');
     });
   } else {
     console.log('User cancelled login or did not fully authorize.');
   }
 });

P.s. Trying to categorize things here, the q is a sub-problem.

Perhaps related

  1. Explain the Facebook access_token

  2. Facebook get access Token

1

There are 1 answers

0
Art On BEST ANSWER
FB.login(function(response) {

FB is where all of the Facebooks functions live. All the methods are defined here: https://developers.facebook.com/docs/reference/javascript/ . Login in particular will popup a dialog asking the user to sign in or approve your app.

   if (response.authResponse) {

If they approve your app or have it already added, the response.authResponse will be populated. authResponse also has accessToken, expiry, and UserID. See more details here: https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

     console.log('Welcome!  Fetching your information.... ');
     FB.api('/me', function(response) {

This goes to Facebook to ask for the user who just approved/signed into your app. Now you can get the basic information about the user. All the available fields with permissions you need to get them are available here: https://developers.facebook.com/docs/reference/api/user/

       console.log('Good to see you, ' + response.name + '.');

This is just getting the name from the user object that you recieved from the /me endpoint.

     });
   } else {
     console.log('User cancelled login or did not fully authorize.');

This means the user cancelled or did not approve your app so you cannot grab any of their information from Facebook.

   }
 });