Last active
August 8, 2017 07:38
-
-
Save masatomix/99b32b0f475cad584d3240f1eac40d76 to your computer and use it in GitHub Desktop.
JerseyでJSONオブジェクトをPOSTする件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Data | |
@AllArgsConstructor | |
public class AccessTokenRequestParam { | |
private String redirect_url; | |
private String client_id; | |
private String client_secret; | |
private String grant_type; | |
private String code; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>org.glassfish.jersey.core</groupId> | |
<artifactId>jersey-client</artifactId> | |
<version>${jersey.version}</version> | |
</dependency> | |
<!-- jersyでProxyを使う場合。 --> | |
<dependency> | |
<groupId>org.glassfish.jersey.connectors</groupId> | |
<artifactId>jersey-apache-connector</artifactId> | |
<version>${jersey.version}</version> | |
</dependency> | |
<!-- jersyでJavaBeansをJSON でPOSTする場合。 --> | |
<dependency> | |
<groupId>org.glassfish.jersey.media</groupId> | |
<artifactId>jersey-media-json-jackson</artifactId> | |
<version>${jersey.version}</version> | |
</dependency> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String getAccessToken(String oauth_server, | |
String redirect_url, String client_id, String client_secret, | |
String authorizationCode, Client client) throws ServletException { | |
String result = null; | |
try { | |
log.debug("OAuthServer:{}", oauth_server); | |
String grant_type = "authorization_code"; | |
AccessTokenRequestParam entity = new AccessTokenRequestParam( | |
redirect_url, client_id, client_secret, grant_type, | |
authorizationCode); | |
Response restResponse = client // | |
.target(oauth_server) // | |
.request(MediaType.APPLICATION_JSON_TYPE) | |
// .post(Entity.entity(formParams, | |
// MediaType.APPLICATION_FORM_URLENCODED_TYPE)); | |
.post(Entity.entity(entity, | |
MediaType.APPLICATION_JSON_TYPE)); | |
result = restResponse.readEntity(String.class); | |
log.debug(result); | |
} catch (BadRequestException e) { | |
throw new ServletException(e); | |
} | |
return result; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String getAccessTokenJSON(String oauth_server, | |
String redirect_url, String client_id, String client_secret, | |
String authorizationCode, Client client) throws ServletException { | |
String grant_type = "authorization_code"; | |
String result = null; | |
// MultivaluedMap<String, String> formParams = new | |
// MultivaluedHashMap<String, String>(); | |
// formParams.putSingle("client_secret", client_secret); | |
// formParams.putSingle("client_id", client_id); | |
// formParams.putSingle("grant_type", grant_type); | |
// formParams.putSingle("redirect_uri", redirect_url); | |
// formParams.putSingle("code", authorizationCode); | |
Map<String, String> formParams = new HashMap<String, String>(); | |
formParams.put("client_secret", client_secret); | |
formParams.put("client_id", client_id); | |
formParams.put("grant_type", grant_type); | |
formParams.put("redirect_uri", redirect_url); | |
formParams.put("code", authorizationCode); | |
try { | |
log.debug("OAuthServer:{}", oauth_server); | |
Response restResponse = client // | |
.target(oauth_server) // | |
.request(MediaType.APPLICATION_JSON_TYPE) | |
// .post(Entity.entity(formParams, | |
// MediaType.APPLICATION_FORM_URLENCODED_TYPE)); | |
.post(Entity.entity(formParams, | |
MediaType.APPLICATION_JSON_TYPE)); | |
result = restResponse.readEntity(String.class); | |
log.debug(result); | |
checkAccessTokenResult(restResponse); | |
} catch (BadRequestException e) { | |
throw new ServletException(e); | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment