2

I am trying to deserialize a json request but getting error that

Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [2,2]

please do help me out with this error. my code :

public class connectionStoS_ctrl {

String clientId = 'jhjhjkjkjh.kjkkjkjjujbhokkoio79088kjkjmm';
String clientSecret = '56657767';
String username= '[email protected]';
String password= 'pasword';
String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;

public connectionStoS_ctrl(){
     requestToStoS();
}

public void requestToStoS(){

   //OAuth2 oA1 = new OAuth2 (12, 'test1');
   //OAuth2 oA2 = new OAuth2 (13, 'test2');

   List<OAuth2> oALst= new List<OAuth2>();
   //oALst.add(oA1);
   //oALst.add(oA2);

   Http h = new Http();
   HttpRequest req = new HttpRequest();
   req.setBody(reqbody);
   req.setMethod('GET');
   req.setEndpoint('https://login.salesforce.com');//Note if my domain is set up use the proper domain name else use login.salesforce.com for prod or developer or test.salesforce.com for sandbox instance

   System.debug('Http Request body '+ reqbody);

   System.debug('Http Request  '+ req);


   HttpResponse res = h.send(req);

   System.debug('obj Authentication Http Response '+ res.getbody() );

   //string serialz = JSON.serialize(res.getbody());
   //serialz = '{' + serialz ;
   //System.debug('obj Authentication Http Response '+ res.getbody());


   OAuth2 objAuthenticationInfo = (OAuth2) JSON.deserialize(String.valueof(res.getbody()), OAuth2.class);
   System.debug('obj Authentication Info 1 '+ JSON.deserialize(res.getbody(), OAuth2.class));

   System.debug('obj Authentication Info 2 '+objAuthenticationInfo.oauth_token);

   //if(objAuthenticationInfo.access_token != null){

   //System.debug(' Inside oAuth ');


   Http h1 = new Http();
   HttpRequest req1 = new HttpRequest();
   //req1.setHeader('Authorization','Bearer '+objAuthenticationInfo);
   req1.setHeader('Content-Type','application/json');
   req1.setHeader('accept','application/json');

   System.debug('jsonstr_BODY '+req1);


   //req1.setBody(jsonstr);//Send JSON body
   //req1.setMethod('POST');
   //req1.setEndpoint(https://login.salesforce.com/services/oauth2/callback+URL);//URL will be your Salesforce REST API end point where you will do POST,PUT,DELETE orGET
   //HttpResponse res1 = h1.send(req1);
   //system.debug('RESPONSE_BODY'+res1.getbody());

   //}
}




public class OAuth2{
     public String oauth_callback{get;set;}
     //public String issued_at{get;set;}
     //public String instance_url{get;set;}
     //public String signature{get;set;}
     public String oauth_token{get;set;}    
}
}

response body

<html>
<head></head><body onload="document.forms['workerForm'].submit()">
<form  action="https://ap1.salesforce.com/visualforce/session" id="workerForm" method="post" name="workerForm" >
    <input type="hidden" name="client_id" id="client_id" value="rCVPxkDShqbjaG_G1fAV" />
    <input type="hidden" name="client_secret" id="client_secret" value="hgj87687nb" />
    <input type="hidden" name="grant_type" id="grant_type" value="password" />
    <input type="hidden" name="password" id="password" value="kjhd7824fdjds" />
    <input type="hidden" name="url" id="url" value="https://streamingapi.ap1.visual.force.com/apex/streamingapi__ConnectionStoS_Received" />
    <input type="hidden" name="username" id="username" value="[email protected]" />
</form>

Please guide me what i am doing wrong.

1 Answer 1

5

A common cause is that the server is responding with a HTML page that contains error information rather than the JSON. Typically the status code in the response res.getStatusCode() would also be some value other than the 2xx success codes.

While you could figure this out using debug logging, its worth putting in code to handle problems in the future too such as:

    HTTPResponse res = new Http().send(req);
    if (res.getStatusCode() >= 200 && res.getStatusCode() < 300) {
        // Everthing fine
    } else {
        throw new MyCustomException('HTTP callout failed:'
                + '\nendPoint=' + req.getEndPoint()
                + '\nstatusCode=' + res.getStatusCode()
                + '\nstatus=' + res.getStatus()
                + '\nbody=' + res.getBody()
                );
    }

PS

From a quick review of Digging Deeper into OAuth 2.0 on Force.com it looks like your request should be "POSTed to https://login.salesforce.com/services/oauth2/token" but your are doing a "GET" of "https://login.salesforce.com".

6
  • yes @keith my status = OK and code is 200 every time but issue comes when I try to deserialize oAuth token from the response.
    – Anu
    Sep 8, 2014 at 3:58
  • @Anu Can you add to your question the text that comes back in the body?
    – Keith C
    Sep 8, 2014 at 7:52
  • I have updated my question with response body please check.
    – Anu
    Sep 8, 2014 at 10:12
  • @Anu See the PS.
    – Keith C
    Sep 8, 2014 at 10:23
  • Ho @Keith that was great. It is getting oAuth code now. Thanks can you please tell me after getting the oAuth we need to send the request again what need to be the setEndpoint for that.
    – Anu
    Sep 8, 2014 at 11:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .