简体   繁体   English

Xamarin.Auth Facebook

[英]Xamarin.Auth Facebook

Ok so I am trying to use Xamarin.Auth to do a very basic authentication on Xamarin.iOS and am getting an 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." 好的,我正在尝试使用Xamarin.Auth对Xamarin.iOS进行非常基本的身份验证,并收到错误:“应用程序配置不允许给定URL:一个或多个给定的URL不允许应用程序的设置。它必须与网站URL或Canvas URL匹配,或者域必须是应用程序域之一的子域。“

I've been Googling for a while and it seems that you may no longer be able to use Xam.Auth for Facebook -- that seems unlikely... 我已经谷歌搜索了一段时间,似乎你可能再也无法使用Xam.Auth进行Facebook - 这似乎不太可能......

Here is my sample code (sans my FB App Id) -- You'll notice it is literally a copy of Xam's sample code: 这是我的示例代码(没有我的FB应用程序ID) - 您会注意到它实际上是Xam的示例代码的副本:

using System;
using System.Collections.Generic;
using System.Json;
using System.Linq;
using System.Threading.Tasks;
using MonoTouch.Dialog;

#if __UNIFIED__
using Foundation;
using UIKit;
#else
using MonoTouch.Foundation;
using MonoTouch.UIKit;
#endif

