Can anyone will provide me example of how to get accesstoken in apex code which can be used for making rest callout?

Thanks in advance.

1 Answer 1


Create a connected app. Refer this link.


Copy client id and client secret from connected app you created.

There are several types of authentication. Here I used username password oauth flow.

Http h = new Http();
HttpRequest request = new HttpRequest();
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
HttpResponse response = h.send(request);
System.debug('response '+response.getBody());

Replace "login" with "test" if you are trying in sandbox. Response will look like:


Get access token by parsing this json response.

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.