question

sky mikesell avatar image
sky mikesell asked Phong Vu answered

I set my all ring central credential with proper settings. But API throw error."Unable to authenticate to platform. Check credentials." I atteched my code and errors . Please check. We are in Production But cannot do single SMS through Production

I am facing error

==================================

Exception: Response has unsuccessful status in

/var/www/html/api/public_html/vendor/ringcentral/ringcentral-php/src/Http/Client.php:44

Stack trace:

#0 /var/www/html/api/public_html/vendor/ringcentral/ringcentral-php/src/Platform/Platform.php(310):

RingCentral\SDK\Http\Client->send(Object(GuzzleHttp\Psr7\Request))

#1 /var/www/html/api/public_html/vendor/ringcentral/ringcentral-php/src/Platform/Platform.php(434):

RingCentral\SDK\Platform\Platform->sendRequest(Object(GuzzleHttp\Psr7\Request), Array)

#2 /var/www/html/api/public_html/vendor/ringcentral/ringcentral-php/src/Platform/Platform.php(212):

RingCentral\SDK\Platform\Platform->requestToken('/restapi/oauth/...', Array)

#3 /var/www/html/api/public_html/sendSMS.php(38): RingCentral\SDK\Platform\Platform->login(Array)

#4 {main}


Next RingCentral\SDK\Http\ApiException: Unauthorized for this grant type in

/var/www/html/api/public_html/vendor/ringcentral/ringcentral-php/src/Http/Client.php:52

Stack trace:

#0 /var/www/html/api/public_html/vendor/ringcentral/ringcentral-php/src/Platform/Platform.php(310):

RingCentral\SDK\Http\Client->send(Object(GuzzleHttp\Psr7\Request))

#1 /var/www/html/api/public_html/vendor/ringcentral/ringcentral-php/src/Platform/Platform.php(434):

RingCentral\SDK\Platform\Platform->sendRequest(Object(GuzzleHttp\Psr7\Request), Array)

#2 /var/www/html/api/public_html/vendor/ringcentral/ringcentral-php/src/Platform/Platform.php(212):

RingCentral\SDK\Platform\Platform->requestToken('/restapi/oauth/...', Array)

#3 /var/www/html/api/public_html/sendSMS.php(38): RingCentral\SDK\Platform\Platform->login(Array)

#4 {main}Unable to authenticate to platform. Check credentials.



==========

My Code

<?php
//Header config
require __DIR__ . '/header.php';
/**Db connect */
require __DIR__ . '/dbConnect.php';
/**Ringcentral vendor config */
require __DIR__ . '/vendor/autoload.php';


//fetch all keys from database
//$query = 'SELECT * FROM public."Settings"';
//$result = pg_query($db, $query);
//while ($row = pg_fetch_assoc($result)) {
  
  
  $clientId = 'Xis4z9ojiqff8kozXxEFcQ';
  $clientSecret = '48TxqwJG...';
  $jwtToken = 'eyJraWQiOiI4...';
  $fromNumber = '+1704626XXX';
//}


$serverUrl = 'https://platform.ringcentral.com';


//$jsonValue = $_GET['finalJson'];
//$jsonValue = json_decode($jsonValue);


//receipient number
$receipientNo = '+1480409XXX';


// sms content
$message = 'Hello Sky';


# Instantiate the SDK and get the platform instance
$rcsdk = new RingCentral\SDK\SDK( $clientId,
$clientSecret,
$serverUrl );
$platform = $rcsdk->platform();


// Authenticate a user using a personal JWT token
try {
  
  
  $platform->login( [ "jwt" => $jwtToken ] );
  send_sms($fromNumber);
} catch (\RingCentral\SDK\Http\ApiException $e) {
  
  
  echo $e;
  exit("Unable to authenticate to platform. Check credentials. ");
}


/*
Read phone number(s) that belongs to the authenticated user and detect if a phone number
has the SMS capability
*/
function read_extension_phone_number_detect_sms_feature(){
  
  
  global $platform;
  $endpoint = "/restapi/v1.0/account/~/extension/~/phone-number";
  $resp = $platform->get($endpoint);
  $jsonObj = $resp->json();
  foreach ($resp->json()->records as $record){
  
  
    foreach ($record->features as $feature){
  
  
      if ($feature == "SmsSender"){
  
  
        // If a user has multiple phone numbers, check and decide which number
        // to be used for sending SMS message.
        return send_sms($record->phoneNumber);
      }
    }
  }
  if (count($jsonObj->records) == 0){
  
  
    exit("This user does not own a phone number!");
  }else{
  
  
    exit("None of this user's phone number(s) has the SMS capability!");
  }
}

/*
Send a text message from a user own phone number to a recipient number
*/
function send_sms($FROM_NUMBER){
  
  
  global $platform, $receipientNo, $message;
  try {
  
  
    $requestBody = array(
      'from' => array ('phoneNumber' => $FROM_NUMBER),
      'to' => array( array('phoneNumber' => $receipientNo) ),
      // To send group messaging, add more (max 10 recipients) 'phoneNumber' object. E.g.
    /*
    'to' => array(
    array('phoneNumber' => $receipientNo),
      array('phoneNumber' => 'Recipient-Phone-Number')
    ),
    */
    'text' => $message
    );
    $endpoint = "/account/~/extension/~/sms";
    $resp = $platform->post($endpoint, $requestBody);
    $jsonObj = $resp->json();
    print("SMS sent. Message id: " . $jsonObj->id . PHP_EOL);
    check_message_status($jsonObj->id);
  } catch (\RingCentral\SDK\Http\ApiException $e) {
  
  
    exit("Error message: " . $e->message . PHP_EOL);
  }
}

/*
Check the sending message status until it's out of the queued status
*/
function check_message_status($messageId){
  
  
  global $platform;
  try {
  
  
    $endpoint = "/restapi/v1.0/account/~/extension/~/message-store/".$messageId;
    $resp = $platform->get($endpoint);
    $jsonObj = $resp->json();
    print("Message status: " . $jsonObj->messageStatus . PHP_EOL);
    if ($jsonObj->messageStatus == "Queued"){
  
  
      sleep(2);
      check_message_status($jsonObj->id);
    }
  } catch (\RingCentral\SDK\Http\ApiException $e) {
  
  
    exit("Error message: " . $e->message . PHP_EOL);
  }
}
?>


===========

oauth
1 comment
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image Phong Vu ♦♦ commented ·

Seriously? You posted the entire app client id, app client secret and JWT token on your company production account on a public forum?

I hope that other people did not see or copy them before I deleted them for you.

0 Likes 0 ·

1 Answer

Phong Vu avatar image
Phong Vu answered

Make sure that you upgrade the latest RingCentral PHP SDK which support JWT token. I doubt that you are still using the old SDK and that is way your error message showed authentication failed.

You app also does not have the Read Accounts app scope which is needed for reading the extension phone number. I added the missing scope to the app and use your credentials to send a test SMS message using the code successfully.

You should receive a text message to your direct number with the following message:

"RC Dev community: Test sent from your main company number to your direct number"

Just run your code again.

Please take my comment seriously as posting your account sensitive info on public site could lead to security issue with your production account.

1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys