我遇到了一个问题,使用WebView访问授权我的Xamarin表单(在iOS下运行)应用程序访问所需的授权。我不相信问题在于WebView和特定网址,而不是Dropbox API本身。
首先,我应该提一下,我可以使用WebView访问任意网站,所以我知道我可以从我的应用程序中访问网页。
在下面的代码中,您将看到我设置了两个按钮。第一个使用WebView成功访问Xamarin的Wikipedia页面。第二次尝试联系Dropbox以开始身份验证过程。但是,该请求失败并显示以下消息:
错误(400)您使用的应用似乎提交了错误的请求。如果您想向应用开发者报告此错误,请在下面提供相关信息。 redirect_uri无效。必须是绝对的uri。
最初,我认为问题在于重定向uri没有正确注册,但是如果我将网址传递给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);
}