首页 文章

使用C#for Inoreader发送带有自定义标头属性值的OAuth 2.0 POST请求

提问于
浏览
-1

我'm trying to perform user authentication via OAuth 2.0 for a UWP C# Inoreader app I'正在努力 . 有关步骤的文档可在此处找到:https://www.inoreader.com/developers/oauth .

我不知道如何编写这部分代码:

获取访问权限和刷新令牌获取AUTHORIZATION_CODE并通过向以下地址发送POST请求立即交换它以进行访问和刷新令牌:https://www.inoreader.com/oauth2/token请求:POST / oauth2 / token HTTP / 1.1主持人:www.inoreader.com内容长度:217内容类型:application / x-www-form-urlencoded用户代理:你的用户代理代码= [AUTHORIZATION_CODE]&redirect_uri = [REDIRECT_URI]&client_id = [CLIENT_ID] &client_secret = [CLIENT_SECRET]&scope =&grant_type = authorization_code请不要忘记包含Content-type标头!

也就是说,使用自定义标头发送POST请求以及传递属性值 .

2 回答

  • 0

    要在UWP中执行OAuth 2.0身份验证操作,我们通常会利用WebAuthenticationBroker Class .

    Web身份验证代理允许应用程序使用Internet身份验证和授权协议(如OpenID或OAuth)连接到在线身份提供程序 . 应用程序可以选择使用Web身份验证代理登录OAuth或基于OpenID协议的Web服务,例如许多社交网络和图片共享网站,前提是特定服务提供商已进行必要的更改 .

    有关详细信息,请参阅Web authentication broker .

    以下是使用WebAuthenticationBroker class和Windows.Web.Http.HttpClient class的示例 . WebAuthenticationBroker 类用于“ Consent page redirection ”, Windows.Web.Http.HttpClient 类用于“ Obtaining access and refresh tokens ” .

    string startURL = "https://www.inoreader.com/oauth2/auth?client_id=[CLIENT_ID]&redirect_uri=[REDIRECT_URI]&response_type=code&scope=[OPTIONAL_SCOPES]&state=[CSRF_PROTECTION_STRING]";
    
    //endURL is the REDIRECT_URI set in your application registration settings
    string endURL = "[REDIRECT_URI]";
    
    System.Uri startURI = new System.Uri(startURL);
    System.Uri endURI = new System.Uri(endURL);
    
    // Get Authorization code
    var webAuthenticationResult = 
        await Windows.Security.Authentication.Web.WebAuthenticationBroker.AuthenticateAsync( 
        Windows.Security.Authentication.Web.WebAuthenticationOptions.None, 
        startURI, 
        endURI);
    
    if (webAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
    {
        //webAuthenticationResult.ResponseData would like "https://yourredirecturi.com/?code=[AUTHORIZATION_CODE]&state=[CSRF_PROTECTION_STRING]"
        var decoder = new WwwFormUrlDecoder(new Uri(webAuthenticationResult.ResponseData).Query);
        //Get the CSRF_PROTECTION_STRING and check if it matches that one that you send during the consent page redirection.
        if (decoder.GetFirstValueByName("state") == "[CSRF_PROTECTION_STRING]")
        {
            //Get the AUTHORIZATION_CODE
            var autorizationCode = decoder.GetFirstValueByName("code");
    
            //Send a POST request
            var pairs = new Dictionary<string, string>();
            pairs.Add("code", autorizationCode);
            pairs.Add("redirect_uri", [REDIRECT_URI]);
            pairs.Add("client_id", [CLIENT_ID]);
            pairs.Add("client_secret", [CLIENT_SECRET]);
            pairs.Add("scope", [OPTIONAL_SCOPES]);
            pairs.Add("grant_type", "authorization_code");
    
            var formContent = new Windows.Web.Http.HttpFormUrlEncodedContent(pairs);
    
            var client = new Windows.Web.Http.HttpClient();
            var httpResponseMessage = await client.PostAsync(new Uri("https://www.inoreader.com/oauth2/token"), formContent);
            if (httpResponseMessage.IsSuccessStatusCode)
            {
                //The Response is a JSON string 
                string jsonString = await httpResponseMessage.Content.ReadAsStringAsync();
                var jsonObject = Windows.Data.Json.JsonObject.Parse(jsonString);
                //Obtaining access and refresh tokens
                var accessToken = jsonObject["access_token"].GetString();
                var refreshToken = jsonObject["refresh_token"].GetString();
            }
        }
    }
    

    使用HttpFormUrlEncodedContent时,它应该能够自动将 Content-type 标头设置为 application/x-www-form-urlencoded .

  • 1

相关问题