LinkedIN API で 2 日目と戦っています。トークンを取得しようとするたびに、400 Bad Request が発生します。
これが私のコードです。誰かがこれを手伝ってくれるでしょうか?
public void RequestAuthentication(System.Web.HttpContextBase context, System.Uri returnUrl)
{
string url = String.Format("https://www.linkedin.com/uas/oauth2/authorization?response_type=code" +
"&client_id={0}" +
"&scope={1}" +
"&state={3}" +
"&redirect_uri={2}",this._consumerKey,_scope,HttpUtility.UrlEncode(returnUrl.ToString()),Guid.NewGuid().ToString());
context.Response.Redirect(url);
}
public AuthenticationResult VerifyAuthentication(System.Web.HttpContextBase context)
{
//TODO: check CSRF
string code = context.Request.QueryString["code"];
string rawUrl = context.Request.Url.OriginalString;
//From this we need to remove code portion
rawUrl = Regex.Replace(rawUrl, "&code=[^&]*", "");
string authUrl = "https://www.linkedin.com/uas/oauth2/accessToken";
string postData = String.Format("grant_type=authorization_code&code={0}&redirect_uri={1}&client_id={2}&client_secret={3}", code,HttpUtility.UrlEncode(context.Request.QueryString["ReturnUrl"]), _consumerKey, _consumerSecret);
//WebClient client = new WebClient();
//var getReq = client.DownloadString(authUrl + "?" + postData);
HttpWebRequest webRequest = WebRequest.Create(authUrl + "?" + postData) as HttpWebRequest;
webRequest.Method = "POST";
//This "application/x-www-form-urlencoded"; line is important
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = postData.Length;
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write(postData);
requestWriter.Close();
StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
}
何か考えはありますか?多分誰かが過去に同様に解決しましたか?