You need to sign in to do that
Don't have an account?
![Arabinda Arabinda](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
DataBase.com - Java integration through Rest API giving error
Hi All,
I am trying to test DataBase.com and Java integartion through REST api.
I refer the link " http://www.salesforce.com/us/developer/docs/dbcom_api_rest/ " for the sample application.
While trying the same in local , its giving the below error.
HTTP/1.1 400 Bad Request [Date: Mon, 29 Jul 2013 11:32:58 GMT, Pragma: no-cache, Cache-Control: no-cache, no-store, Content-Type: application/json;charset=UTF-8, Transfer-Encoding: chunked] An error has occured.
[Error code 400 :The request couldn’t be understood, usually because the JSON or XML body contains an error.]
I am not able to get why this error is coming and also search about this over net but no solution for this problem.
Please help to find out the issue .
Thanks
Arabinda
Hi ,
Please find the code below.
public HttpResponse oauth2Login(UserCredentials userCredentials)
{
HttpResponse response = null;
this.userCredentials = userCredentials;
String loginHostUri = "https://" + userCredentials.loginInstanceDomain + OAUTH_ENDPOINT;
try
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(loginHostUri);
StringBuffer requestBodyText = new StringBuffer("grant_type=password");
requestBodyText.append("&username=");
requestBodyText.append(userCredentials.userName);
requestBodyText.append("&password=");
requestBodyText.append(userCredentials.password);
requestBodyText.append("&client_id=");
requestBodyText.append(userCredentials.consumerKey);
requestBodyText.append("&client_secret=");
requestBodyText.append(userCredentials.consumerSecret);
StringEntity requestBody = new StringEntity(requestBodyText.toString());
requestBody.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(requestBody);
httpPost.addHeader(prettyPrintHeader);
response = httpClient.execute(httpPost);
System.out.println("\nResponse is : " + response);
if(response.getStatusLine().getStatusCode() == 200)
{
InputStreamReader inputStream = new InputStreamReader(response.getEntity().getContent());
oauth2Response = gson.fromJson( inputStream,OAuth2Response.class );
restUri = oauth2Response.instance_url + REST_ENDPOINT +"/v" + this.userCredentials.apiVersion +".0";
System.out.println("\nSuccessfully logged in to instance: " + restUri);
oauthHeader = new BasicHeader("Authorization", "OAuth " + oauth2Response.access_token);
}
else
{
System.out.println("An error has occured.");
System.exit(-1);
}
}
catch (UnsupportedEncodingException uee)
{
uee.printStackTrace();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
catch (NullPointerException npe)
{
npe.printStackTrace();
}
return response;
}
I am using the same code as provided in the DataBase.com introduction document.
Thanks
Arabinda
Hi ,
One of my friend found the problem.I did setup and implemented the sample code properly as per document.
But while passing the password I used to give password only .
But in Salesforce we need to password+security token as password.
Thanks
Arabinda