【问题标题】:How to access oauth2 token response in a UWP app如何在 UWP 应用中访问 oauth2 令牌响应
【发布时间】:2018-07-19 11:49:02
【问题描述】:

我正在开发一个使用 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 的文档说

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

如何知道使用什么重定向 url 以及如何在 uwp 应用程序中访问令牌数据?

【问题讨论】:

    标签: c# oauth oauth-2.0 uwp imgur


    【解决方案1】:

    要获取重定向 URL,您需要在 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

    【讨论】:

    • 我会试试的。谢谢
    猜你喜欢
    • 2011-03-07
    • 1970-01-01
    • 2018-07-02
    • 2014-09-04
    • 2020-11-27
    • 2013-03-01
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    相关资源
    最近更新 更多