Xamarin.Auth Facebook

mdo*_*ick 3 c# facebook oauth-2.0 xamarin

好的,我正在尝试使用Xamarin.Auth对Xamarin.iOS进行非常基本的身份验证,并收到错误:"应用程序配置不允许给定URL:一个或多个给定的URL不允许应用程序的设置.它必须与网站URL或Canvas URL匹配,或者域必须是App域之一的子域."

我已经谷歌搜索了一段时间,似乎你可能再也无法使用Xam.Auth进行Facebook - 这似乎不太可能......

这是我的示例代码(没有我的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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Kri*_*ten 7

您是否已将URL "http://www.facebook.com/connect/login_success.html"作为有效的重定向URL添加到Facebook上的应用配置中?

我希望您拥有的域中有一个URL,这看起来就像您从样本中复制的内容

  • @OussamaAlRifai - 您只需要URL,因为Xamarin Auth正在侦听webview中包含auth代码作为查询参数的重定向.通常你的应用程序服务器会这样做,但由于它是一个手机应用程序,他们劫持该URL并拉下代码以执行剩余的oauth进程.此外,它是一个额外的安全层,因为Oauth提供程序只会重定向到您在提供程序站点为其配置的URL. (2认同)