如何在 UWP 应用程序中访问 oauth2 令牌响应
How to access oauth2 token response in a UWP app
我正在开发一个使用 Imgur.API imgur api 包装器的 UWP 应用程序。
我在用户的网络浏览器中使用
打开oauth2授权url
var success = await Windows.System.Launcher.LaunchUriAsync(imgur.getAuthorizationUrl());
哪里
imgur.getAuthorizationUrl()
是
的结果
var authorizationUrl = endpoint.GetAuthorizationUrl(Imgur.API.Enums.OAuth2ResponseType.Token);
我使用 OAuth2ResponseType.Token 因为 Imgur 的文档说
Only token should be used, as the other methods have been deprecated.
如何知道要使用什么重定向 url 以及如何访问 uwp 应用程序中的令牌数据?
要获得重定向 URL,您需要在 imgur developer portal 中注册您的应用程序。
注册后,您将获得执行 OAuth2 流程所需的 client_id
、secret_key
和 redirect_url
。
要在您的 UWP 应用程序中对用户进行身份验证,您应该使用 WebAuthenticationBroker 它将为您处理所有 OAuth2 流程。
以下是 class 文档中的示例代码摘录:
String FacebookURL = "https://www.facebook.com/dialog/oauth?client_id=" + FacebookClientID.Text + "&redirect_uri=" + Uri.EscapeUriString(FacebookCallbackUrl.Text) + "&scope=read_stream&display=popup&response_type=token";
System.Uri StartUri = new Uri(FacebookURL);
System.Uri EndUri = new Uri(FacebookCallbackUrl.Text);
WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
WebAuthenticationOptions.None,
StartUri,
EndUri);
if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
{
OutputToken(WebAuthenticationResult.ResponseData.ToString());
}
else if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.ErrorHttp)
{
OutputToken("HTTP Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseErrorDetail.ToString());
}
else
{
OutputToken("Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseStatus.ToString());
}
您可以在此处获得完整示例:WebAuthenticationBroker sample
我正在开发一个使用 Imgur.API imgur api 包装器的 UWP 应用程序。
我在用户的网络浏览器中使用
打开oauth2授权urlvar success = await Windows.System.Launcher.LaunchUriAsync(imgur.getAuthorizationUrl());
哪里
imgur.getAuthorizationUrl()
是
的结果var authorizationUrl = endpoint.GetAuthorizationUrl(Imgur.API.Enums.OAuth2ResponseType.Token);
我使用 OAuth2ResponseType.Token 因为 Imgur 的文档说
Only token should be used, as the other methods have been deprecated.
如何知道要使用什么重定向 url 以及如何访问 uwp 应用程序中的令牌数据?
要获得重定向 URL,您需要在 imgur developer portal 中注册您的应用程序。
注册后,您将获得执行 OAuth2 流程所需的 client_id
、secret_key
和 redirect_url
。
要在您的 UWP 应用程序中对用户进行身份验证,您应该使用 WebAuthenticationBroker 它将为您处理所有 OAuth2 流程。
以下是 class 文档中的示例代码摘录:
String FacebookURL = "https://www.facebook.com/dialog/oauth?client_id=" + FacebookClientID.Text + "&redirect_uri=" + Uri.EscapeUriString(FacebookCallbackUrl.Text) + "&scope=read_stream&display=popup&response_type=token";
System.Uri StartUri = new Uri(FacebookURL);
System.Uri EndUri = new Uri(FacebookCallbackUrl.Text);
WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
WebAuthenticationOptions.None,
StartUri,
EndUri);
if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
{
OutputToken(WebAuthenticationResult.ResponseData.ToString());
}
else if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.ErrorHttp)
{
OutputToken("HTTP Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseErrorDetail.ToString());
}
else
{
OutputToken("Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseStatus.ToString());
}
您可以在此处获得完整示例:WebAuthenticationBroker sample