簡體   English   中英

為什么我會收到未經授權的Yahoo OAuth 2.0錯誤(401)?

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

我正在實施指南中給出的Yahoo OAuth 2.0- https://developer.yahoo.com/oauth2/guide/我成功獲得了第4步中給出的訪問代碼,但在第5步中說“為新訪問使用Exchange刷新令牌”令牌”,我的代碼因錯誤而失敗-“遠程服務器返回了錯誤:(401)未經授權。” 我的應用程序放置在http://www.example.com/TutorialCode/YahooOAuth2.0/yahoooauth2.aspx中 ,它獲取了訪問令牌。 現在,我正在另一頁面的刷新令牌中請求新的訪問令牌-http: //www.example.com/TutorialCode/YahooOAuth2.0/newaccesstoken.aspx

這是刷新令牌,我將其從上一頁復制並粘貼到此頁面,然后單擊按鈕以獲取新的訪問令牌,但是失敗。 我的代碼是-

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#

 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());
    }
}

有人可以幫助我找到問題的根本原因嗎? 謝謝

您需要對請求中的參數值進行URL編碼。 它們可能包含&=類的字符,這些字符會破壞形式編碼。

除此之外,您可能希望將舊式POST方法替換為更新的,更簡單的方法,如HTTP請求中帶有post的答案中所述

您可以使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM