How do I use the Facebook API on mobile devices?
This bit of documentation has not been as helpful as you might think. I unde开发者_Go百科rstand 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.
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'); }
});
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/
精彩评论