title: Apex Rest callout for Access token
body: <p>I want to generate a simple post call from Apex for the access token. I am new to Apex and Rest so is there someone explain me how to do that? </p>
  
  <p>I have endpointurl, client _secret, client_id, scope, and grant_type is Client_credentials'. 
  I tried to run this in Postman and it works fine but not in salesforce.</p>
  
  <pre><code>Http http = new Http();
  HttpRequest request = new HttpRequest();
  
  request.setEndpoint('https://xxx.xxx.eu/o/oauth2/token');
  
  string mybody='{"client_id":"xxxx","client_secret":"xxxx","grant_type":"client_credentials","scope": "xxx.Rest.everything"}';
  
  request.setMethod('POST');
  request.setHeader('Content-Type', 'application/json;charset=UTF-8');
  // Set the body as a JSON object
  request.setBody(mybody);
  
  HttpResponse response = http.send(request);
  
  // Parse the JSON response
  if (response.getStatusCode() != 201) {
      System.debug('The status code returned was not expected: ' +
          response.getStatusCode() + ' ' + response.getStatus());
  } else {
      System.debug(response.getBody());
  }
  
  ´´´´
  </code></pre>