链接无法检索访问令牌缺少必需参数错误

时间:2016-01-06 15:40:56

标签: java linkedin

我一直在按照位于here的文档将我的应用程序与LinkedIn集成。我在LinkedIn中创建了我的应用程序,并且能够成功检索授权代码,但在尝试获取访问令牌时出现以下错误:

  

{" error_description":"缺少必需参数,包括无效参数   参数值,参数不止一次。 :无法检索访问令牌:appId或重定向uri与授权代码或授权代码已过期不匹配","错误":" invalid_request"}

我已经验证了以下情况:

  1. 重定向uri与授权请求和访问令牌请求相同
  2. 我在发布后的20秒内使用授权码。
  3. 我包括"内容类型:application / x-www-form-urlencoded"在请求标题中
  4. 我试过的一些事情没有成功:

    1. 使用参数作为网址
    2. 的一部分发布请求
    3. 使用参数作为正文的一部分发布请求
    4. 将重定向uri作为编码和纯文本发送
    5. 使用get请求而不是帖子。
    6. 这是我目前的代码:

      linkedin_access_token_url = "https://www.linkedin.com/uas/oauth2/accessToken?"+
                      "grant_type=authorization_code"+
                      "&code="+ authCode
                      + "&redirect_uri=https://localhost:8090/ProfileSetup/linkedInAuth.jsp
                      + "&client_id=" + linkedin_client_id
                      + "&client_secret=" + linkedin_client_secret;
              HttpClient http = new DefaultHttpClient();
              HttpPost httppost = new HttpPost(linkedin_access_token_url);
              try {
                  httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
                  HttpResponse response = http.execute(httppost);
                  HttpEntity entity = response.getEntity();
                  System.out.println("status code " +
                  response.getStatusLine().getStatusCode()); 
                  System.out.println("statusreason"+
                  response.getStatusLine().getReasonPhrase());
                  InputStream stream = entity.getContent();
                  StringBuilder sb = new StringBuilder();
                  String line;
                  try {
                      br = new BufferedReader(new InputStreamReader(stream));
                      while ((line = br.readLine()) != null) {
                          sb.append(line);
                      }
                  } catch (Exception e) {
                  }
                  String resp = sb.toString();
                  System.out.println("response " + resp);
              } catch (Exception ex) {
                  System.out.println("linked in  HttpResponse Error: " + ex);
              } finally {
                  httppost.releaseConnection();
              }
      

      授权网址(实际客户端ID代替linkedin_client_id发送):

      https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=linkedin_client_id&redirect_uri=https://localhost:8090/ProfileSetup/linkedInAuth.jsp&state=0kcmjj5504tpgb9&scope=r_basicprofile
      

      有谁看到我做错了什么?如果我将这个已编译的URL并将其粘贴到浏览器中,我就可以检索访问令牌而不会出现任何问题。我的请求有问题吗?

2 个答案:

答案 0 :(得分:0)

您似乎将请求的所有参数都包含在网址中的属性中,而不是POST正文中包含x-www-form-urlencoded元素。

查看此其他Stack线程,了解有关如何在请求正文中发送值而不是URL属性的详细信息:

Sending HTTP POST Request In Java

答案 1 :(得分:0)

我终于能够弄清楚我的应用程序出了什么问题。我的本地环境未正确配置为https。将代码移动到使用https设置的开发框中可以解决问题。

相关问题