Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the Facebook API on mobile devices?

This bit of documentation has not been as helpful as you might think. I understand that I have to hang a display parameter on the end of the URL, but I'm not sure how to invoke a login window like that. Here's what I have now:

<script src="http://connect.facebook.net/en_US/all.js" type="text/javascript"></script>
<script type="text/javascript">
  // initialize the library with the API key
  FB.init({ apiKey: '{{ facebook_api_key }}', status: true, cookie: true, xfbml: true});

  function facebookConnect(form){
      function handleResponse(response){
          form.submit();
      }
      FB.login(handleResponse,{perms:'publish_stream,user_about_me,status_update,email,offline_access'});
  }

</script>

This works fine in a desktop browser, but I can't figure out how to get the 'touch' or 'wap' modes for dialogs.

I'm using django-socialregistration if that's of any relevance.

like image 889
C. Alan Zoppa Avatar asked Dec 30 '10 17:12

C. Alan Zoppa


People also ask

How do I use the Facebook API?

To use the Facebook API we have to add an app entry to our Facebook Developer Apps dashboard. You will need a Facebook developer account if you don’t have one already. Choose a random category and click Create App ID. On the next page, scroll down to the bottom and complete both fields with the project packages names.

How do I enable Facebook login for devices?

Enable Login for Devices Load your app's dashboard and change Product > Facebook login > Settings > Login from Devices to 'Yes'. 2. Generate a Code When the person clicks the Connect to Facebook or Log in with Facebook call-to-action, you device should make an HTTP POST to:

How do I connect my device to Facebook?

Load your app's dashboard and change Product > Facebook login > Settings > Login from Devices to 'Yes'. 2. Generate a Code When the person clicks the Connect to Facebook or Log in with Facebook call-to-action, you device should make an HTTP POST to:

How long is the Facebook device login API code?

Display the full code you received from Facebook's Device Login API. The code is between 6 and 12 characters long. You can include a Close or Cancel button so people can cancel the device login flow.


2 Answers

Logging into Facebook using a touch dialog used to work a while ago, but that same old code doesn't seem to work anymore.

However,

You can redirect a user to the login URL, and facebook will do the rest depending on the platform. The following snippet will login the user, and redirect back to your current page if set.

Update: You also need to edit your 'Basic App Settings' > 'Select how your app integrates with Facebook' and check 'Mobile Web' for this to work.

window.location = "http://www.facebook.com/dialog/oauth/"
    + "?" + "scope=" + "publish_stream,user_about_me,status_update,email,offline_access"
    + "&" + "client_id=" + YOUR_APP_ID_GOES_HERE
    + "&" + "redirect_uri=" + YOUR_CURRENT_PAGE_URL
    + "&" + "response_type=token";

The display:touch property can still be used in FB.ui() as follow (for instance, posting on a wall):

// Make sure you call FB.init() before FB.ui()
FB.ui({
    app_id: YOUR_APP_ID_GOES_HERE,
    method: 'feed',
    name: "post title",
    link: "http://link-to-what-you-are-sharing/",
    picture: "your_image.jpg",
    description: "post description",
    display: 'touch'
},
function callback(r){
    if ( (r && r.id) || (r && r.post_id) ){ alert('posted');}
    else{ alert('failed'); }
});
like image 155
Pierre Avatar answered Oct 05 '22 23:10

Pierre


Why are you wrapping the response handler inside a function?

Do something like :

function handleStatusChange(response) {
  if (response.authResponse) {
    console.log(response);
  }
}

window.fbAsyncInit = function() {
  FB.init({ appId: 'YOUR_APP_ID', 
    status: true, 
    cookie: true,
    xfbml: true,
    oauth: true
  });

  FB.Event.subscribe('auth.statusChange', handleStatusChange);  
};

Taken from https://developers.facebook.com/docs/guides/mobile/web/

like image 39
pradeek Avatar answered Oct 05 '22 23:10

pradeek