question

IWD avatar image
IWD asked IWD edited

SMS & PHP - Working Sandbox app code not working with graduated app

Hello All,

New to RingCentral development, but I created an app with an SMS endpoint (send SMS only) and built some code to get it working in the sandbox. Code works perfectly using the Sandbox credentials/client/server info. The app was then graduated, so I updated the following to the graduated app's credentials/info:

$client_id = 'production_appclientid'; //Removed for security
$client_secret = 'production_appclientsecret'; //Removed for security
$server_url = 'https://platform.ringcentral.com';
$rc_jwt = 'production_jwt_key'; //Removed for security


Having changed only the above 4 lines of code, the app is now failing at the highlighted line below:

function send_sms($fromNumber){
  global $platform, $RECIPIENT;
  try {
    $requestBody = array(
       'from' => array ('phoneNumber' => $fromNumber),
       'to' => array( array('phoneNumber' => $RECIPIENT) 
       'text' => 'test'
    );
    $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);
  }
}

I still have the Sandbox Environment active, do I need to suspend that? The Production Environment Status is "Active", and I'm able to see the requests I'm submitting are generating errors via the analytics panel.


When I view the Production End Points analytics, I see the errors are all to the endpoint "1/account/*/extension/*/sms". Every request to that endpoint in the Production Environment results in an error 403 or 400. Do I need to enable another endpoint for my app?


Any help/suggestions appreciated.

rest apisending sms
11 comments
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 ·

Did you also call this function to detect the user's phone numbers and if the "from" phone number is SMS enabled?

/*
  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!");
  }
}
0 Likes 0 ·
IWD avatar image IWD Phong Vu ♦♦ commented ·

Thank you for your reply. I forgot to mention that in my code I wasn't calling this function, instead calling send_sms directly and specifying the $fromNumber programmatically, such as:

send_sms('18005551212');


I've updated my code to call this function instead of send_sms, then call send_sms from inside this function exactly as you've shown:

return send_sms($record->phoneNumber);


The code now fails at the highlighted line:

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"){
            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!");
    }
}

Here is the truncated error message. It appears to be a permissions issue?

Expected HTTP Error: Client error: `GET https://platform.ringcentral.com/restapi/v1.0/account/~/extension/~/phone-number` resulted in a `403 Forbidden` response: { "errorCode" : "InsufficientPermissions", "message" : "In order to call this API endpoint, application needs to hav (truncated...)

0 Likes 0 ·
Phong Vu avatar image Phong Vu ♦♦ IWD commented ·

Add the "Read Accounts" app scope to the app and try again. This is documented in the developer guide.

0 Likes 0 ·
Show more comments

1 Answer

Phong Vu avatar image
Phong Vu answered

Your user extension does not need to have a license and phone. But it does need a phone number. You can purchase a phone number and assign to the user as a direct number then that user can send text message from his own number.

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.

IWD avatar image IWD commented ·

Thank you. I appreciate all your help.

0 Likes 0 ·

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