Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I reuse my server-side Facebook access_token in javascript SDK?
    primarykey
    data
    text
    <p><strong>Scenario/setup</strong></p> <p>I have a web application that allows a user to log in using either Facebook or username+password. The user is able to link Facebook to the account, in which case the backend stores an offline Facebook access_token token, so that it can make various requests. This all works fine.</p> <p>Now I want to use features of the Facebook Javascript SDK (specifically open the requests dialog), but I do not want the user to be forced to log into Facebook on the client if he just logged into the application using username/password.</p> <p><strong>Question</strong></p> <p>Having a valid access_token on the server, I want to pass this token to the Facebook javascript SDK, so that no matter how the user logged into my web application, he is now "logged in" to the Facebook account associated with his account, as far as the SDK is concerned.</p> <p>I have found a few vague examples, but none that explain in detail how to do it entirely.</p> <p>Currently I am thinking the way to go is passing the token to javascript in FB.Init, but I cannot get it to work:</p> <pre><code>FB.Init({ authResponse: { access_token: &lt;token&gt;, expiresIn: &lt;expiry&gt;, signedRequest: &lt;what-is-this?&gt;, userID: &lt;user&gt; }, ... }); </code></pre> <p>Another example I found, passed the access_token directly to the API call, but this does not seem to work either:</p> <p><code>FB.ui({ method: 'apprequests', access_token: &lt;token&gt;, ... ();</code></p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COThere should be no need to pass the access token from the server to the client – when you embed the JS SDK and set `status: true` while doing so, it should automatically recognize a user that has connected to your app before and provide you with an access token client-side. (If it doesn’t do so automatically, then maybe you will need to call `FB.getLoginStatus` explicitly.)
      singulars
    2. COI think you misunderstand what I am trying to do. What you suggest, implies that the user is either logged onto Facebook in the browser or will be prompted to do so. I want to avoid this. The user has not neccesarily logged into my web app using Facebook at this particular login, but the server has a valid offline access_token from a previous login. I want to use this previously obtained token on the client, instead of bothering the user with logging in.
      singulars
    3. COPassing the access token while initializing the SDK as you tried is not possible. It is possible to pass the token explicitly when making API calls, yes – but you are not trying to make an API call here, you are trying to invoke the Requests dialog. And that is specifically designed to be actively triggered by the user, to avoid developers misusing this feature in a spammy way. And therefor I am pretty sure it will not work without having an actually logged-in user.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload