News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
Migrating Password Auth to JWT Auth Flow
Tags: authentication
Sep 13, 2023 at 8:14am   •   1 replies  •  0 likes
system-operator

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!

1 Answer
answered on Sep 13, 2023 at 9:04am  

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



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us