Java, Jersey, Salesforce and Oauth
Now that you have your Client ID, Client Secret and Password Token...
We can start doing raw HTTP communication with salesforce.
Use your REST Client and do:
POST to (empty content)
https://login.salesforce.com/services/oauth2/token?grant_&client_id=<client id>&client_secret=<client secret>&username=<username>&password=<password><password token>
You should get back something that looks like:
{
Now that we know we have a valid login request, let's try to do it programmatically:
We can start doing raw HTTP communication with salesforce.
Use your REST Client and do:
POST to (empty content)
https://login.salesforce.com/services/oauth2/token?grant_&client_id=<client id>&client_secret=<client secret>&username=<username>&password=<password><password token>
You should get back something that looks like:
{
- "id": "https://login.salesforce.com/id/...",
- "issued_at": "1424110144678",
- "token_type": "Bearer",
- "instance_url": "https://..1.salesforce.com",
- "signature": "...",
- "access_token":"..."
Now that we know we have a valid login request, let's try to do it programmatically:
import java.io.IOException;
import java.util.HashMap;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class OAuth {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
StringBuilder content = new StringBuilder();
content.append("https://login.salesforce.com/services/oauth2/token");
content.append("?grant_type=password");
content.append("&client_id=xxxxxxxx");
content.append("&client_secret=xxxxxx");
content.append("&username=xxxxxx");
content.append("&password=XXXxxxxxx");
WebTarget target = client.target(content.toString());
Response r = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity("", MediaType.APPLICATION_FORM_URLENCODED));
ObjectMapper om = new ObjectMapper();
System.out.println("AS STRING");
String s = r.readEntity(String.class);
System.out.println(s);
try {
System.out.println("AS HASHMAP");
System.out.println(om.readValue(s, HashMap.class));
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Once that works, let's improve our code a bit, to make it more generic and flexible.
Moving OAuth token into it's own class, make the OAuth provider into a singleton (we don't want to re-create token accidentally), Move the properties into a separate location
The Properties file can later be improved, as well as the rest of the code for that matter.
Moving OAuth token into it's own class, make the OAuth provider into a singleton (we don't want to re-create token accidentally), Move the properties into a separate location
The Properties file can later be improved, as well as the rest of the code for that matter.
import java.io.IOException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class OAuth {
private SFDCOAuthToken token;
/**
* Note that we are working with a single user at this time.
* For multi-user application, things are going to change
*
* @return SFDCOAuthToken
*/
public synchronized SFDCOAuthToken getOAuthToken() {
if (token != null)
return token;
else {
generateToken();
return token;
}
}
private synchronized void generateToken() {
Client client = ClientBuilder.newClient();
StringBuilder content = new StringBuilder();
content.append("https://login.salesforce.com/services/oauth2/token");
content.append("?grant_type=password");
content.append("&client_id=");
content.append(Properties.CLIENT_ID);
content.append("&client_secret=");
content.append(Properties.CLIENT_SECRET);
content.append("&username=");
content.append(Properties.USERNAME);
content.append("&password=");
content.append(Properties.PASSWORD);
content.append(Properties.PASSWORD_TOKEN);
WebTarget target = client.target(content.toString());
Response r = target.request(MediaType.APPLICATION_JSON_TYPE).post(
Entity.entity("", MediaType.APPLICATION_FORM_URLENCODED));
try {
ObjectMapper om = new ObjectMapper();
String s = r.readEntity(String.class);
token = om.readValue(s, SFDCOAuthToken.class);
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
OAuth oauth = new OAuth();
System.out.println(oauth.getOAuthToken().getAccess_token());
}
}
public class SFDCOAuthToken {
private String id;
private String issued_at;
private String token_type;
private String instance_url;
private String signature;
private String access_token;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getIssued_at() {
return issued_at;
}
public void setIssued_at(String issued_at) {
this.issued_at = issued_at;
}
public String getToken_type() {
return token_type;
}
public void setToken_type(String token_type) {
this.token_type = token_type;
}
public String getInstance_url() {
return instance_url;
}
public void setInstance_url(String instance_url) {
this.instance_url = instance_url;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
}
public class Properties {
final public static String LOGIN_URL = "xxx";
final public static String CLIENT_ID = "xxx";
final public static String CLIENT_SECRET="xxx";
final public static String USERNAME = "xxx";
final public static String PASSWORD = "xxx";
final public static String PASSWORD_TOKEN = "xxx";
}