Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Please help me in implementing Yahoo Oath 2.0 in asp.net & C#

$
0
0

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-
<
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>
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.

Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>