Facebook Login for Windows Phone 8.1 - c#

I am trying to figure out a simple code for logging in facebook from a Windows Phone 8.1 app (C#).
As the Facebook Client from NuGet doesn't target Windows Phone 8.1, it seems I'd have to write some extra code. As I read in this facebook post, I'd need to launch an Uri to invoke the Login Dialog. That much, I managed to do :
await Launcher.LaunchUriAsync(FacebookUri.DisplayLoginDialog);
where DisplayLoginDialog is a static string object with the required necessary data on the request (appId, productId, permissions, etc). I was redirected to the facebook app, to accept that my app requires such permissions.
And I accepted.
So what now? How do I get a response or something with the access_token? I have researched a lot for this but I wasn't able to find relevant posts.
The same facebook link from above, says at the section of Handling the Login Dialog that :
If someone successfully logs in, the URI association of your app will be automatically triggered, meaning they will be sent to your app, along with an access token:
msft-{ProductID}://authorize/?
access_token={user-access-token}&
expires_in={expiration-time-of-token}
But I am confused on how to actually use this in C#.
How do I get a response with the access token after the login suceeded or and error code and error message if it failed, like it is written in the facebook post?

In the Package.appxmanifest file, go to 'Declarations' Tab and add a Protocol Declaration.
In the Name Field, enter your Microsoft product id as 'msft-PRODUCT_ID' [product id without dashes].
In the App.xaml.cs file, add the following code
protected override void OnActivated(IActivatedEventArgs args)
{
if (args.Kind == ActivationKind.Protocol)
{
ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
Uri responseUri = eventArgs.Uri;
//Now you can use responseUri to retrieve the access token and expiration time of token
}
base.OnActivated(args);
}

You might want to take a look at this: Sign into Windows Phone 8 apps with Facebook Login.
This tutorial on the Microsoft blogs shows you how you can tie directly into the Facebook app from your app like many do on Android and iOS.

Related

Facebook Login Error 191 when use IIS Virtual Directory

I already look other reference article but still can't solve my problem
I own a site named https://shop.yourdomain.com/
Because all my users use Android in App view to browse my site, I manually build a facebook login flow and It works perfectly.
(ref:https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow)
My problem is below:
I build another website and use IIS virtual directory and use it as a application and URL is
https://shop.yourdomain.com/second-site/
but this time when I use facebook login it will return info below.
API Error Code: 191
API Type OAuthException: OAuthException
API Error Description: The specified URL is not owned by the application
Error Message: redirect_uri is not owned by the application.
Already Set Facebook Login APP
1.Setting -> Basic -> Domain add https://shop.yourdomain.com/
2.Facebook login -> Setting -> Valid OAuth Redirect URI add https://shop.yourdomain.com/
3.Redirect Strict Mode -> off
Ever Tried
1.I tried adding https://shop.yourdomain.com/second-site/ to Facebook login -> Setting -> Valid OAuth Redirect URI, but it still didn't work.
2.When I develop this website on my own computer, I use http://localhost:port/ and add http://localhost:port on facebook application setting, and login flow is work.
Has anyone ever encountered a problem of that kind? Please help me. Thank you so much.
Logic Flow Detail is below:
1.After user click 'login' button, then Javascript redirect to Facebook Oauth
location.href = "https://www.facebook.com/v2.12/dialog/oauth
?client_id=XXX
&redirect_uri=https://shop.yourdomain.com/second-site/login/facebooklogin/
&state=parameters
2.(Redirect to Facebook Domain)User Login in Facebook or User auth Facebook for my website
3.Facebook HttpGet Back To my WebSite
[HttpGet]
public ActionResult FacebookLogin(string code, string error, string state)
{
try
{
HttpClient client = new HttpClient();
string facebookUrl = "https://graph.facebook.com/";
string facebookMethod = "v2.12/oauth/access_token?client_id=XXX
&redirect_uri=https://shop.yourdomain.com/second-site/login/facebooklogin/
&client_secret=OOO&code=code;
var facebookToken = client.GetAsync(facebookUrl + facebookMethod).Result;
string facebookTokenString = facebookToken.Content.ReadAsStringAsync().Result;
JObject facebookTokenResult = JObject.Parse(facebookTokenString);
}
catch(Exception e)
{
//Handle Exception
}
}
4. facebookTokenResult is below
{
"error":{
"message":"Can't Load this website : this website is not include in application domain. If you want to load this website, please add all domain and subdomain in 'application domain'(I translate it in English)",
"type":"OAuthException",
"code":191,
"fbtrace_id":"XXXOOO"
}
}
I finally find the answer and is not about the any facebook setting.
The problem is the http and https and I only set facebook allow https.
I make a mistake that I put only http url in c# code and that is why facebook always tell me that domain is not allowed.

ADAL for Windows Phone 8.1 Problems

I'm creating a Windows Phone 8.1 app (Windows Runtime) that will need to authenticate against an Azure Active Directory OAuth endpoint. I'm using the ADAL for WP81 nuget package as the authentication manager to get the OAuth token back.
The problem I am struggling with, is where I need to call the various ADAL Login methods in the Phone page lifecycle. Right now I'm calling AuthenticationContext.AquireTokenAndContine() in the Page.Loaded event. I've also implemented the ContinuationManager, IWebAuthenticationContinuable, and App.Activated events as described in the github sample code. I'm also using Windows.Security.Authentication.Web.WebAuthenticationBroker.GetCurrentApplicationCallbackUri() to get my client URI.
No matter what I do, I continue to get the following error. Any insight on what i can do? Thank you in advance for your help.
'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalException'
occurred in mscorlib.ni.dll but was not handled in user code
Additional information: authentication_ui_failed: The browser based
authentication dialog failed to complete
async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
await LoadFromViewModel();
}
public async Task LoadFromViewModel()
{
// Try to get a token without triggering any user prompt.
// ADAL will check whether the requested token is in the cache or can be obtained without user itneraction (e.g. via a refresh token).
AuthenticationResult result = await authContext.AcquireTokenSilentAsync(this.viewModel.RESTApiResourceUri, this.viewModel.AzureADClientId);
if (result != null && result.Status == AuthenticationStatus.Success)
{
// A token was successfully retrieved. Get the To Do list for the current user
await viewModel.BindData();
}
else
{
// Acquiring a token without user interaction was not possible.
// Trigger an authentication experience and specify that once a token has been obtained the GetTodoList method should be called
authContext.AcquireTokenAndContinue(this.viewModel.RESTApiResourceUri, this.viewModel.AzureADClientId, this.viewModel.AzureADRedirectUri, AuthenticationSuceeded);
}
}
ADAL uses the WebAUthenticationBroker (WAB) for displaying its prompts. The WAB in Windows Phone 8.1 will not show up until the entire UX of the app has been loaded, hence your current method location won't work - at least until the WAB behavior doesn't change. Please see Authentication failed with Azure Active Directory in Windows Phone for a similar thread.

