Last post Jan 25, 2015 05:11 AM by yogihosting
Jan 22, 2015 04:28 AM|yogihosting|LINK
I am implementing Yahoo OAuth 2.0 according to the guide given by yahoo at
https://developer.yahoo.com/oauth2/guide/ . My code is failing on the step 4 of the guide which says "Exchange authorization code for Access Token". It is giving error "The remote server returned an error: (400) Bad Request.". My app is live at
http://www.schoonheidsinstituut-antwerpen.com/yahooapi.aspx where you can see the error live.
My C# code is given below -
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)
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";
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"]);
// 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 = "";
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;
Page html-<form id="form1" runat="server">
<asp:Button ID="yahooButton" Text="Get Yahoo Contact" runat="server" OnClick="yahooButton_Click" />
<div id="dataDiv" runat="server"></div>
</form>I find that the HTTP Post Request is failing with 400 error code. Can somebody please help me in finding a reason for this error? thanks.
Jan 23, 2015 01:18 AM|Shawn - MSFT|LINK
400 Bad Request means the request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.
client_id Consumer Key provided to you when you signed up.
So, you need check the step1 and step2 make sure that client_id is right after you Sign in and get credentials.
Then in step2 you need pass the client_id to the corresponding parameter within the url in step2.
I notice that you pass the consumerKey to the client_id, and you need make sure that the consumerKey is correct and get from the Yahoo when you signed up.
Jan 23, 2015 05:43 AM|yogihosting|LINK
thanks for the answer. I checked the consumer key and consumer secret once more, they are correct. I don't know why Bad Request is coming even though i am doing the step 2 correctly according to the yahoo guide?
Thanks and regards.
Jan 25, 2015 05:11 AM|yogihosting|LINK
Hello, i finally got the thing which is giving error. I forgot to implement the note in yahoo docs which says "Note: The
Basic authorization header is generated through a Base64 encoding of
I added two lines of code in my http post header section of the code which are -
request.Headers["Authorization"] = "Basic " + headerString;
Now it works fine and i am getting the access_token from yahoo.
Thanks for the help Shawn, hope it helps someone in need.