我想知道是否有人可以给我一些使用API中的链接的指示。我以为我很怀疑,但是由于某种原因,当尝试获取auth_token时,我只是收到了HTTP 400错误请求。
我的代码就是:
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() ; }
出于测试目的,代码全部位于同一页面中,并且与redirectUrl位于同一页面中。
真的很困惑,尝试了所有变化。
当我被定向链接到允许应用程序时,第一位显然很有效。第二部分获取身份验证令牌失败。
经过长时间的挠头,尤里卡时刻-不需要发送请求中的信息。
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;