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) 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*/ } Page html- |
↧
Please help me in implementing Yahoo Oath 2.0 in asp.net & C#
↧