The remote server returned an error: (400) Bad Request.? in yahoo Oath 2.0

The remote server returned an error: (400) Bad Request.? in yahoo Oath 2.0

我正在根据 yahoo 在 https://developer.yahoo.com/oauth2/guide/ 提供的指南实施 Yahoo OAuth 2.0。我的代码在指南的第 4 步失败,上面写着 "Exchange authorization code for Access Token"。它给出了错误 "The remote server returned an error: (400) Bad Request."。 我的应用程序在 http://www.schoonheidsinstituut-antwerpen.com/yahooapi.aspx 上线,您可以在其中看到实时错误。

我的 C# 代码如下 -

string consumerKey = "mykey1";
string consumerSecret = "mykey2";
string returnUrl = "http://www.schoonheidsinstituut-antwerpen.com/yahooapi.aspx";
protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["code"] != null)
        GetAccessToken();
}
protected void yahooButton_Click(object sender, EventArgs e)
{
    /*Sending User To Authorize Access Page*/
    string url = "https://api.login.yahoo.com/oauth2/request_auth?client_id=" + consumerKey + "&redirect_uri=" + returnUrl + "&response_type=code&language=en-us";
    Response.Redirect(url);
    /*End*/
}
public void GetAccessToken()
{
    /*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";
    request.Headers["Authorization"] = "Basic";

    // 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("&code=" + Request.QueryString["code"]);
    data.Append("&grant_type=authorization_code");

    // 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();
        }
    }
    catch (Exception ex)
    {
        dataDiv.InnerHtml = ex.Message;
    }
    /*End*/        
}

第html页是-

<form id="form1" runat="server">
    <div>
        <asp:Button ID="yahooButton" Text="Get Yahoo Contact" runat="server" OnClick="yahooButton_Click" />
    </div>
    <div id="dataDiv" runat="server"></div>
</form>

我发现 HTTP Post 请求失败,错误代码为 400。有人可以帮我找到这个错误的原因吗? 谢谢。

我花了将近一周的时间来寻找答案。我终于得到了错误的东西。我忘了在雅虎文档中实现注释 "Note: The Authorization: Basic authorization header is generated through a Base64 encoding of client_id:client_secret per RFC 2617."

我在代码的 http post header 部分添加了两行代码,它们是 -

    byte[] headerByte=System.Text.Encoding.UTF8.GetBytes(consumerKey+":"+consumerSecret);
    string headerString=System.Convert.ToBase64String(headerByte);
    request.Headers["Authorization"] = "Basic " + headerString;

现在它工作正常,我从雅虎那里得到 access_token。

希望对有需要的人有所帮助。

此致

瑜珈