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
Does the RingCentral javascript SDK automatically refresh tokens?
Tags: sdk, oauth
Nov 10, 2021 at 11:51am   •   2 replies  •  0 likes
The Posse Foundation

We have a basic 3 legged OAuth web app that allows us to edit extensions, but we keep running into invalid refresh token & token not found errors, which have no explanation or documentation tied to them. I read from some other post that the SDK manages refreshing the tokens automatically, but I haven't seen any official documentation stating so. If the SDK doesn't automatically refresh, what is the best way to refresh them?

2 Answers
answered on Nov 10, 2021 at 8:37pm  

Yes, you can implement a loop to keep the access token valid, but you can use the refresh token to get a new access token if the access token expired. Thus, you don't really need to run an interval every 45 minutes. Calling this function will trigger the SDK to refresh the access token if it expired.

platform = rcsdk.platform()

if (await platform.loggedIn()){
 // call API   
}else{
  console.log("BOTH TOKEN TOKENS EXPIRED
}

For auto refresh, you should detect if there is no API call within 7 days (refresh token is valid for 7 days), call the refresh() method to get new tokens (both access and refresh token)


 2
on Nov 11, 2021 at 7:57am   •  0 likes

That makes sense and seems like a better solution, thank you for the informative answer!

answered on Nov 10, 2021 at 1:54pm  

I don't know if this will help anyone else, but I made a basic solution for refreshing the tokens since they don't seem to refresh automatically. Using the @ringcentral/sdk package for Node.js.

const platform = client.platform()

setInterval(async () => {
    try {
        if(!platform.auth().refreshTokenValid()) {
            throw 'Refresh token not valid!'
        }

        const res = await platform.refresh()
        const data = await res.json()

        await platform.auth().setData(data)

        const date = new Date()

        console.log(`(${date.toLocaleString()}) Successfully generated new tokens.`)

        return Promise.resolve()
    } catch (err) {
        console.trace(`Failed to refresh token: ${err}`)
        return Promise.reject(err)
    }
},1000 * 60 * 45) // Refresh every 45 minutes (Tokens expire every 60 minutes)

 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