c# - Xamarin.Auth Facebook

标签 c# facebook oauth-2.0 xamarin

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

我在谷歌上搜索了一段时间,看来您可能无法再使用 Xam.Auth for 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");
        }
    }
}

最佳答案

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

我希望在您拥有的域中有一个 URL,这看起来像是您从示例中复制的内容

关于c# - Xamarin.Auth Facebook ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26435598/

相关文章:

oauth - 如何获取 Slack API 的用户 token ?

c# - 如何从 WMI 获取方法

c# - 弹出应用

c# - 从 UI 层抽象 asp.net mvc 标识

ios - 哦哦用户取消了Facebook登录。 -PARSE

android - 使用WSO2作为移动应用程序的API网关时,如何安全地处理comsumer-key和secret

c# - c#中的///和#region有什么区别?

Android facebook sdk登录报错1118578

php - 通过 facebook 登录 Laravel 社交名流结果很抱歉,出了点问题

java - 让 oauth2 与 spring-boot 和 rest 一起工作