question

system-operator8834 avatar image
system-operator8834 asked Glenn B. answered

Migrating Password Auth to JWT Auth Flow

We'll apologize in advance for the length but wanted to go into detail as we thought that our issue in migrating from password auth to JWT auth might help others who are going to be doing the same thing in the coming months.


The code below is used in a small VBA function that sends SMS text messages to customers reminding them of appointments. SMS text messages are sent from the RC phone number of the employee assigned to the customer receiving the SMS text. It's been working without issue/error for a long time now.

(EXISTING APP - PRODUCTION ENV - PASSWORD AUTH CODE >>> START)
Public Const RingCentralSMSURI = "https://platform.ringcentral.com/restapi/v1.0/account/~/extension/~/sms "
Public Const RingCentralTokenURI = "https://platform.ringcentral.com/restapi/oauth/token "
Public Const RingCentralGetSMSURI = "https://platform.ringcentral.com/restapi/v1.0/account/~/extension/~/message-store "
Public Const AppKey = "PRODUCTION APP KEY"
Public Const AppSecret = "PRODUCTION APP SECRET"

Public Function SendSMS(strRCUserTel As String, strRCUserExt As String, strRCUserPwd As String, strToPhone As String, strMsg As String) As Boolean

Dim strAccessToken As String
Dim strRequestBody As String
Dim strSMSStatus As String
Dim http As New MSXML2.XMLHTTP60

'Set the request body
strRequestBody = "grant_type=password" & "&username=" & strRCUserTel & "&password=" & strRCUserPwd & "&extension=" & strRCUserExt

'Set the HTTP request properties
http.Open "POST", RingCentralTokenURI, False
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.setRequestHeader "Authorization", "Basic " & Base64Encode(AppKey & ":" & AppSecret)

'Send the HTTP request
http.Send strRequestBody

'Parse the JSON response and extract the access token
strAccessToken = ParseJson(http.responseText)("access_token")

'Send the SMS message
http.Open "POST", RingCentralSMSURI, False
http.setRequestHeader "Authorization", "Bearer " & strAccessToken
http.setRequestHeader "Accept", "application/json"
http.setRequestHeader "Content-Length", "323"
http.setRequestHeader "Content-Type", "application/json"
http.Send "{" & """to"": [{""phoneNumber"": """ & strToPhone & """}]," & """from"": {""phoneNumber"": """ & strRCUserTel & """}," & """text"": """ & strMsg & """" & "}"

End Function
(EXISTING APP - PRODUCTION ENV - PASSWORD AUTH CODE >>> END)

The New App Sandbox Environment code below works fine and our new app was graduated.

(NEW APP - SANDBOX ENVIRONMENT - JWT AUTH CODE >>> START)
Public Const RingCentralSMSURI = "https://platform.devtest.ringcentral.com/restapi/v1.0/account/~/extension/~/sms
Public Const RingCentralTokenURI = "https://platform.devtest.ringcentral.com/restapi/oauth/token
Public Const RingCentralGetSMSURI = "https://platform.devtest.ringcentral.com/restapi/v1.0/account/~/extension/~/message-store "

Public Const AppKey = "NEW APP SANDBOX APP KEY"
Public Const AppSecret = "NEW APP SANDBOX APP SECRET"
Public Const JWToken = "SANDBOX JWT"
Public Function SendSMS() As String

Dim strRCUserTel As String
Dim strToPhone As String
Dim strMsg As String

'Set the RC telephone number/user, password, extension
strRCUserTel = "SANDBOX MAIN COMPANY NUMBER"
strToPhone = "RECIPIENT PHONE NUMBER"
strMsg = "This is a test using JWT auth."

Dim strAccessToken As String
Dim strRequestBody As String
Dim strSMSStatus As String
Dim http As New MSXML2.XMLHTTP60

'Set the request body
strRequestBody = "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=" & JWToken

'Set the HTTP request properties
http.Open "POST", RingCentralTokenURI, False
http.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
http.setRequestHeader "Authorization", "Basic " & Base64Encode(AppKey & ":" & AppSecret)

'Send the HTTP request
http.send strRequestBody

'Parse the JSON response and extract the access token
strAccessToken = ParseJson(http.responseText)("access_token")

'Send the SMS message
http.Open "POST", RingCentralSMSURI, False
http.setRequestHeader "Authorization", "Bearer " & strAccessToken
http.setRequestHeader "Accept", "application/json"
http.setRequestHeader "Content-Length", "323"
http.setRequestHeader "Content-Type", "application/json"
http.send "{" & """to"": [{""phoneNumber"": """ & strToPhone & """}]," & """from"": {""phoneNumber"": """ & strRCUserTel & """}," & """text"": """ & strMsg & """" & "}"

End Function
(NEW APP - SANDBOX ENVIRONMENT - JWT AUTH CODE >>> END)

The problem we are having is moving the New App Sandbox Environment code over to Production and trying to specify the RC telephone number that we want to send the SMS FROM. Modifying the New App Sandbox Environment code above - we changed the following bold items:

Public Const RingCentralSMSURI = "https://platform.ringcentral.com/restapi/v1.0/account/~/extension/~/sms "
Public Const RingCentralTokenURI = "https://platform.ringcentral.com/restapi/oauth/token "
Public Const RingCentralGetSMSURI = "https://platform.ringcentral.com/restapi/v1.0/account/~/extension/~/message-store "
Public Const AppKey = "NEW APP PRODUCTION APP KEY"
Public Const AppSecret = "NEW APP PRODUCTION APP SECRET"
Public Const JWToken = "PRODUCTION JWT"
'Set the RC telephone number/user, password, extension
strRCUserTel = "USERS RC PHONE"

When we specify one of our users RC phone number as strRCUserTel - we get the following error:

{
  "errorCode" : "FeatureNotAvailable",
  "message" : "Phone number doesn't belong to extension",
  "errors" : [ {
    "errorCode" : "MSG-304",
    "message" : "Phone number doesn't belong to extension"
  } ]
}

Clearly, we are not passing the users RC phone number properly (or incompletely). Any suggestions on how to proceed would be much appreciated - thank you!

authentication
1 |3000

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

1 Answer

Glenn B. avatar image
Glenn B. answered

The answer to this issue was provided by Phong Vu and can be read in this thread:
https://community.ringcentral.com/questions/117044/password-to-jwt-migration.html


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