HTTP响应代码:411 for LinkedIn with ApacheOltu

时间:2017-04-26 15:46:17

标签: oauth-2.0 authorization linkedin access-token oltu

查看https://developer.linkedin.com/docs/oauth2我在这里尝试获取访问令牌。

这段代码有什么问题?

    OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
    TokenRequestBuilder r = OAuthClientRequest
            .tokenLocation("https://www.linkedin.com/oauth/v2/accessToken")
            .setCode(code)
            .setGrantType(GrantType.AUTHORIZATION_CODE)
            .setClientId(LinkedInClientID)
            .setClientSecret(LinkedInClientSecret)
            .setRedirectURI("http://localhost:8080/authenticatedLinkedIn");

    Map<String,String> m = new HashMap<String,String>();
    m.put("Content-Type", "application/x-www-form-urlencoded");

    int bodyLength = r.buildBodyMessage().getBody().length();
    System.out.println("Body l = " + bodyLength);
    m.put("Content-Length", Integer.toString(bodyLength));
    r.buildHeaderMessage().setHeaders(m);
    OAuthClientRequest request = r.buildQueryMessage();
    OAuthJSONAccessTokenResponse tk = oAuthClient.accessToken(request, OAuth.HttpMethod.POST);

我收到以下错误:

  

服务器返回HTTP响应代码:411为URL:   https://www.linkedin.com/oauth/v2/accessToken?code=AQSZfSXpQ6z3575474fhfbZmxJQofGiwtpw53Y1FnlALvKBWJgQKfJH8kvHM-3f5ZtOqndit594S2cmZrFuiNaXcBOHuSf8yMgFgr4uh-a40Ag&grant_type=authorization_code&client_secret=gHPiGTTyb1KKHPEP&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2FauthenticatedLinkedIn&client_id=86txkd469mat

1 个答案:

答案 0 :(得分:1)

您需要在正文urlenconded中发送带有参数的POST请求...所以

首先改变:

OAuthClientRequest request = r.buildQueryMessage();

使用:

OAuthClientRequest request = r.buildBodyMessage();

然后你不需要放任何标题Oltu会为你管理这个。

希望它会有所帮助。