• Rohini Gurav 2
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Hello,
I am tring to connect java to salesforce by using rest api..

here i am attached the code,after running this code it throughs a exception 

Code:-

import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.json.JSONObject;
import org.json.JSONTokener;

public class  JavarestApi{
private static final String clientId="3MVG9d8..z.hDcPL3l_8qXSX5sPV_vOueBOIUnrKxsH0MQdtcD0r0H13mxvNTwXV4.mt8YX1unfFha68RuDor"; 
private static final String clientSecret="1958974945496795189";
private static final String redirectUrl="https://localhost:8443/RestTest/oauth/_callback";
private static  String tokenUrl="";
private static final String environment="https://login.salesforce.com";
private static final String username="***********@gmail.com";
private static final String password="*****************************************";                   //passwordsecurity token

private static  String accessToken="";
private static  String instanceUrl="";

public static void main(String ar[]){
System.out.println("----------getting a token---------");

tokenUrl=environment+"/services/oauth2/token";
System.out.println("tokenUrl    ........."+tokenUrl);
HttpClient httpclient=new HttpClient();
//httpclient.getParams().setParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
httpclient.getParams().setParameter(HttpClientParams.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

PostMethod post=new PostMethod(tokenUrl);
post.addParameter("grant_type","password");
post.addParameter("client_id",clientId);
post.addParameter("client_secret",clientSecret);
post.addParameter("redirect_url",redirectUrl);
post.addParameter("username",username);
post.addParameter("password",password);

try{
httpclient.executeMethod(post);
JSONObject authResponse=new JSONObject(new JSONTokener (new InputStreamReader(post.getResponseBodyAsStream())));
System.out.println("Auth Response :-"+authResponse.toString(2));

accessToken=authResponse.getString("access_token");
instanceUrl=authResponse.getString("instance_url");

System.out.println("Got Access Token " +accessToken);
System.out.println("Got Instance Url " +instanceUrl);


new JavarestApi().createaccount(instanceUrl,accessToken);
}
catch(Exception e){
    System.out.println("Exception during Connect"+e);
}

}

private String createaccount(String instanceUrl,String accessToken) throws Exception{
    System.out.println("----------start account--------");
HttpClient httpclient=new HttpClient();
JSONObject account=new JSONObject();
String accountId="";

try{
account.put("Name", "Amol");
account.put("AccountNumber", "98123");

PostMethod post=new PostMethod(instanceUrl+"/services/data/v20.0/sobjects/Account/");
post.setRequestHeader("Authorization", "OAuth" +accessToken);
post.setRequestEntity(new StringRequestEntity(account.toString(),"application/x-www-form-urlencoded",null));

httpclient.executeMethod(post);
System.out.println("HTTP status"+post.getStatusCode()+"creating account\n\n");
 if(post.getStatusCode() == HttpStatus.SC_CREATED ){
     try{
         JSONObject response=new JSONObject(new JSONTokener (new InputStreamReader(post.getResponseBodyAsStream())));
         System.out.println("create response" + response.toString(2));
         if(response.getBoolean("success")){
             accountId=response.getString("id");
             
             System.out.println("new record id:-" +accountId+"\n\n");
         }
     }catch(Exception e){
         e.printStackTrace();
     }
 }


}
catch(Exception e){
    e.printStackTrace();
}
System.out.println("----------end account--------");
return accountId;
}

}



Exception :-

----------getting a token---------

tokenUrl    .........https://login.salesforce.com/services/oauth2/token
Auth Response :-{
  "access_token": "00D7F000001ZxXl!ARYAQBcTSXtZXdYDXFHlneUawyG9iqF6_dCTBPSbBCqrV6vSDPzzcNJesGFBS239x7p51xBAP4ZaqHOfgrmH7tZam2wTS3GX",
  "signature": "GFMgzpOtCxO2UuPBqGh2SHogWMdx0mk6J6X30goaFRU=",
  "instance_url": "https://ap5.salesforce.com",
  "id": "https://login.salesforce.com/id/00D7F000001ZxXlUAK/0057F000000qvkHQAQ",
  "token_type": "Bearer",
  "issued_at": "1503479401122"
}
Got Access Token 00D7F000001ZxXl!ARYAQBcTSXtZXdYDXFHlneUawyG9iqF6_dCTBPSbBCqrV6vSDPzzcNJesGFBS239x7p51xBAP4ZaqHOfgrmH7tZam2wTS3GX
Got Instance Url https://ap5.salesforce.com
----------start account--------
HTTP status401creating account


----------end account--------
Aug 23, 2017 5:09:59 AM org.apache.commons.httpclient.HttpMethodDirector processWWWAuthChallenge
WARNING: Unable to respond to any of these challenges: {token=Token}


$401unthorized http error

it not inserted the data into salesforce