我遇到了一个问题,使用WebView来访问通过我的Xamarin表单(运行在iOS下)应用程序授权访问所必需的授权uri。我不认为问题在于WebView和特定的url,而不是Dropbox本身。
首先,我应该提到我能够使用WebView访问任意的网站,所以我知道我可以从我的应用程序中访问web。
在下面的代码中,您将看到我设置了两个按钮。第一种方法使用WebView成功地访问Xamarin的维基百科页面。第二次尝试与Dropbox联系,以便开始身份验证过程。但是,该请求在消息中失败:
错误(400)似乎你使用的应用程序提交了一个糟糕的请求。如果您想向应用程序的开发人员报告此错误,请包括以下信息。无效的redirect_uri。一定是绝对的。
最初,我认为问题在于重定向uri没有正确注册,但是如果我将被传递到webview的url复制到webview(包括下面的注释),并将其直接粘贴到safari中,则身份验证页面将按预期的方式打开。只有在使用WebView时,我才会收到错误消息。这让我觉得这不是redirect_uri,也不是url本身,而是WebView将其呈现给dropbox.com的方式。
有人能帮我吗?我还尝试使用一个在Safari / Chrome中工作的uri,并将它直接传递到webview (绕过DropboxOAuth2Helper),没有任何结果。uri将在Safari中工作,但在WebView中不起作用。
public App()
{
Button xamarinBtn = new Button();
xamarinBtn.Text = "Xamarin";
xamarinBtn.Clicked += OnButtonClicked;
xamarinBtn.StyleId = "Xamarin";
Button dropboxBtn = new Button();
dropboxBtn.Text = "Dropbox";
dropboxBtn.Clicked += OnButtonClicked;
dropboxBtn.StyleId = "Dropbox";
StackLayout layout = new StackLayout();
layout.Children.Add(xamarinBtn);
layout.Children.Add(dropboxBtn);
ContentPage page = new ContentPage();
page.Content = layout;
MainPage = new NavigationPage(page);
}
private void OnButtonClicked(object sender, EventArgs e)
{
Button btn = sender as Button;
UrlWebViewSource source = new UrlWebViewSource();
if (btn.StyleId == "Xamarin")
{
source.Url = "https://en.wikipedia.org/wiki/Xamarin";
}
else if (btn.StyleId == "Dropbox")
{
string oauth2State = Guid.NewGuid().ToString("N");
Uri authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri(OAuthResponseType.Token, "d1612up7la63slo", "http://127.0.0.1:52475/authorize", oauth2State);
source.Url = authorizeUri.AbsoluteUri; // https://www.dropbox.com/oauth2/authorize?response_type=token&client_id=d1612up7la63slo&redirect_uri=http%3A%2F%2F127.0.0.1%3A52475%2Fauthorize&state=1062a614aa3d4e2e85cd84de32903987
}
WebView webView = new WebView();
webView.Source = source;
ContentPage page = new ContentPage();
page.Content = webView;
MainPage.Navigation.PushAsync(page);
}
发布于 2018-05-11 09:15:34
您应该通过app为您的应用程序添加一个OAuth 2重定向URI:
完成此操作后,我尝试了OC代码,它工作正常,可以显示授权视图。但不幸的是,Xamarin的Dropbox失败了。您可以尝试使用另一个api来显示此视图:
Uri authorizeUri = DropboxOAuth2Helper.GetAuthorizeUri("d1612up7la63slo");
source.Url = source.Url = authorizeUri.AbsoluteUri;
https://stackoverflow.com/questions/50282440
复制相似问题