mobile version of Facebook app going into redirect loop - facebook

I have developed a Facebook app using the C# SDK and it is working fine. Now I want to also enable it on mobiles, so I tried to set the "mobile url" to the same one as my canvas url (which is a cloudapp.net address). However, when I try to access it from a mobile, it seems to go into a redirect loop involving my canvas url, the apps.facebook url and the m.facebook/apps url. Sometimes it goes out of the loop and I get the facebook error message saying : "the mobile version of the app is unavailable because it is misconfigured. It appears to be caught in a redirect loop."
I think it may have to do with the fact that the facebooksettings in the webconfig file specify that my cloudapp page should redirect to the apps.facebook.com page, which then redirects to the mobile url and so on. Can someone tell me how to solve this problem - I just want my mobile url to be the same as my canvas url.
Thanks.

If the request is from mobile add extra logic
For v6,
var fb = new FacebookClient();
var desktopLoginUrl = fb.GetLoginUrl(new { .... });
var mobileLoginUrl = fb.GetLoginUrl(new { ...., mobile = true });
For v5,
var urlBuilder = new UrlBuilder(oauthClient.GetLoginUrl(...));
urlBuilder.Host = "m.facebook.com";
var loginUrl = urlBuilder.Uri;

Related

Facebook Login - Windows 10 UWP - Desktop

I am building a new Universal Windows 10 UWP App, and trying to do a Facebook login.
On Facebook, I have set the Windows App section of my app to have the identifier of my app:
Windows : s-1-15-xxxxxxxxxxxx
Windows Phone : fef49b712e5a843cbfeb0c9d780423fc (Not the actual one)
In the package manifest file, I have added the protocol of:
msft-fef49b712e5a843cbfeb0c9d780423fc
Which means I set my redirect_uri parameter to:
msft-fef49b712e5a843cbfeb0c9d780423fc://authorize
When running on a phone (with Windows 10 Mobile Preview) the service works fine, it opens up the Facebook app on the phone (using fbconnect://authorize?.....), which in turn authenticates, and then opens my app back up - perfect!!
However, when trying the same on the desktop it doesn't work. In my Launcher.LaunchUriAsync() is set to have a fallback Uri of the standard Facebook web dialog (https://www.facebook.com/dialog/oauth?.....) - This is because there is no Facebook app for Windows 10 that supports login.
Sending the same redirect_uri through to Facebook, it opens up the web browser (Edge) and asks for permissions etc. once the permissions have been given, nothing happens. It seems as though the protocol handling isn't working.
Any thoughts would be useful.
On desktop, try using the WebAuthenticationBroker instead of Launcher.LaunchUriAsync as described in this example: http://dotnetbyexample.blogspot.de/2015/06/custom-oauth-login-to-facebook-for.html
private async Task<string> AuthenticateFacebookAsync()
{
try
{
var fb = new FacebookClient();
var redirectUri =
WebAuthenticationBroker.GetCurrentApplicationCallbackUri().ToString();
var loginUri = fb.GetLoginUrl(new
{
client_id = AppId,
redirect_uri = redirectUri,
scope = ExtendedPermissions,
display = "popup",
response_type = "token"
});
var callbackUri = new Uri(redirectUri, UriKind.Absolute);
var authenticationResult =
await
WebAuthenticationBroker.AuthenticateAsync(
WebAuthenticationOptions.None,
loginUri, callbackUri);
return ParseAuthenticationResult(fb, authenticationResult);
}
catch (Exception ex)
{
return ex.Message;
}
}
Use WebAuthenticationBroker.GetCurrentApplicationCallbackUri().AbsoluteUri for redirect_uri.
And update the same uri in facebook developer console.
https://developers.facebook.com/apps//settings/
Facebook developer console

Facebook API on Windows Phone

I'm struggling to post a picture from my Windows Phone 8 App using the Facebook API.
I start the process with the following
Browser.Visibility = Visibility.Visible;
var authUrl = facebookClient.GetLoginUrl(new
{
client_id = Constants.Facebook.AppId,
client_secret = Constants.Facebook.AppSecret,
scope = "publish_stream",
response_type = "token",
display = "touch",
redirect_uri = "https://www.facebook.com/connect/login_success.html"
});
Browser.Navigate(authUrl);
My Browser Navigated event only fires once with the following URL:
https://m.facebook.com/login.php?skip_api_login=1&api_key=281638808668585&signed_next=1&next=https://m.facebook.com/dialog/oauth?redirect_uri=https%253A%252F%252Fwww.facebook.com%252Fconnect%252Flogin_success.html&scope=publish_stream&response_type=token&client_id=281638808668585&ret=login&cancel_uri=https://www.facebook.com/connect/login_success.html?error=access_denied&error_code=200&error_description=Permissions+error&error_reason=user_denied%23_=_&display=touch&_rdr
I can remember the app showing me the login window once, after which I had to allow access, but it has not done so again.
I believe the Browser need to keep redirecting till facebookClient.TryParseOAuthCallbackUrl(e.Uri, out oauthResult)is successfull, after which I can continue to post the picture but it never gets there, any ideas? I don't get any exceptions

Facebook apps: Redirect to canvas iframe to URL within app (not main app page)

I'm using the Facebook PHP SDK; setting up an app on Facebook.
I need the app to run within Canvas only, so if the user goes to this url, for example:
http://{site_url}/fbapp/index.php?this=that&that=this&etc=1
I want to:
a) redirect using javascript to the canvas page (ie. http://apps.facebook.com/appname/) and then
b) the iframe must go directly to the page within the app (ie. index.php?this=that&that=this&etc=1)
I've googled this to the moon but not getting anywhere!
Any ideas??
I've tried this:
var currlocation = location.href;
if ( window.self === window.top && !currlocation.match('facebook') ) {
location.href='https://www.facebook.com/dialog/oauth?client_id=<?= $facebook->getAppID() ?>&redirect_uri=<?= $site_url; ?>?<?= $_SERVER['QUERY_STRING']; ?>'; }
It results in an indefinite redirect loop between the site and facebook.
Instead of -
location.href='https://www.facebook.com/dialog/oauth?client_id=getAppID() ?>&redirect_uri=?';
Use-
header("Location: CANVAS_URL");
exit;

