2017-04-26 115 views
0

查看https://developer.linkedin.com/docs/oauth2我試着在這裏獲取訪問令牌。HTTP響應代碼:411 for Apache with ApacheOltu

這段代碼有什麼問題?

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網址: 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

您需要發送與urlenconded身體PARAMS POST請求。 ... so

第一次更改:

OAuthClientRequest request = r.buildQueryMessage(); 

有了:

OAuthClientRequest request = r.buildBodyMessage(); 

那麼你不需要把任何頭Oltu會對此進行管理U.

希望這將有助於。