C# LInkedIn oAuth2请求令牌400错误请求

C# LInkedIn oAuth2请求令牌400错误请求,c#,asp.net-mvc,linkedin,C#,Asp.net Mvc,Linkedin,我在LinkedIN API上战斗第二天,每次我试图获得一个令牌,我都会收到400个错误请求 这是我的密码,也许有人能帮上忙 public void RequestAuthentication(System.Web.HttpContextBase context, System.Uri returnUrl) { string url = String.Format("https://www.linkedin.com/uas/oauth2/authorization?response_ty

我在LinkedIN API上战斗第二天,每次我试图获得一个令牌,我都会收到400个错误请求

这是我的密码,也许有人能帮上忙

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());
}

有什么想法吗?也许有人在过去解决过类似的问题?

您必须在这两种情况下使用相同的重定向uri

public void RequestAuthentication(System.Web.HttpContextBase context, System.Uri returnUrl)


功能。但是在您的代码中,第一个函数HttpUtility.UrlEncode(returnUrl.ToString())和第二个函数HttpUtility.UrlEncode(context.Request.QueryString[“returnUrl”])的uri是不同的(我认为)。因此,请确保您已解决此问题。你的代码可能会工作。

我刚刚调试了这个,下面是我在成功之前尝试过的一些东西。我不确定哪一个是正确的,所以我将把它们都记下来,以防您需要从某个地方开始:

  • HTTP协议1.1
  • 添加
    内容类型:application/x-www-form-urlencoded
    标题
  • 不刷新授权码返回页面的响应;URL参数(
    $\u GET['code']
    在PHP中)中的代码显然不能重复使用(表示每20秒过期一次)
    • 换句话说,不要试图重用或缓存授权代码,请尽快将其直接流到访问令牌请求中
  • 请尝试使用另一个应用程序(如SoapUI或Fiddlr)来点击端点,以显示它正在工作,并更清楚地看到一些标题
    • 也就是说,查看响应头(不仅仅是响应代码)会很有帮助
  • 将数据作为帖子内容而不是URL参数发送

请注意,
400
错误表示格式错误的请求(),而不是缺少的资源(
404
),如果您想得太快,这也可能是一个问题。

计算机中的错误日期可能是400错误的原因。(对你来说有点晚,但可能对某人有帮助)你找到答案了吗?你们能把它贴在这里吗?正如你们所提到的,我尝试了所有的方法,但仍然无法获得访问令牌,因为我的重定向URL也是正确的。你能帮我解决这个问题吗?分享我的代码帖子
https://stackoverflow.com/questions/52501144/unable-to-get-access-token-linkedin-oauth
public AuthenticationResult VerifyAuthentication(System.Web.HttpContextBase context)