namespace Xamarin.Auth.Sample.iOS
{
    [Register ("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
    {
        void LoginToFacebook (bool allowCancel)
        {
            var auth = new OAuth2Authenticator (
                clientId: "SOME_ID",
                scope: "",
                authorizeUrl: new Uri ("https://m.facebook.com/dialog/oauth/"),
                redirectUrl: new Uri ("http://www.facebook.com/connect/login_success.html"));

            auth.AllowCancel = allowCancel;

            // If authorization succeeds or is canceled, .Completed will be fired.
            auth.Completed += (s, e) =>
            {
                // We presented the UI, so it's up to us to dismiss it.
                dialog.DismissViewController (true, null);

                if (!e.IsAuthenticated) {
                    facebookStatus.Caption = "Not authorized";
                    dialog.ReloadData();
                    return;
                }

                // Now that we're logged in, make a OAuth2 request to get the user's info.
                var request = new OAuth2Request("GET", new Uri ("https://graph.facebook.com/me"), null, e.Account);
                request.GetResponseAsync().ContinueWith (t => {
                    if (t.IsFaulted)
                        facebookStatus.Caption = "Error: " + t.Exception.InnerException.Message;
                    else if (t.IsCanceled)
                        facebookStatus.Caption = "Canceled";
                    else
                    {
                        var obj = JsonValue.Parse(t.Result.GetResponseText());
                        facebookStatus.Caption = "Logged in as " + obj["name"];
                    }

                    dialog.ReloadData();
                }, uiScheduler);
            };

            UIViewController vc = auth.GetUI ();
            dialog.PresentViewController (vc, true, null);
        }

        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            facebook = new Section ("Facebook");
            facebook.Add (new StyledStringElement("Log in", () => LoginToFacebook (true)));         
            facebook.Add (new StyledStringElement("Log in (no cancel)", () => LoginToFacebook (false)));
            facebook.Add (facebookStatus = new StringElement (String.Empty));

            dialog = new DialogViewController (new RootElement ("Xamarin.Auth Sample") {
                facebook,
            });

            window = new UIWindow (UIScreen.MainScreen.Bounds);
            window.RootViewController = new UINavigationController (dialog);
            window.MakeKeyAndVisible ();

            return true;
        }

        private readonly TaskScheduler uiScheduler = 
            TaskScheduler.FromCurrentSynchronizationContext();

        UIWindow window;
        DialogViewController dialog;

        Section facebook;
        StringElement facebookStatus;

        // This is the main entry point of the application.
        static void Main (string[] args)
        {
            UIApplication.Main (args, null, "AppDelegate");
        }
    }
}

Have you added the URL "http://www.facebook.com/connect/login_success.html" as a valid redirect URL to your app's configuration on Facebook? 您是否已将URL "http://www.facebook.com/connect/login_success.html"添加为Facebook上应用配置的有效重定向网址?

I would expect a URL in a domain you own, this looks like something you copied from a sample 我希望您拥有的域中有一个URL,这看起来就像您从样本中复制的内容

redirect_url depends on service provider and app type defined on server side (google calls it console). redirect_url取决于服务器端定义的服务提供商和应用类型(谷歌称之为控制台)。 Basically there are 2 types of apps: Server-AKA-Web and Mobile-AKA-Installed. 基本上有两种类型的应用程序:Server-AKA-Web和Mobile-AKA-Installed。 Server-AKA-Web uses almost always authorization code grant while Mobile-AKA-Installed can use implicit grant flow or modifies authorization code grant flow where client_secret is not sent (and not present on the device), because it is considered to be insecure. Server-AKA-Web几乎总是使用授权代码授权,而Mobile-AKA-Installed可以使用隐式授权流或修改授权代码授权流,其中client_secret未发送(并且不存在于设备上),因为它被认为是不安全的。 Server-AKA-Web is used on servers (web apps) where it is a lot more complicated for malicious user to get the client_secret. Server-AKA-Web用于服务器(Web应用程序),恶意用户获取client_secret要复杂得多。 Server can open any web page (http[s] scheme) - usually some page/route/path on the same host, but it could be something else. 服务器可以打开任何网页(http [s]方案) - 通常是同一主机上的一些页面/路由/路径,但它可能是其他东西。

Xamarin.Auth used this approach of opening some page in browser and analysing the url to grab OAuth data returned. Xamarin.Auth使用这种方法在浏览器中打开一些页面并分析url以获取返回的OAuth数据。 It could not parse http[s]://localhost what is valid redirect_url, but mobile apps have no web server to have localhost loadable page. 它无法解析http [s]:// localhost什么是有效的redirect_url,但移动应用程序没有Web服务器可以使用localhost可加载页面。 Moreover Xamarin.Auth could never work with custom schemes like fb22145312 for facebook. 此外,Xamarin.Auth永远无法使用像fb22145312这样的自定义方案。

This changed with version v.1.4 which added support for custom schemes required now with google OAuth authentication for Mobile-AKA-Installed apps where embedded WebViews are forbidden and mobile app must use so called Native UI - on Android [Chrome] CustomTabs and on iOS SFSafariViewController. 这在版本v.1.4中发生了变化,该版本增加了对现在所需的自定义方案的支持,其中包含针对Mobile-AKA安装的应用的Google OAuth身份验证,其中禁止嵌入式WebView,移动应用必须使用所谓的原生用户界面 - 在Android [Chrome] CustomTabs和iOS上SFSafariViewController。 They need custom schemes for deep-AKA-app linking, so redirect_url can be intercepted by mobile app that registered for that scheme. 他们需要针对深度AKA-app链接的自定义方案,因此redirect_url可以被注册该方案的移动应用程序拦截。

So if your app is web app then you can use http[s] scheme for redirect_url, but if you have chosen Android or iOS app then provider (Facebook in this case) will generate scheme for the app which must be used. 因此,如果您的应用程序是Web应用程序,那么您可以使用http [s]方案进行redirect_url,但如果您选择了Android或iOS应用程序,那么提供商(在这种情况下为Facebook)将为应用程序生成必须使用的方案。

Providers have different ways of checking validity of the requests and seems that other data for your app on server side (Website, valid Urls are different from your redirect_url). 提供商有不同的方法来检查请求的有效性,并且似乎服务器端的应用程序的其他数据(网站,有效的URL与您的redirect_url不同)。 This explains last part of the error message you are getting. 这解释了您收到的错误消息的最后一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM