Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Javascript API accessToken

I am developing a Facebook app. I have a server side OAuth flow which allows me to authenticate a user without requiring him to click on any button. I hence retrieve his accessToken as long as other information and use those ones on the server side before to generate the page.

In my application, I now need to use the Javascript API which could technically share the same Oauth token.

Is it possible to instantiate the FB javascript Object with a given Oauth token ?

I know it is possible to do the contrary meaning doing the Oauth process on the client side and share the Oauth key with the server side via the cookie but this has two many drawbacks in my opinion : _ First, it implies to have this "login" button which is to me not a good user experience for a facebook app. _ Also, I don't get how this Oauth process is supposed to behave if my application is composed of two different pages. Going from one page to another reloads entirely the javascript. Will this Oauth process (with the popup and so forth) be done on each page reloading ?

Thanks for your help !

like image 801
Olivier Pichon Avatar asked Jan 28 '12 16:01

Olivier Pichon


1 Answers

I had a similar problem once, Facebook has an older method called FB.getLoginStatus which you can use to check if the user has given permission without a popup -

FB.init({appId: 'XXXXXXXXXXXXXX', xfbml: true, cookie: true, oauth: true});
FB.getLoginStatus(function(response) {

  if (response.authResponse) {
    token = response.authResponse.accessToken;
    FB.api('/me', function(response) {
        console.log(response);
         // do something here they are logged in and have given you perms   
    });
  } else {
    // no user session available, someone you dont know
  }
});

hope that helps!

like image 166
Drew Dahlman Avatar answered Sep 28 '22 01:09

Drew Dahlman