Set Javascript API accessToken
Asked Answered
H

1

6

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 !

Hilda answered 28/1, 2012 at 16:28 Comment(0)
A
9

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!

Affaire answered 28/1, 2012 at 16:41 Comment(2)
It worked just great ! FYI, It appeared we don't need to set token as a global variable. Thks a lot for your help !Hilda
Thanks a ton! Had the same problem and this solved it in a few seconds! You saved me HOURS of work.Pianola

© 2022 - 2024 — McMap. All rights reserved.