Last active
August 4, 2017 04:45
-
-
Save jmertic/5844627 to your computer and use it in GitHub Desktop.
Sample Java code using Jersey and JSONObject for connecting to the new RESTful SugarCRM REST API in 6.7 and later.
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
package com.sugarcrm.client; | |
import com.sun.jersey.api.client.Client; | |
import com.sun.jersey.api.client.ClientResponse; | |
import com.sun.jersey.api.client.WebResource; | |
import net.sf.json.JSONObject; | |
public class JerseyClientPost { | |
public static void main(String[] args) { | |
try { | |
Client client = Client.create(); | |
// specify the REST web service to interact with | |
String baseurl = "<<instanceurl>>/rest/v10"; | |
WebResource webResource = client.resource(baseurl + "/oauth2/token"); | |
JSONObject jsonInput = new JSONObject() | |
.element( "grant_type", "password" ) | |
.element( "username", "<<username>>" ) | |
.element( "password", "<<password>>" ) | |
.element( "client_id", "sugar" ); | |
ClientResponse response = webResource.type("application/json") | |
.post(ClientResponse.class, jsonInput.toString()); | |
if (response.getStatus() != 201) { | |
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); | |
} | |
JSONObject jsonOutput = (JSONObject) JSONSerializer.toJSON( response.getEntity(String.class) ); | |
if ( jsonOutput.has("error") ) { | |
throw new RuntimeException("Failed : " + jsonOutput.getString("error_message")); | |
} | |
String token = jsonOutput.getString("access_token"); | |
System.out.println("Success! OAuth token is " + token); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Not sure if RuntimeException here is a good Java. See: http://stackoverflow.com/questions/499437/in-java-when-should-i-create-a-checked-exception-and-when-should-it-be-a-runti
Probably should be regular Exception or more precise type.
Not sure why you expect code 201 from Sugar on oauth2/token. It returns 200 AFAIK.
@mmarum-sugarcrm please do run an give me any feedback on changes. Don't have a Java env so I have nowhere to run it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks Ok to me as a sample, I can try running it later but I assume you've run it and this works.
I've never used Jersey before and it looks like it's GPL. But there's a long standing political battle between Oracle/Sun, IBM, and open source (Apache basically) for control of the Java ecosystem. The "com.sun." import at the top will make some people turn up their nose for the wrong reason. You might want to use the Apache HttpClient instead since it's in pretty standard use in open source and commercial Java projects and would let you do same thing just with a more agnostic "org.apache." import.
http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/trunk/src/examples/TrivialApp.java?revision=608014&view=markup