Facebook API on Windows Phone - facebook

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

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 SDK Login URL Exposing the Client Secret?

I'm currently building a website where I'll offer the users a link to share a photo on Facebook, whereby the application would:
Provide the user with a link to request permissions from Facebook
Redirect back to the website with the access code from the user's acceptance of the permissions
Server-side post the photo to the user's photos
To generate the link for the first step, I'm doing this:
var fb = new FacebookClient();
var options = new
{
client_id = "MY_APP_ID",
client_secret = "MY_APP_SECRET",
redirect_uri = string.Format("http://localhost:51182/Home/FacebookShare?path={0}", Server.UrlEncode(path)),
response_type = "code",
scope = "publish_stream"
};
var loginUrl = fb.GetLoginUrl(options);
Then this loginUrl value is added to my MVC ViewModel and used in a link in the View.
However, I've noticed that the loginUrl contains the client_secret value in clear text. Isn't this a bad thing? Shouldn't users not be able to see the client_secret? Did I go about this the wrong way?
The fb.GetLoginUrl method is rather simple, in that it just adds anything you give it inside the options object as parameters to the URL created.
So take
client_secret = "MY_APP_SECRET",
out of your options object, and it should not show up in the login URL any more.

Graph Request Only Works With My Facebook Account

I have a web page which asks the user to log in and then proceeds to get JSon via Graph for a particular Facebook group. It builds the Uri dynamically by taking the access_token that is returned after login. It works fine when I do this, but if I try to log in with a different account, no data for the feed is returned.
One hint in this problem is when the facebook dialog screen appears, it only asks for my username/password. It doesn't ask go to the usual screen where Facebook asks for you to give permissions for "Basic Information" etc. It's just a username/password screen and then I go straight in.
This is the login code:
function login()
{
FB.login(function (response)
{
if (response.authResponse)
{
// connected
var authResponse = response.authResponse;
access_token = authResponse.accessToken;
refresh();
} else
{
// cancelled
}
});
}
One hint in this problem is when the facebook dialog screen appears,
it only asks for my username/password. It doesn't ask go to the usual
screen where Facebook asks for you to give permissions for "Basic
Information" etc. It's just a username/password screen and then I go
straight in.
This is because you've already authorized the app, so once you login it will take you straight to the app.
It works fine when I do this, but if I try to log in with a different
account, no data for the feed is returned.
Well the limited view of code you posted is fine, so something else is the problem. First debug step is to get the access token for that different account, and check the debugger to see if its tied to the appropriate user and scope.

mobile version of Facebook app going into redirect loop

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;

Facebook fan page tab and user id

As per the documentation in the following link, we can get the user id if the uer will interact with the form..
http://wiki.developers.facebook.com/ind … d_Policies
"If a viewing user interacts with the tab (like submits a form, takes an action that causes an AJAX load of new content, or follows a relative URL that loads on the tab), that user's UID is sent to the application as the fb_sig_user parameter, the profile owner's user ID is sent as the fb_sig_profile_user parameter. The viewing user's session key is key is sent only if the user authorized the application. "
In my fan page tab am I have an AJAX form which the user can submit with some value.. now I need the users id also.. how can I get this..
I tried to get the value in my AJAX submit page using $_POST['fb_sig_user'] with no success.. can anyone help me with this please..
You won't be able to get the id of the user using $_POST['fb_sig_user'] unless you authenticate the user by having this in the facebook's ajax function:
ajax.requireLogin = true;
For example, I'm retrieving it fine with this:
function do_ajax(url, div_id)
{
document.getElementById('poller_waitMessage').setStyle('display', 'block');
var ajax = new Ajax();
ajax.responseType = Ajax.FBML;
ajax.onerror = function(error)
{
new Dialog().showMessage("Error:", "Some communication error occured, Please reload the page.","Ok");
};
ajax.ondone = function(data)
{
document.getElementById('poller_waitMessage').setStyle('display', 'none');
document.getElementById(div_id).setInnerFBML(data);
}
ajax.requireLogin = true; // <----- this is important
ajax.post(url);
}
I've been happily using the form variable fb_sig_profile_user for previous apps, and when developing a new app last week, the variable was no where to be found.
Searched for several days, I was about to give up, and then the found answer:
ajax.requireLogin = true;
I understand FB cares about privacy and all, but they really need to announce these kinds of changes before just taking it away.
Million Thanks!