简体   繁体   English

为什么我会收到未经授权的Yahoo OAuth 2.0错误(401)?

[英]Why i am getting Yahoo OAuth 2.0 error (401) Unauthorized?

I am implementing Yahoo OAuth 2.0 given in the guide - https://developer.yahoo.com/oauth2/guide/ I am successful in getting the Access Code given in step 4 but in step 5 which says 'Exchange refresh token for new access token' my code is failing with error - 'The remote server returned an error: (401) Unauthorized.' 我正在实施指南中给出的Yahoo OAuth 2.0- https://developer.yahoo.com/oauth2/guide/我成功获得了第4步中给出的访问代码,但在第5步中说“为新访问使用Exchange刷新令牌”令牌”,我的代码因错误而失败-“远程服务器返回了错误:(401)未经授权。” My application is placed in http://www.example.com/TutorialCode/YahooOAuth2.0/yahoooauth2.aspx and it gets the Access Token. 我的应用程序放置在http://www.example.com/TutorialCode/YahooOAuth2.0/yahoooauth2.aspx中 ,它获取了访问令牌。 Now i am requesting the new access token from the refresh token in another page - http://www.example.com/TutorialCode/YahooOAuth2.0/newaccesstoken.aspx 现在,我正在另一页面的刷新令牌中请求新的访问令牌-http: //www.example.com/TutorialCode/YahooOAuth2.0/newaccesstoken.aspx

that is the refresh token i copy and paste from previous page to this page and click button to get new access token but it is failing. 这是刷新令牌,我将其从上一页复制并粘贴到此页面,然后单击按钮以获取新的访问令牌,但是失败。 My code is - 我的代码是-

HTML HTML

<asp:TextBox placeholder="Refresh Token" ID="refreshTokenTextBox" runat="server"></asp:TextBox>
<asp:Button ID="newAccessTokenButton" runat="server" Text="Get New Access Token" OnClick="newAccessTokenButton_Click" />
<div id="newDataDiv" runat="server"></div>

C# C#

 protected void newAccessTokenButton_Click(object sender, EventArgs e)
{
    string consumerKey = "xxxx";
    string consumerSecret = "myconsumerkey";

    string returnUrl = "http://www.example.com/TutorialCode/YahooOAuth2.0/newaccesstoken.aspx";
    //string encodedReturnUrl = System.Web.HttpUtility.UrlEncode(returnUrl);

    /*Exchange authorization code for Access Token by sending Post Request*/
    Uri address = new Uri("https://api.login.yahoo.com/oauth2/get_token");

    // Create the web request  
    HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

    // Set type to POST  
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    byte[] headerByte = System.Text.Encoding.UTF8.GetBytes(consumerKey + ":" + consumerSecret);
    string headerString = System.Convert.ToBase64String(headerByte);
    request.Headers["Authorization"] = "Basic " + headerString;

    // Create the data we want to send  
    StringBuilder data = new StringBuilder();
    data.Append("client_id=" + consumerKey);
    data.Append("&client_secret=" + consumerSecret);
    data.Append("&redirect_uri=" + returnUrl);
    data.Append("&refresh_token =" + refreshTokenTextBox.Text.Trim());
    data.Append("&grant_type=refresh_token");

    // Create a byte array of the data we want to send  
    byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

    // Set the content length in the request headers  
    request.ContentLength = byteData.Length;

    // Write data  
    using (Stream postStream = request.GetRequestStream())
    {
        postStream.Write(byteData, 0, byteData.Length);
    }

    // Get response  
    string responseFromServer = "";
    try
    {
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            // Get the response stream  
            StreamReader reader = new StreamReader(response.GetResponseStream());
            responseFromServer = reader.ReadToEnd();
            //ShowNewReceivedData(responseFromServer);
            newDataDiv.InnerHtml = responseFromServer;
        }
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message+"<br/>"+ex.ToString());
    }
}

Can somebody help me in getting the root cause of the problem? 有人可以帮助我找到问题的根本原因吗? Thanks 谢谢

You need to URL encode the parameter values in the request. 您需要对请求中的参数值进行URL编码。 They may contain characters like & or = that would break the form encoding. 它们可能包含&=类的字符,这些字符会破坏形式编码。

Other than that you may want to swap the legacy POST approach for a more recent, easier approach, as described in the answer in HTTP request with post 除此之外,您可能希望将旧式POST方法替换为更新的,更简单的方法,如HTTP请求中带有post的答案中所述

You can check your parameters with a curl command: 您可以使用curl命令检查参数:

curl -u "${consumerKey}:${consumerSecret}" -d "grant_type=refresh_token&redirect_uri=${returnUrl}&refresh_token=${refreshToken}" https://api.login.yahoo.com/oauth2/get_token

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在 Google Calendar API 中收到 (401) 未经授权的错误 - Why am I getting a (401) Unauthorized Error in Google Calendar API 为什么我会收到 401 未经授权的响应? - Why Am I Getting 401 Unauthorized Response? 401未经授权使用Yahoo OAuth - 401 Unauthorized using Yahoo OAuth 将文件字节流发送到WCF服务时,为什么会出现“ 401-未经授权”错误? - Why am I getting a “401 - Unauthorized” error when sending a file byte stream to a WCF service? 为什么我收到“远程服务器返回错误:(401) 未经授权”。 从这个代码片段? - Why am I getting 'The remote server returned an error: (401) Unauthorized.' from this code snippet? 在将Hammock与Tumblr API结合使用以检索喜欢项时,为什么会出现此401未经授权的错误? - Why am I getting this 401 unauthorized error while using Hammock with the Tumblr API to retrieve likes? 将HttpResponseMessage与安全网址一起使用时,为什么会出现未经授权的401? - Why am I getting a 401 unauthorized when using HttpResponseMessage with a secure url? Yahoo OAuth 2.0错误-95022 - Yahoo OAuth 2.0 error - 95022 升级到Xamarin Forms 2.0后,为什么会出现此错误? - Why am I getting this error after upgrading to Xamarin Forms 2.0? 应用程序更新程序-不断收到401未经授权的错误 - Application Updater - Keep getting 401 Unauthorized error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM