【问题标题】:LInkedIn oAuth2 request token 400 Bad RequestLInkedIn oAuth2 请求令牌 400 错误请求
【发布时间】:2015-04-08 02:55:41
【问题描述】:

我在第二天使用 LinkedIN API,每次尝试获取令牌时,都会收到 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());
}

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

【问题讨论】:

  • 机器中的错误日期可能是导致 400 错误的原因。 (对你来说有点晚,但可能对某人有帮助)
  • 你找到答案了吗?可以发在这里吗

标签: c# asp.net-mvc linkedin


【解决方案1】:

您必须在两者中使用相同的 redirect_uri

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

还有

public AuthenticationResult VerifyAuthentication(System.Web.HttpContextBase context)

功能。但是在您的代码 redirect_uri 的第一个函数 HttpUtility.UrlEncode(returnUrl.ToString()) 和第二个函数 HttpUtility.UrlEncode(context.Request.QueryString["ReturnUrl "]) 不一样(我认为)。因此,请确保您已解决此问题。您的代码可能有效。

【讨论】:

    【解决方案2】:

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

    • HTTP 协议 1.1
    • 添加content-type: application/x-www-form-urlencoded 标头
    • 不刷新授权码返回页面的响应; URL 参数中的代码(PHP 中的$_GET['code'])显然不能重复使用(another answer 表示它每 20 秒过期一次)
      • 换一种说法,不要尝试重复使用或缓存授权码,尽快直接流入访问令牌请求中
    • 请尝试使用其他应用程序(如 SoapUI 或 Fiddlr)来访问端点以显示它正在工作,并更清楚地查看某些标题
      • 话虽如此,查看响应标头(不仅仅是响应代码)可能会有所帮助
    • 将数据作为 POST 内容而不是 URL 参数发送

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

    【讨论】:

    • 正如您所提到的,我尝试了所有方法,但仍然无法获取访问令牌,因为我的重定向 URL 也是正确的。你能帮我解决这个问题吗?分享我的代码帖子https://*.com/questions/52501144/unable-to-get-access-token-linkedin-oauth