Error requesting access token API-REST

Good Afternoon, Requesting your support with the following topic:
We create an integration with SugaCRM through an API-REST.
The detail is that we are trying to obtain an authentication token from SugarCRM with the endpoint /rest/v11/oauth2/token using the authorization protocol: OAuth2

However we get the following response:
Error requesting access token
403 Forbidden
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
</body></html>

We have proceeded with the following points
1. Token request from Postman which is correct.
2. Access credentials have been verified and are correct.
3. The connection user is “Active”.
Is there something we need to consider?
Thanks for your help.

Parents
  • I don't know if this helps but, I got this from someone on this group several years ago and I keep it on my web server to test custom API calls from PHP. I'm a lazy developer ;)

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 'On');
    
    //change to your environment
    $rest = "https://sugarcrm.devel.mydomain.com/sugarcrm/rest/v11"; 
    
    $user = "<the username here>"; // userid of the webservice user
    $pass = "<the password here>"; // and the password of user “user”
    
    //what you want to test:
    
    $parameters = 
    array('email_addresses'=> array(
           'francesca.shiekh@mydomain.com',
           'francescas@mydomain.com'
        ));
    $api_to_test = '/doSomethingWithTheseEmails/';
    
    // get the token and execute
    
    $url = $rest . "/oauth2/token"; // login
    $oauth2_token_parameters = array(
      "grant_type" => "password",
      "client_id" => "sugar",
      "client_secret" => "",
      "username" => $user,
      "password" => $pass,
      "platform" => "base"
    );
    echo('<pre>');
    echo($url.'<p>');
    $oauth2_token_result = call($url, '', 'POST', $oauth2_token_parameters);
    echo(print_r($oauth2_token_result, true));
    if (empty($oauth2_token_result->error)){
      //logged in ok
    echo('logged in ok <p>');
      $url = $rest . $api_to_test;
    echo('<pre>');
    echo($url.'<p>');
    
    //
    //choose here if you are doing a POST or a GET
    //
      $result = call($url, $oauth2_token_result->access_token, 'POST', $parameters); //example for POST 
      //$result = call($url, $oauth2_token_result->access_token, 'GET');
    
    echo('<p>');
    var_dump($result);
      $url = $rest . "/oauth2/logout"; // logout
      $oauth2_logout_result = call($url, $oauth2_token_result->access_token, 'GET');
    }else{
      print("<hr>Login Failed<hr>");
    }
    /*******************************************************************************
     * call
     ******************************************************************************/
    /**
    * Generic function to make cURL request.
    * @param $url - The URL route to use.
    * @param string $oauthtoken - The oauth token.
    * @param string $type - GET, POST, PUT. Defaults to GET.
    * @param array $parameters - Endpoint parameters.
    * @return mixed
    */
    function call($url, $oauthtoken='', $type='GET', $parameters=array())
    {
       $type = strtoupper($type);
       $curl_request = curl_init($url);
       if ($type == 'POST')
       {
          curl_setopt($curl_request, CURLOPT_POST, 1);
       }
       elseif ($type == 'PUT')
       {
          curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT");
       }
       elseif ($type == 'DELETE')
       {
          curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE");
       }
       curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
       curl_setopt($curl_request, CURLOPT_HEADER, false);
       curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
       curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
       if (!empty($oauthtoken))
       {
          curl_setopt($curl_request, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "oauth-token: {$oauthtoken}"));
       }else
       {
          curl_setopt($curl_request, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
       }
       if (!empty($parameters))
       {
          //encode the parameters as JSON
          $jsonEncodedData = json_encode($parameters);
          echo('<p> json parameters <p>:');
          echo($jsonEncodedData);
          curl_setopt($curl_request, CURLOPT_POSTFIELDS, $jsonEncodedData);
       }
       $result = curl_exec($curl_request);
       curl_close($curl_request);
       //decode the response from JSON
       $response = json_decode($result);
       return $response;
    }
    ?>
    

    Hope it helps you too!

Reply
  • I don't know if this helps but, I got this from someone on this group several years ago and I keep it on my web server to test custom API calls from PHP. I'm a lazy developer ;)

    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 'On');
    
    //change to your environment
    $rest = "https://sugarcrm.devel.mydomain.com/sugarcrm/rest/v11"; 
    
    $user = "<the username here>"; // userid of the webservice user
    $pass = "<the password here>"; // and the password of user “user”
    
    //what you want to test:
    
    $parameters = 
    array('email_addresses'=> array(
           'francesca.shiekh@mydomain.com',
           'francescas@mydomain.com'
        ));
    $api_to_test = '/doSomethingWithTheseEmails/';
    
    // get the token and execute
    
    $url = $rest . "/oauth2/token"; // login
    $oauth2_token_parameters = array(
      "grant_type" => "password",
      "client_id" => "sugar",
      "client_secret" => "",
      "username" => $user,
      "password" => $pass,
      "platform" => "base"
    );
    echo('<pre>');
    echo($url.'<p>');
    $oauth2_token_result = call($url, '', 'POST', $oauth2_token_parameters);
    echo(print_r($oauth2_token_result, true));
    if (empty($oauth2_token_result->error)){
      //logged in ok
    echo('logged in ok <p>');
      $url = $rest . $api_to_test;
    echo('<pre>');
    echo($url.'<p>');
    
    //
    //choose here if you are doing a POST or a GET
    //
      $result = call($url, $oauth2_token_result->access_token, 'POST', $parameters); //example for POST 
      //$result = call($url, $oauth2_token_result->access_token, 'GET');
    
    echo('<p>');
    var_dump($result);
      $url = $rest . "/oauth2/logout"; // logout
      $oauth2_logout_result = call($url, $oauth2_token_result->access_token, 'GET');
    }else{
      print("<hr>Login Failed<hr>");
    }
    /*******************************************************************************
     * call
     ******************************************************************************/
    /**
    * Generic function to make cURL request.
    * @param $url - The URL route to use.
    * @param string $oauthtoken - The oauth token.
    * @param string $type - GET, POST, PUT. Defaults to GET.
    * @param array $parameters - Endpoint parameters.
    * @return mixed
    */
    function call($url, $oauthtoken='', $type='GET', $parameters=array())
    {
       $type = strtoupper($type);
       $curl_request = curl_init($url);
       if ($type == 'POST')
       {
          curl_setopt($curl_request, CURLOPT_POST, 1);
       }
       elseif ($type == 'PUT')
       {
          curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT");
       }
       elseif ($type == 'DELETE')
       {
          curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE");
       }
       curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
       curl_setopt($curl_request, CURLOPT_HEADER, false);
       curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
       curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
       if (!empty($oauthtoken))
       {
          curl_setopt($curl_request, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "oauth-token: {$oauthtoken}"));
       }else
       {
          curl_setopt($curl_request, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
       }
       if (!empty($parameters))
       {
          //encode the parameters as JSON
          $jsonEncodedData = json_encode($parameters);
          echo('<p> json parameters <p>:');
          echo($jsonEncodedData);
          curl_setopt($curl_request, CURLOPT_POSTFIELDS, $jsonEncodedData);
       }
       $result = curl_exec($curl_request);
       curl_close($curl_request);
       //decode the response from JSON
       $response = json_decode($result);
       return $response;
    }
    ?>
    

    Hope it helps you too!

Children
No Data