Botkit: Facebook Login integration

Created on 3 Dec 2016  路  6Comments  路  Source: howdyai/botkit

Our Facebook Botkit is looking with envy at the Slack Botkit's integrated redirect URI feature...

Anyone managed to integrate the Facebook Login flow to Botkit? I'd be delighted to grab some code.

Can we expect to have this feature in the master any soon?

Facebook-related enhancement stale

Most helpful comment

This is an interesting feature idea!

All 6 comments

@devhaoy Can you give an example of how you use this feature for a bot? I admit I can't think of a use case where a bot needs the access token granted by the login feature

@agamrafaeli any app that needs to access user's personal information - financial services as much as dating apps! An example of the latest, take a look at this pretty cool bot https://m.me/foxsybot

This is an interesting feature idea!

I am trying to get users to Login via the account_link button type (smooth since everything happens in Messenger). I need to modify this code for account linking into something that will just log in the user. Could anyone please give me a hand?

webserver.get('/login/callback', function(req, res) {
      var accountLinkingToken = req.query['account_linking_token'];
      var redirectURI = req.query['redirect_uri'];

      // Authorization Code should be generated per user by the developer. This will 
      // be passed to the Account Linking callback.
      var authCode = "1234567890";

      // Redirect users to this URI on successful login
      var redirectURISuccess = redirectURI + "&authorization_code=" + authCode;

      res.render('authorize', {
        accountLinkingToken: accountLinkingToken,
        redirectURI: redirectURI,
        redirectURISuccess: redirectURISuccess
      });
    });

  webserver.get('login/callback', function (req, res) {
        // log user out
        //delete req.session.token;

        // move success message into local variable so it only appears once (single read)
      console.log("redirect uri", req.query['redirect_uri']);  
      //var viewData = { success: 1 };
        //delete req.session.success;

      //  res.render('login', viewData);
        res.render('login');
    });

  webserver.post('login/callback', function (req, res1) {
      var accountLinkingToken = req.query['account_linking_token'];
      var redirectURI = req.query['redirect_uri'];
      console.log("redirect uri", req.query['redirect_uri']);
      var optionsget = {
            host : 'HOST', // here only the domain name
            path : '/authenticate?username=' + req.body.username + '&password=' + req.body.password, // the rest of the url with parameters if needed
            method : 'GET' // do GET
        };

        console.info('Options prepared:');
        console.info(optionsget);
        console.info('Do the GET call');

        // do the GET request
        var reqGet = http.request(optionsget, function(res) {
            console.log("statusCode: ", res.statusCode);
            // uncomment it for header details
        //  console.log("headers: ", res.headers);
            var data = '';
                res.on('data', function(d) {
                 data += d;
                });
                res.on('end', function(){
                    if (data != ''){
                        var custauth = JSON.parse(data);
                        var optionsget = {
                            host : 'graph.facebook.com', // here only the domain name
                            path : '/v2.6/me?access_token=' + process.env.page_token + '&fields=recipient&account_linking_token=' + accountLinkingToken, // the rest of the url with parameters if needed
                            method : 'GET' // do GET
                        };
                        console.info('Options prepared:');
                        console.info(optionsget);
                        console.info('Do the GET call');
                        var reqGet1 = https.request(optionsget, function(res) {
                            console.log("statusCode: ", res.statusCode);
                            // uncomment it for header details
                        //  console.log("headers: ", res.headers);
                            var data = '';
                            res.on('data', function(d) {
                                data += d;
                            });
                            res.on('end', function(){
                                var page_scoped_id = JSON.parse(data);
                                var optionsget = {
                                    host : 'HOST', // here only the domain name
                                    path : '/session?psid=' + page_scoped_id.recipient + '&custid=' + custauth.custid, // the rest of the url with parameters if needed
                                    method : 'GET' // do GET
                                };
                                console.info('Options prepared:');
                                console.info(optionsget);
                                console.info('Do the GET call');
                                var reqGet2 = http.request(optionsget, function(res) {
                                    console.log("statusCode: ", res.statusCode);});
                                reqGet2.end();
                            })
                        });
                        reqGet1.end();

                        res1.writeHead(301,
                                  {Location: redirectURI + '&authorization_code=' + custauth.authorization_code}
                                );
                        res1.end();

                    }
                    else if (res.statusCode = 404 ){
                        res1.writeHead(301,
                                  {Location: redirectURI }
                                );
                        res1.end();
                    }
                })
            });
        reqGet.end();

    }); 

Has anyone done it?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

simpixelated picture simpixelated  路  3Comments

seriousssam picture seriousssam  路  3Comments

GautierT picture GautierT  路  3Comments

koubenecn picture koubenecn  路  3Comments

dfischer picture dfischer  路  4Comments