Skip to content

Instantly share code, notes, and snippets.

@masatomix
Last active August 8, 2017 07:38
Show Gist options
  • Save masatomix/99b32b0f475cad584d3240f1eac40d76 to your computer and use it in GitHub Desktop.
Save masatomix/99b32b0f475cad584d3240f1eac40d76 to your computer and use it in GitHub Desktop.
JerseyでJSONオブジェクトをPOSTする件
@Data
@AllArgsConstructor
public class AccessTokenRequestParam {
private String redirect_url;
private String client_id;
private String client_secret;
private String grant_type;
private String code;
}
<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>
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;
}
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