首页 > 解决方案 > Dropbox 无法使用 Xamarin.Forms 进行身份验证

问题描述

我正在尝试使用 Xamarin.Forms 在 Dropbox 中进行身份验证,我使用以下代码。

                this.oauth2State = Guid.NewGuid().ToString("N");
                var authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(OAuthResponseType.Token, AppKeyDropboxtoken, new Uri(RedirectUri), this.oauth2State, false, false, null, loginAgain);
                webView = new CustomWebview
                {
                    Source = authorizeUri.AbsoluteUri
                };
                webView.Navigating += this.WebViewOnNavigating;
                //Grid Layout
                Grid lyStack = new Grid
                {
                    Children =
                    {
                        webView,
                        lyTitle,
                    },
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    VerticalOptions = LayoutOptions.FillAndExpand,
                    Padding = new Thickness(0, 20, 0, 0),
                };
                this.Content = lyStack;

但是当我将应用程序提交到生产环境时,我收到以下错误消息:

你好呀,

您的生产密钥请求被拒绝,原因如下:

您的应用程序当前在 Web 视图中处理 OAuth 应用程序授权流程,而不是在系统浏览器中。OAuth 应用程序授权流程应在用户的系统浏览器中处理。有关更多信息,请参见此处: https ://www.dropbox.com/developers/documentation/http/documentation#oauth2-authorize 。

如果您认为您的应用不应该被拒绝,或重新提交您的请求,请发送电子邮件至 api-program@dropbox.com。

标签: xamarinxamarin.formsdropbox

解决方案


使用 Xamarin.Auth 解决了这个问题,这就是它的工作方式:

        private string oauth2State;
        private const string RedirectUri = "https://www.myredirecturl.com/en/";
        private string AccessToken { get; set; }
        private const string AppKeyDropboxtoken = "xxxxxxxxxxxxxxx";
        private void openDropbox()
        {
            this.oauth2State = Guid.NewGuid().ToString("N");
            var authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(OAuthResponseType.Token, AppKeyDropboxtoken, new Uri(RedirectUri), this.oauth2State, false, false, null, false);
            //Authorize URI
            OAuth2Authenticator auth = new OAuth2Authenticator
            (
                clientId: AppKeyDropboxtoken,
                scope: "",
                authorizeUrl: new Uri(authorizeUri.AbsoluteUri),
                redirectUrl: new Uri(RedirectUri),
                isUsingNativeUI: false
            );

            auth.Completed += (sender, eventArgs) =>
            {
                if (eventArgs.IsAuthenticated)
                {
                    Debug.Write("IsAuthenticated . . . . . ");
                    // Use eventArgs.Account to do wonderful things
                    this.AccessToken = eventArgs.Account.Properties["access_token"].ToString();
                    Debug.WriteLine("AccessToken: " + this.AccessToken);
                    //Do Something
                }
            };

            var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
            presenter.Login(auth);
        }

并在平台上初始化演示者,

您的 OnCreate 方法上的 Android

global::Xamarin.Auth.Presenters.XamarinAndroid.AuthenticationConfiguration.Init(this, bundle);

AppDelegate.cs 上的 iO

global::Xamarin.Auth.Presenters.XamarinIOS.AuthenticationConfiguration.Init();

推荐阅读