如何在UWP应用程序中访问oauth2令牌响应

时间:2018-02-08 19:35:21

标签: c# oauth oauth-2.0 uwp imgur

我正在开发一个使用Imgur.API imgur api包装器的UWP应用程序。

我使用

在用户的网络浏览器中打开oauth2授权网址
var success = await Windows.System.Launcher.LaunchUriAsync(imgur.getAuthorizationUrl());

哪里

  

imgur.getAuthorizationUrl()

的结果
var authorizationUrl = endpoint.GetAuthorizationUrl(Imgur.API.Enums.OAuth2ResponseType.Token);

我使用OAuth2ResponseType.Token,因为Imgur的文档说

  

只应使用令牌,因为其他方法已被弃用。

如何知道重定向网址以及如何在uwp应用程序中访问令牌数据?

1 个答案:

答案 0 :(得分:2)

要获取重定向网址,您需要在imgur developer portal中注册您的应用。 注册后,您将获得执行OAuth2流程所需的client_idsecret_keyredirect_url

要对您的UWP应用程序中的用户进行身份验证,您应该使用WebAuthenticationBroker来处理所有OAuth2流程。

以下是类文档中的示例代码摘录:

 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

相关问题