-2

I am trying to connect another org using user agent auth flow, I am getting auth code and passing it to apex method and then I am getting the below error.

private static final string RESOURCE_REDIRECT_URI = 'https://xxxx.lightning.force.com/lightning/n/Connected_Orgs';
    private static final string RESOURCE_CLIENT_ID = 'XXXXXXX';
    private static final string RESOURCE_CLIENT_SECRET = 'XXXXX';
public Static String getAccesstoken(String Accesscode){         

         Accesscode = Accesscode.replace('=','%3D').trim();        
         Accesscode = Accesscode.replace('"', '');

         String loginUri = 'https://test.salesforce.com';
         String header = '';
         String reqbody =  'grant_type=authorization_code';
                reqbody += '&client_id='+ EncodingUtil.urlEncode(RESOURCE_CLIENT_ID, 'UTF-8');
                reqbody += '&client_secret='+ EncodingUtil.urlEncode(RESOURCE_CLIENT_SECRET, 'UTF-8');
                reqbody += '&code='+ EncodingUtil.urlEncode(Accesscode, 'UTF-8');
                reqbody += '&redirect_uri='+ EncodingUtil.urlEncode(RESOURCE_REDIRECT_URI, 'UTF-8');
                reqbody += '&scope=refresh_token full';

         String endpoint = loginUri + '/services/oauth2/token?' + reqbody;   

         Map<String, object> mapReqBody = getHTTPResponse(endpoint, header, 'POST');

        header = 'access_token:'+string.valueOf(mapReqBody.get('access_token')) + ' ' + 'refresh_token:'+string.valueOf(mapReqBody.get('refresh_token'));        
        return header;        
    }

    public static Map<String, object> getHTTPResponse(String endpoint, String header, String method){
        HttpRequest req = new HttpRequest();  
        req.setMethod(method);
        req.setEndpoint(endpoint);        

        if(header != ''){
            req.setHeader('Content-Type', 'application/json');
            req.setHeader('Authorization', header);
        }
        Http httpReq = new Http();  
        HTTPResponse res = httpReq.send(req);

        return (Map<String, object>)JSON.deserializeUntyped(res.getBody());
    }
https://test.salesforce.com/services/oauth2/token?
grant_type=authorization_code&
client_id=xxxx&
client_secret=xxxx&
code=aPrxvJxdfdfsdcv_QO04.ORKytXPF4gEknyrN6HU5N24pQAfFXcxSTKuMz3uTziP_8Cug%3D%3D&
redirect_uri=https://xxxx.lightning.force.com/lightning/n/Connected_Orgs?0.source=alohaHeader&
scope=refresh_token full

**Error:*

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

Actual Response body:

response: System.HttpResponse[Status=Unknown Version, StatusCode=400] 
response Body: <h1>Bad Message 400</h1><pre>reason: Unknown Version</pre> 
response status: Unknown Version
  • 3
    Likely it means that the endpoint is returning to you HTML rather than JSON. You should try to debug the actual response body and wrap your deserialization in a try catch. Given the level of information in your post, it will be nearly impossible to help more than that until you add that information. – Adrian Larson Aug 20 at 21:43
  • added what I am getting in the response, Response, body, and status. – Ajay Aug 20 at 22:13
  • it worked after removing this "reqbody += '&scope=refresh_token full'; – Ajay Aug 21 at 21:13
0

it worked after removing this "reqbody += '&scope=refresh_token full';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.