function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
anschoeweGanschoeweG 

Is it possible to do headless OAuth 2.0

I realize that OAuth is most useful when the user is working in a web application or desktop application that redirects to the Salesforce login screen to authenticate.  However, to start testing the wonders to the new REST API, I wanted to write a simple program without a front-end but would still allow me to authenticate using OAuth2.  Once authorized I would be able to call the normal REST commands.  I can't figure out how to login programmatically without using proxy classes for the SOAP API.

 

Is it possible to authenticate with only GETs and POSTs using Salesforce's OAuth2 service?  I'm primarily using Java and would like to use HttpGet and HttpPost to authenticate before issuing REST commands.  Any samples are appriciated.  I would obviously just put my Salesforce login credentials in my code for now.  This is just for testing.  I would like to test without having to run a full-blown web application for authentication purposes.

 

Andrew

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

You can use the oAuth2 username and password flow, see https://na3.salesforce.com/help/doc/en/remoteaccess_authenticate.htm

All Answers

SuperfellSuperfell

You can use the oAuth2 username and password flow, see https://na3.salesforce.com/help/doc/en/remoteaccess_authenticate.htm

This was selected as the best answer
anschoeweGanschoeweG

Thanks.  That looks to be what I'm looking for.  However, for the life of me, I can't seem to authorize by following those directions.  I always get the same error.

 

 

unsupported_response_type

 

 

I'm using the following Java code to authorize:

 

PostMethod post = new PostMethod("https://test.salesforce.com/services/oauth2/authorize");
Map<String, String> bodyMap = new HashMap<String, String>();
bodyMap.put("grant_type", "password");
bodyMap.put("client_id", clientId);
bodyMap.put("client_secret", clientSecret);
bodyMap.put("username", URLEncoder.encode("first.last@company.com.test", "UTF-8"));
bodyMap.put("password", "*****");
bodyMap.put("format", "json");
JSONObject jsonObject = new JSONObject(bodyMap);

post.setRequestEntity(new StringRequestEntity(jsonObject.toString(), "application/json", "UTF-8"));
httpClient.executeMethod(post);


 

 

The body contents doesn't seem to have any affect.  With or without the body, I get the same error: 

 

error=unsupported_response_type&error_description=response%20type%20not%20supported

 

Any help is greatly appreciated.  I feel like I'm really close the OAuth 2.0 Username-Password flow is the right authentication flow to follow.

 

Andrew

 

 

 

anschoeweGanschoeweG

I seem to fix part of the problem by fixing the post url.  I was incorrectly targeting the autherize path when I wanted the 'token' path.  With that change, I get a different error response.

 

 

PostMethod post = new PostMethod("https://test.salesforce.com/services/oauth2/token");
Map<String, String> bodyMap = new HashMap<String, String>();
bodyMap.put("grant_type", "password");
bodyMap.put("client_id", clientId);
bodyMap.put("client_secret", clientSecret);
bodyMap.put("username", URLEncoder.encode("first.last@company.com.test", "UTF-8"));
bodyMap.put("password", "*****");
bodyMap.put("format", "json");
JSONObject jsonObject = new JSONObject(bodyMap);

post.setRequestEntity(new StringRequestEntity(jsonObject.toString(), "application/json", "UTF-8"));
httpClient.executeMethod(post);

 

 

The new error is:

 

{"error":"unsupported_grant_type","error_description":"grant type not supported"}

 Anyone know how to solve this?  The documentation states I need to specify 'password' as the grant_type.

 

Andrew

 

SuperfellSuperfell

The POST needs to be form encoded parameters, not a json payload. there are examples (using curl) in the REST forum.

anschoeweGanschoeweG

It worked beautifully.  Thanks for all your help.  For anyone else, here how you do the OAuth 2.0 Username-Password flow authentication.

 

		PostMethod post = new PostMethod(environment + "/services/oauth2/token");
		post.addParameter("grant_type", "password");
		post.addParameter("client_id", clientId);
		post.addParameter("client_secret", clientSecret);
		post.addParameter("username", username);
		post.addParameter("password", password);
		post.addParameter("format", "json");
		
		
		httpClient.executeMethod(post);
		
		JSONObject response = new JSONObject(post.getResponseBodyAsString());
		log.info(response);
		
		String accessToken = response.getString("acccess_token");
		String instanceUrl = response.getString("instance_url");

 

 

lorris guillemotlorris guillemot
Can you send me your class "PostMethod" at this adresse : lorris97@hotmail.fr
Thanks.
 
kotha ramyakotha ramya
where am i supposed to write this code
kotha ramyakotha ramya
I am getting "error":"unsupported_grant_type","error_description":"grant type not supported" this error without the body am just trying to authorize the org