xamarin - 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。
解决方案
使用 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();
推荐阅读
- angular - 无需重新加载页面的角度路由/切换组件
- python - 如何使用 selenium 与 tabindex = -1 交互表单
- vue.js - 使用 Heroku Strapi 后端将 vue/nuxt 应用程序部署到 Netlify 时出错
- django - Django Zappa Lambda 部署“botocore.errorfactory.ResourceNotFoundException”
- javascript - 定制 Javascript 库的“现代”方式
- java - 通过 Selenium 运行 JUnit 测试用例时出错
- c# - 使用 OpenAPI 操作过滤器向需要身份验证的控制器端点添加安全要求
- java - RxJava Android:观察者不兼容类型错误
- sql-server - 剩余分配空间的 10% 时删除事务日志
- reactjs - 使用“useReducer”时如何添加副作用?