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

Java 链接到无法检索访问令牌缺少必需参数错误,java,linkedin,Java,Linkedin,我一直致力于通过以下文档将我的应用程序与LinkedIn集成。我已在LinkedIn中创建了我的应用程序,并且能够成功检索授权代码,但在尝试获取访问令牌时,我遇到以下错误: {“error_description”:“缺少必需的参数,包括无效的 参数值,参数多次:无法检索访问令牌:appId或重定向uri与授权代码不匹配或授权代码已过期,“错误”:“无效的\u请求”} 我已证实以下情况属实: 授权请求和访问令牌请求的重定向uri相同 我在授权码发出后20秒内使用授权码 我在请求头中包括“内容类型

我一直致力于通过以下文档将我的应用程序与LinkedIn集成。我已在LinkedIn中创建了我的应用程序,并且能够成功检索授权代码,但在尝试获取访问令牌时,我遇到以下错误:

{“error_description”:“缺少必需的参数,包括无效的 参数值,参数多次:无法检索访问令牌:appId或重定向uri与授权代码不匹配或授权代码已过期,“错误”:“无效的\u请求”}

我已证实以下情况属实:

  • 授权请求和访问令牌请求的重定向uri相同
  • 我在授权码发出后20秒内使用授权码
  • 我在请求头中包括“内容类型:application/x-www-form-urlencoded”
  • 有些事情我尝试过,但没有成功:

  • 将带有参数的请求作为url的一部分发布
  • 将带有参数的请求作为正文的一部分过帐
  • 以编码和纯文本形式发送重定向uri
  • 使用get请求而不是post 这是我目前的代码:

    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();
            }
    
    以及授权url(发送实际客户端id代替linkedin\u客户端id):


    有人知道我做错了什么吗?如果我获取这个编译后的url并将其粘贴到浏览器中,我就能够毫无问题地检索访问令牌。我的要求有问题吗

    看起来您将请求的所有参数作为属性包含在URL中,而不是作为
    x-www-form-urlencoded
    元素包含在
    POST
    正文中

    查看另一个堆栈线程,了解有关如何在请求正文中而不是作为URL属性发送值的详细信息:


    我终于能够找出我的应用程序出了什么问题。我的本地环境未正确配置https。将代码移动到我们使用https设置的dev box中修复了这个问题

    由于它是vi direct url,您是否尝试从post更改您的请求以查看发生了什么?我尝试了一个get请求,结果相同。您确信您已在LinkedIn应用程序的配置中配置为有效的重定向目标?是,我已经确认该url列在linkedIn上的授权重定向url下。它用于获取授权代码,如果我将编译后的url粘贴到浏览器中,我将收到正确的访问令牌json。
    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