Google Chrome Extension + Login with Facebook + Parse

后端 未结 3 2170
再見小時候
再見小時候 2021-01-31 23:24

I\'m trying to build a google chrome extension, a use case requires that users can login with Facebook and share via a post to their Facebook wall.

The correct applicati

3条回答
  •  不知归路
    2021-01-31 23:54

    I had this kind of problem lately ;). It's a bit complicated, but I'll try to explain it in the simplest way.

    Put somewhere in your extension (maybe options page) link "login with facebook" that redirects to: https://www.facebook.com/dialog/oauth?client_id=&response_type=token&scope=&redirect_uri=http://www.facebook.com/connect/login_success.html

    When someone clicks on this link, will be asked to give permissions, etc (facebook page). If someone would agree, the page will load: http://www.facebook.com/connect/login_success.html with parameters access_token and expires_in in address.

    Now, background page should have script like this:

    var successURL = 'www.facebook.com/connect/login_success.html';
    
    function onFacebookLogin(){
      if (!localStorage.getItem('accessToken')) {
        chrome.tabs.query({}, function(tabs) { // get all tabs from every window
          for (var i = 0; i < tabs.length; i++) {
            if (tabs[i].url.indexOf(successURL) !== -1) {
              // below you get string like this: access_token=...&expires_in=...
              var params = tabs[i].url.split('#')[1];
    
              // in my extension I have used mootools method: parseQueryString. The following code is just an example ;)
              var accessToken = params.split('&')[0];
              accessToken = accessToken.split('=')[1];
    
              localStorage.setItem('accessToken', accessToken);
              chrome.tabs.remove(tabs[i].id);
            }
          }
        });
      }
    }
    
    chrome.tabs.onUpdated.addListener(onFacebookLogin);
    

    And this is how you get access token from facebook. Now you can send requests to https://graph.facebook.com.

    Hope I helped ;)

提交回复
热议问题