Initiate login for custom stories

276 views Asked by At

I have created a custom story to be posted on facebook timeline when user clicks on a share button.

While i was logged into facebook, I could successfully post on timeline using the following code

function postLike() {
    FB.api(
   'https://graph.facebook.com/me/og_pricepan:compared',
   'post',
   { product: objectToLike,
       privacy: { 'value': 'SELF'}
   },
   function (response) {
       if (!response) {
           alert('Error occurred.');
       } else if (response.error) {
           document.getElementById('result').innerHTML =
         'Error: ' + response.error.message;
       } else {
           document.getElementById('result').innerHTML =
         '<a href=\"https://www.facebook.com/me/activity/' +
         response.id + '\">' +
         'Story created.  ID is ' +
         response.id + '</a>';
       }
   }
);

Now, when the user is not logged into facebook, I get the following error:

Error: An active access token must be used to query information about the current user.

  1. Shouldn't the login window get open by itself asking the user to login into facebook or I need to do something on my end to handle this situation?

  2. Also, do i need to submit my application for review before users can create custom stories from my website to their timeline?

1

There are 1 answers

1
Leo On BEST ANSWER

Shouldn't the login window get open by itself asking the user to login into facebook or I need to do something on my end to handle this situation?

No. You need to explicitly login the user by following the Facebook Login Flow to request an access token

Also, do i need to submit my application for review before users can create custom stories from my website to their timeline?

Yes and No. No, because it's not like you will have someone from Facebook Support Team going through your app to make sure your app ticks all the boxes. Yes, because you will need to register your app with Facebook. So, technically all you need to do is register with Facebook as a developer and register your app in order to get an app secret and API key. You can have your app in "Sandbox mode", create test users and test your integration with Facebook before you publish it live. There's few requirements you need to meet before publishing it, such as providing set of logos of a specific size, etc. Here's a blog explaining part of the process, but really, the process is very straight-forward and you should be able to get through easily by yourself.

Now, the error you are getting says it all. There's no user logged in to facebook. The endpoint you are trying to post to, requires a active valid access token, if it doesn't exist then there's no context, no user authenticated and you need to explicitly authenticate the user by invoking the login dialog...

FB.login(function(response) {
    if (response.authResponse) {
        // The person logged into your app
    } else {
        // The person cancelled the login dialog
    }
});

If you need to know whether a user is logged in or not, you can follow to simple approaches:

1.By invoking the FB.getLoginStatus function

FB.getLoginStatus(function(response) {
   if (response.status === 'connected') {
     var uid = response.authResponse.userID;
     var accessToken = response.authResponse.accessToken;
   } 
   else if (response.status === 'not_authorized') {
       // the user is logged in to Facebook, 
       // but has not authenticated your app
   } 
   else {
       // the user isn't logged in to Facebook.
   }
  });

2.Or, by subscribing to the auth.authResponseChange event when initializing the framework and setting the status option to true

There's a full-legged tutorial on Facebook Developers Site for Javascript SDK explaining both approaches.

Hope it makes sense