how can I test my facebook page tab app from localhost

I'm developing a facebook page tab app. When I set page tab URL in app settings to something like localhost/fbapp, it doesn't work since the page tab is not simply iframe. Is there any workaround to resolve this?
EDIT
I'm building facebook page tab with ASP.NET MVC-3.0 with fb C# SDK.
What I've tried so far is to modify hosts file to point the URL I have on app settings to localhost(127.0.0.1). I've been testing facebook app in this way always but it doesn't work in facebook page tab.
In Application setting in page tab url set
http://localhost/yourpagetabdir/
I am using this setting for my page application test frmlocalhost its works fine from my ASP.Net Development server if you are using IIS server make sure your IIS server allow page in iframe.
after this if u want to get page-signed request
Facebook.FacebookConfigurationSection s = new FacebookConfigurationSection();
s.AppId = "AppID";
s.AppSecret = "Secret";
FacebookWebContext wc = new FacebookWebContext(s);
dynamic da = wc.SignedRequest.Data;
dynamic page = da.page;
string curpageid = page.id;
bool isLiked = page.liked;
bool isAdmin = page.admin;
Try editing your hosts file so that when you go to http://example.com it links to your http://localhost/yourapp
Here's how to do it:
Windows: http://www.trailheadinteractive.com/creating_multiple_virtual_sites_a_single_apache_install_windows_xp (your localhost URL should have a valid website format: 127.0.0.1 dummydomainname.tld)
MacOS: http://danilo.ariadoss.com/how-to-setup-virtual-hosts-mamp-environment/ (again, make sure you have a valid website format)
Linux: You probably know how to do this already :)
This should work as then the iframe would point to a valid URL, which on your local machine would point to your content. Note that this won't work if you access the tab from other computers since for them the URL would point to something else (or nothing, depending on what domain name you choose).
Using FB PHP SDK for example, you can post the signed_request and parse it with a few lines:-
require_once ("facebook.php"); // this file comes from the SDK
$config = array();
$config['appId'] = 'get_appID_from_app_menu';
$config['secret'] = 'get_secret_below_appID';
$facebook = new Facebook($config);
$signed_request = $facebook->getSignedRequest();
print_r($signed_request);
Avoid that lenghty example given in FB's blog. Good luck coding :)
I figured out the issue with facebook page tab. It seems that they don't accept http on page tab even I have sandbox mode enabled. All I have to do is set up IIS to work with https and run the app over the ssl. It is now working perfectly. Thank you guys for all help

Facebook Secure Forms

I am building some forms using the html input tabs in Facebook.
My form actions are post and insert linking to externally hosted php
function files.
The problem is, if the user isn't using Facebook's new https setting, a pop up
is shown with "un secure form"
Is there a why to provide a secure handler externally hosted or is this something
I would have go deeper into Facebook ap making with?
Facebook loads your app into an iframe (using POSTing a form).
When the user is using https with facebook they use the Secure Canvas URL property from the app settings, and when the user is not on secure browsing they use the Canvas URL.
Because of that, your page is not loaded from https but http, which is why that warning is shown to the user by the browser.
What you can do is to check when your page is loading which protocol is used and if it's not https then reload the page from https.
Something like (using window.location object):
var location = window.location;
if (location.protocol != "https:") {
window.location.href = "https://" + location.host + "/" + location.pathname + location.search
}
Or if you don't expect to have urls in your querystring you can simply replace:
window.location.href = window.location.href.replace("http", "https");