Jon Selby Jon Selby - 3 months ago 25
ASP.NET (C#) Question

Linked In API (auth token (oauth2)) c#.net

I wonder if someone can give me some pointers on using the linked in API. I thought I had it sussed but for some reason when trying to get the auth_token I just get http 400 Bad Request.

My code, simply is:

public string redirectUrl = URLTOREDIRECT;

public String apiKey = APIKEY;

public String apiSecret = APISECRET;

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.QueryString["code"] != null)
{
VerifyAuthentication(Request.QueryString["code"]);
}
}
}

protected void Button1_Click(object sender, EventArgs e)
{

Response.Redirect("https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=apiKey&scope=rw_company_admin&state=DCEEFWF45453sdffef424&redirect_uri=" + HttpUtility.HtmlEncode(redirectUrl));

}

public String VerifyAuthentication(string code)
{
string authUrl = "https://www.linkedin.com/uas/oauth2/accessToken";

var sign = "grant_type=authorization_code" + "&code=" + code + "&redirect_uri=" + HttpUtility.HtmlEncode(redirectUrl) + "&client_id=" + _consumerkey + "&client_secret=" + _consumerSecret;
// var postData = String.Format("grant_type=authorization_code&code={0}&redirect_uri={1}&client_id={2}&client_secret={3}", code, HttpUtility.HtmlEncode(redirectUrl), apiKey, apiSecret);

HttpWebRequest webRequest = WebRequest.Create(authUrl + "?" + sign) as HttpWebRequest;
webRequest.Method = "POST";

//This "application/x-www-form-urlencoded"; line is important
webRequest.ContentType = "application/x-www-form-urlencoded";

webRequest.ContentLength = sign.Length;

StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(sign);
requestWriter.Close();

StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());

return responseReader.ReadToEnd().ToString() ;
}


The code is all in the same page for testing purposes and is the same page as the redirectUrl.

Really stumped, tried all variations.

First bit clearly works as I get directed to linked in to allow app. The second part getting the auth token fails.

Answer

After much head scratching, eureka moment - did not need to send the info in the request.

    string authUrl = "https://www.linkedin.com/uas/oauth2/accessToken";

    var sign = "grant_type=authorization_code&code=" + HttpUtility.UrlEncode(code) + "&redirect_uri=" + HttpUtility.HtmlEncode(redirectUrl) + "&client_id=" + apiKey + "&client_secret=" + apiSecret;
    //byte[] byteArray = Encoding.UTF8.GetBytes(sign);


    HttpWebRequest webRequest = WebRequest.Create(authUrl + "?" + sign) as HttpWebRequest;
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";

    Stream dataStream = webRequest.GetRequestStream();

    String postData = String.Empty;
    byte[] postArray = Encoding.ASCII.GetBytes(postData);

    dataStream.Write(postArray, 0, postArray.Length);
    dataStream.Close();

    WebResponse response = webRequest.GetResponse();
    dataStream = response.GetResponseStream();


    StreamReader responseReader = new StreamReader(dataStream);
    String returnVal = responseReader.ReadToEnd().ToString();
    responseReader.Close();
    dataStream.Close();
    response.Close();
    return returnVal;
Comments