· 7 years ago · Apr 17, 2018, 09:32 PM
1private String obtainAccessToken(String username, String password) throws Exception {
2
3 ArrayList<NameValuePair> params;
4
5 params = new ArrayList<NameValuePair>();
6 params.add(new BasicNameValuePair("grant_type", "password"));
7 params.add(new BasicNameValuePair("username", username));
8 params.add(new BasicNameValuePair("password", password));
9
10 HttpPost request = new HttpPost(new URL("http://localhost:" + 8080 + "/oauth/token/").toURI());
11
12
13 request.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
14
15
16 request.setHeader(new BasicHeader("Authorization", "Basic Z2lneTpzZWNyZXQ="));
17 request.setHeader("Content-type", "application/x-www-form-urlencoded");
18
19 System.out.println("executing request " + request.getRequestLine());
20 HttpResponse response = HttpClientBuilder.create().build().execute(request);
21
22 String test = response.getEntity().getContent().toString();
23
24 JacksonJsonParser jsonParser = new JacksonJsonParser();
25 return jsonParser.parseMap(response.getEntity().getContent().toString()).get("access_token").toString();
26 }
27
28executing request POST http://localhost:8080/oauth/token/ HTTP/1.1
2922:59:07.416 [main] DEBUG o.a.h.c.protocol.RequestAddCookies - CookieSpec selected: default
3022:59:07.438 [main] DEBUG o.a.h.c.protocol.RequestAuthCache - Auth cache not set in the context
3122:59:07.440 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://localhost:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
3222:59:07.460 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://localhost:8080][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
3322:59:07.462 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Opening connection {}->http://localhost:8080
3422:59:07.470 [main] DEBUG o.a.h.i.c.DefaultHttpClientConnectionOperator - Connecting to localhost/127.0.0.1:8080
3522:59:07.474 [main] DEBUG o.a.h.i.c.DefaultHttpClientConnectionOperator - Connection established 127.0.0.1:60428<->127.0.0.1:8080
3622:59:07.474 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Executing request POST /oauth/token/ HTTP/1.1
3722:59:07.474 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED
3822:59:07.477 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> POST /oauth/token/ HTTP/1.1
3922:59:07.477 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Authorization: Basic Z2lneTpzZWNyZXQ=
4022:59:07.477 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-type: application/x-www-form-urlencoded
4122:59:07.477 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Content-Length: 50
4222:59:07.477 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Host: localhost:8080
4322:59:07.477 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Connection: Keep-Alive
4422:59:07.477 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.5.4 (Java/1.8.0_151)
4522:59:07.477 [main] DEBUG org.apache.http.headers - http-outgoing-0 >> Accept-Encoding: gzip,deflate
4622:59:07.477 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "POST /oauth/token/ HTTP/1.1[r][n]"
4722:59:07.477 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Authorization: Basic Z2lneTpzZWNyZXQ=[r][n]"
4822:59:07.477 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-type: application/x-www-form-urlencoded[r][n]"
4922:59:07.477 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Length: 50[r][n]"
5022:59:07.477 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: localhost:8080[r][n]"
5122:59:07.477 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[r][n]"
5222:59:07.477 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.4 (Java/1.8.0_151)[r][n]"
5322:59:07.477 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[r][n]"
5422:59:07.478 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[r][n]"
5522:59:07.479 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "grant_type=password&username=ludo&password=asticot"
5622:59:07.484 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 403 Forbidden[r][n]"
5722:59:07.484 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Server: Apache-Coyote/1.1[r][n]"
5822:59:07.484 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "X-Content-Type-Options: nosniff[r][n]"
5922:59:07.484 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "X-XSS-Protection: 1; mode=block[r][n]"
6022:59:07.484 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Cache-Control: no-cache, no-store, max-age=0, must-revalidate[r][n]"
6122:59:07.484 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Pragma: no-cache[r][n]"
6222:59:07.484 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Expires: 0[r][n]"
6322:59:07.484 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "X-Frame-Options: DENY[r][n]"
6422:59:07.484 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Set-Cookie: JSESSIONID=D8C9BEE08765CCFA35DD604A990B351A; Path=/; HttpOnly[r][n]"
6522:59:07.484 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Content-Type: application/json;charset=UTF-8[r][n]"
6622:59:07.484 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Transfer-Encoding: chunked[r][n]"
6722:59:07.484 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "Date: Tue, 17 Apr 2018 20:59:07 GMT[r][n]"
6822:59:07.484 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "[r][n]"
6922:59:07.488 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 403 Forbidden
7022:59:07.488 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Server: Apache-Coyote/1.1
7122:59:07.488 [main] DEBUG org.apache.http.headers - http-outgoing-0 << X-Content-Type-Options: nosniff
7222:59:07.488 [main] DEBUG org.apache.http.headers - http-outgoing-0 << X-XSS-Protection: 1; mode=block
7322:59:07.488 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Cache-Control: no-cache, no-store, max-age=0, must-revalidate
7422:59:07.488 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Pragma: no-cache
7522:59:07.488 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Expires: 0
7622:59:07.488 [main] DEBUG org.apache.http.headers - http-outgoing-0 << X-Frame-Options: DENY
7722:59:07.488 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Set-Cookie: JSESSIONID=D8C9BEE08765CCFA35DD604A990B351A; Path=/; HttpOnly
7822:59:07.488 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Type: application/json;charset=UTF-8
7922:59:07.488 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Transfer-Encoding: chunked
8022:59:07.488 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Tue, 17 Apr 2018 20:59:07 GMT
8122:59:07.496 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Connection can be kept alive indefinitely