[英]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授權代碼”。 它給出錯誤“遠程服務器返回錯誤:(400)錯誤的請求。”。 我的應用程序位於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發布請求失敗,並顯示400錯誤代碼。 有人可以幫我找到此錯誤的原因嗎? 謝謝。
我花了將近一個星期的時間來尋找答案。 我終於得到了錯誤的東西。 我忘記了在yahoo docs中實現該注釋,該注釋說:“注釋:授權:基本授權標頭是根據RFC 2617通過client_id:client_secret的Base64編碼生成的。”
我在代碼的http post標頭部分添加了兩行代碼,分別是-
byte[] headerByte=System.Text.Encoding.UTF8.GetBytes(consumerKey+":"+consumerSecret);
string headerString=System.Convert.ToBase64String(headerByte);
request.Headers["Authorization"] = "Basic " + headerString;
現在工作正常,我正在從Yahoo獲得access_token。
希望它能幫助需要幫助的人。
問候
瑜珈
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.