Sign in to multiple Microsoft account in Windows Store app

Is it possible to:
sign-in to multiple Microsoft accounts in a Windows Store app? Mail app does it*
sign-in to Microsoft account that is not the one used for logging in to Windows? Store app does it*
I tried something like the following (taken from Live SDK sample code) but it only sign me in with the account I used to log in to Windows
var client = new LiveAuthClient();
var result = await client.LoginAsync(new[] { "wl.basic" });
if (result.Status == LiveConnectSessionStatus.Connected)
{
this.AddAccountButton.Content = "connected";
}
Appreciate any help.
* I'm not sure if the built-in apps use different API's to do this
I had a very similar issue, I've been told that's not possible, here: link
I found out, that if the user use the Windows with a Microsoft account, there is no possibilities for multiple accounts. But in case of a local account, you can implement a signing off logic to your app, and it won't log you in automatically.

Facebook login throws "Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed

I know this question was already asked many times, but still It seems to throw this error altough I tried several url addresses.
I'm trying to make facebook login through a asp.net mvc app like in microsoft tutorial
here.
When I check it on local host it works fine (Site URL in facebook is set to: http://localhost:55797/
but when I try to check it after I upload the app to the server it gives me this error:
Given URL is not allowed by the Application configuration.: One or more of the given URLs is not allowed by the App's settings. It must match the Website URL or Canvas URL, or the domain must be a subdomain of one of the App's domains.
My server's login page is: http://proj.ruppin.ac.il/igroup20/test1/Account/Login.aspx
I tried to write the following in my facebook app settings Site URL:
*http://proj.ruppin.ac.il/
*http://proj.ruppin.ac.il/igroup20/
*http://proj.ruppin.ac.il/igroup20/test1/
*http://proj.ruppin.ac.il/igroup20/test1/Account/
*http://proj.ruppin.ac.il/igroup20/test1/Account/Login.aspx
but none of them works for me.
this is my AuthConfig.cs:
public static void RegisterOpenAuth()
{
// See http://go.microsoft.com/fwlink/?LinkId=252803 for details on setting up this ASP.NET
// application to support logging in via external services.
//OpenAuth.AuthenticationClients.AddTwitter(
// consumerKey: "your Twitter consumer key",
// consumerSecret: "your Twitter consumer secret");
OpenAuth.AuthenticationClients.AddFacebook(
appId: "474928559284763",
appSecret: "****"); //hidden just for this question
//OpenAuth.AuthenticationClients.AddMicrosoft(
// clientId: "your Microsoft account client id",
// clientSecret: "your Microsoft account client secret");
//OpenAuth.AuthenticationClients.AddGoogle();
}
EDIT:
those are the screens that seems relevant to me, but I seem to have different gui, not like Tommy for example down here in this post:
EDIT2:
I just noticed there is a place for app domains and wrote there "proj.ruppin.ac.il" and in the web site url: "http://proj.ruppin.ac.il/igroup20/test1/" but still same error
For the benefit of searchers, I had the same when creating a test version of a site.
The answer was to go into the facebook site > Apps > My App > Settings > 'Advanced' tab and add the localhost address in the section titled 'Valid OAuth redirect URIs'.
Note: The same style of work will be required for an xyz.azurewebsites.net domain as well as a .com when you are testing.
Try this! These steps worked for my fb app hosted locally on http://localhost:3000/
Once signed in to your app on developers.facebook.com/...
Click Settings > Basic
Leave the "App Domains" field blank.
Click + Add Platform > Website
Use http://localhost:3000/ for both "Site URL" and "Mobile Site URL" fields
Click Save Changes
FINALLY!
You will need to update your App Domains as well in the Facebook Application configuration module to proj.ruppin.ac.il (Image taken from referenced tutorial to show that there are 2 places to update when changing FB app login URL)
Here's all the things I did to get this working
Nothing in "App Domains"
Put the server name (with port) in "Site URL"
In the "App Details" tab turn on your "Website" (I have the mobile website on) http://cl.ly/image/0h0D0L050Z2v (Why is this separate from setting the URL?)
Quadruple check your appid. I swear mine changed. But maybe I'm hallucinating that.
I got it to work with example.com in the settings. No http://www. It should work for you too (and you can add more that one domain.
Your issue is with your Facebook JavaScript SDK implementation and your Facebook app settings. The JS SDK is failing to initialize because it's been given the "wrong" settings.
The App Domain should be proj.ruppin.ac.il, and the Site URL should be http://proj.ruppin.ac.il/igroup20/test1/. It appears you've already done that. If so, make sure it saved properly, and you're not receiving any validation issues.
Lastly, go to your Facebook App's "Dashboard" and look for any warnings. For example, you may see Sandbox Mode is enabled; you may need to disable it before you can use the Facebook JS SDK.
Like others, I'm still seeing the "old" Facebook apps panel, but if you could provide a full screen capture of the "Dashboard" and "Settings" panes, we could be more helpful.
For the errors like top level domains are not allowed we need to provide both website and mobile url along with port number this works for me fine
you just need to also include the port number for your local host.
you can give the url like this:
1. https://developers.facebook.com/x/apps/662194763843967/settings/
Add your dome at App Domains in app settings
see don't use http://
suppose your domain is
http://localhost/abhinav/
then write localhost/abhinav/
click on add platform
let say its a web app
write your full site url at website text-box
Site URL : http://localhost/abhinav/
Below is the reference image where you can see what should you place at App Domains
http://i.stack.imgur.com/qZVkk.png\
So,there is some bug in Facebook and its APIs-
There is currently a bug which will prevent Facebook Login for Windows Phone
from working if you have any entries in
the "Valid OAuth redirect URIs" field in the Advanced section of your app settings.
This can be worked around by adding "https://m.facebook.com/dialog/return/ms"
in this field.
Try different links this one worked for me
"http://www.facebook.com/"

Facebook authorize not working [duplicate]

I'm trying to integrate my project with Facebook. I'm taking baby steps at first and just trying to login, get a Facebook session, and get some user data. I'm developing it locally so my Facebook application settings are:
site URL: http://127.0.0.1:8888/mySite/
The canvas URL is the same as above. I haven't specified a site domain.
However, when I click on the login button, I get an error:
API Error Code: 191
API Error Description: The specified URL is not owned by the application
Error Message: redirect_uri is not owned by the application.
At the moment I haven't written any server-side code to deal with redirects, etc. All I've done is add the JavaScript SDK based on the tutorial in Facebook for Websites.
What have I done wrong? It's obviously something to do with my Facebook application settings, but I can't see what!
UPDATE:
To answer the API Error Code: 191
The redirect_uri should be equal (or relative) to the Site URL.
Tip: Use base URLs instead of full URLs pointing to specific pages.
NOT RECOMMENDED: For example, if you use www.mydomain.com/fb/test.html as your Site URL and having www.mydomain.com/fb/secondPage.html as redirect_uri this will give you the 191 error.
RECOMMENDED: So instead have your Site URL set to a base URL like: www.mydomain.com/ OR www.mydomain.com/fb/.
I went through the Facebook Python sample application today, and I was shocked it was stating clearly that you can use http://localhost:8080/ as Site URL if you are developing locally:
Configure the Site URL, and point it
to your Web Server. If you're
developing locally, you can use
http://localhost:8080/
While I was sure you can't do that, based on my own experience (very old test though) it seems that you actually CAN test your Facebook application locally!
So I picked up an old application of mine and edited its name, Site URL and Canvas URL:
Site URL: http://localhost:80/fblocal/
I downloaded the latest Facebook PHP-SDK and threw it in my xampp/htdocs/fblocal/ folder.
But I got the same error as yours! I noticed that XAMPP is doing an automatic redirection to http://localhost/fblocal/ so I changed the setting to simply http://localhost/fblocal/ and the error was gone BUT I had to remove the application (from privacy settings) and re-install my application and here are the results:
After that, asked for the publish_stream permission, and I was able to publish to my profile (using the PHP-SDK):
$user = $facebook->getUser();
if ($user) {
try {
$post = $facebook->api('/me/feed', 'post', array('message'=>'Hello World, from localhost!'));
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
Results:
For me, it was a missing app domain. Go into the app, and make sure that you have the root of your site set up as an app domain. See screenshot.
This is just because of a URL mistake.
Whatever website URL is specified should be correct.
I mentioned website URL as http://localhost:3000/
and domain as localhost, but in my browser I was running http://0.0.0.0:3000/.
When I ran server as localhost:3000 it solved the problem.
As I mentioned, the site URL as localhost Facebook will redirect to the same, if we are running
0.0.0.0:3000, it will rise error that "Given URL is not allowed by the Application configuration".
I was also facing the same problem when I am using the facebook authentication method.
But I rectify that issue with following changes in Facebook api (Apps >> My App >> Basic).
I removed the url which i have given in
===> App on Facebook (Canvas URLs)
I gave site url only in
===> Website with Facebook Login option
Then i gave that AppId and App Secret in my webpage.
So by clicking on login button, It ask for access permissions then it redirect it to give url (Website with Facebook Login ).
I fixed this by passing the redirect url to the FacebookRedirectLoginHelper::getAccessToken() in my callback function:
Changing from
try {
$accessToken = $helper->getAccessToken();
}
...
to
try {
$accessToken = $helper->getAccessToken($fbRedirectUrl);
}
...
I am developing on a vagrant box, and it seems FacebookRedirectLoginHelper::getCurrentUrl() had issues generating a valid url.
Had the same problem:
$params = array('redirect_uri' => 'facebook.com/pages/foobar-dev');
$facebook->getLoginUrl($params);
When I changed the redirect_uri from the devloper page to the live page, 191 Error came up.
So I deleted the $params:
$facebook->getLoginUrl();
After the app-request now FB redirects to the app url itself f.e.: my.domain.com
What I do now is checking in index.php of my app if I'm inside FB iframe or not. If not I redirect to the live FB page f.e.:
$app = 'facebook.com/pages/foobar-live';
$rd = (isset($_SERVER['HTTP_REFERER'])) ? parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) : false;
if ($rd == 'apps.facebook.com' || (!isset($_REQUEST['signed_request']))) {
echo '<script>window.parent.location = "'.$app.'";</script>';
die();
}
I have noticed also that even if you specify your website under secion - Website With Facebook Login -> Site url as e.g. http://example.com, but if your App Domains section is empty, and you open website as www.example.com you will get this error, too. To avoid it in "App Domains" section write example.com, that will allow subdomains, like www.example.com, something.example.com etc
in the facebook App Page, goto the basic tab. find "Website with Facebook Login" Option.
you will find Site URL: input there put the full URL ( for example http://Mywebsite.com/MyLogin.aspx ). this is the URL you can use with the call like If the APP ID is 123456789
https://graph.facebook.com/oauth/authorize?client_id=123456789&redirect_uri=http://Mywebsite/MyLogin.aspx&scope=publish_actions
Something I'd like to add, since this is error 191 first question on google:
When redirecting to facebook instead of your own site for a signed request, you might experience this error if the user has secure browsing on and your app does redirect to facebook without SSL.
Working locally...
I couldn't get the feeds api to work, but the share api worked pretty much straight away with no problems.
For me it's the Valid OAuth Redirect URIs in Facebook Login Settings. The 191 error is gone once I updated the correct redirect URI.

Categories