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
Error: Refresh token has expired Javascript SDK
Tags: sdk
Jun 24, 2021 at 9:56am   •   1 replies  •  0 likes
Nitesh Giri

I have used OAuth 2.0 flow to get access token and refresh token. I would be taking care of managing refresh token by saving it into some persistent storage.

I am calling an endpoint to get all the extensions for account.

const { SDK } = require('@ringcentral/sdk');

const rcsdk = new SDK({
server: 'https://platform.devtest.ringcentral.com/',
clientId: '',
clientSecret: '',

const platform = rcsdk.platform();
const data = await platform.auth().data();
data.token_type = 'bearer';
data.expire_time = 3600;
data.access_token = <access_token>;
data.refresh_token = <refresh_token>;
data.refresh_token_expires_time = 60480
platform.auth().setData(data);
const accounts = await platform.get('/restapi/v1.0/account/accountId/extension');

I get the following error:

Error: Refresh token has expired

If same access token is used in Postman for following endpoint works:

curl --location --request GET 'https://platform.devtest.ringcentral.com/restapi/v1.0/account/accountId/extension' \
--header 'accept: application/json' \
--header 'authorization: Bearer <access_token>' \
--header 'Content-Type: application/json' 


Also, how can I get the refreshed refresh_token if I use platform.refresh() ?

1 Answer
answered on Jun 25, 2021 at 1:36pm  

Where do you login and why do you need to set the tokens?

const data = await platform.auth().data();
data.token_type = 'bearer';
data.expire_time = 3600;
data.access_token = <access_token>;
data.refresh_token = <refresh_token>;
data.refresh_token_expires_time = 60480
platform.auth().setData(data);

You don't need to call the refresh() method. After login, if the platform instance is persisted, then call

if (await platform.loggedIn()){
  // call platform API
  const accounts = await platform.get('/restapi/v1.0/account/accountId/extension');
}else{
  console.log("Auto-login failed: BOTH TOKEN TOKENS EXPIRED")
  console.log("CAN'T REFRESH: " + e.message)
  // ask user to relogin
}

If the platform is destroyed and you want to save the tokens and reuse it after creating a new platform instance.

var tokenObj = await platform.auth().data()
var tokenStr = JSON.stringgify(tokenObj)
// stringify and safe the token string in a safe place

// then read the token and reuse it
var savedTokenObj = JSON.parse(tokenStr)
platform.auth().setData(tokenObj)
if (await this.platform.loggedIn()){
  // call platform API
  const accounts = await platform.get('/restapi/v1.0/account/accountId/extension');
}else{
  console.log("Auto-login failed: BOTH TOKEN TOKENS EXPIRED")
  console.log("CAN'T REFRESH: " + e.message)
  // ask user to relogin
}

You can implement callback functions to get notified if login, refresh succeeds or fails

platform.on(platform.events.loginSuccess, loginSuccess)
platform.on(platform.events.logoutSuccess, logoutSuccess)
platform.on(platform.events.refreshSuccess, refreshSuccess)
platform.on(platform.events.refreshError, refreshError)

Remember that after refresh successfully, you have to get the new tokens and save it for reuse


 1
on Jun 26, 2021 at 8:54am   •  0 likes

Thanks @Phong Vu for your reply.

As far as I know I would need username & password to login to platform. I don't want to login to platform by using credentials. Basically, my client app(3rd party application) would be accessing the Ringcentral user's data authorized through OAuth flow. Is there any way to use SDK without using username & passoword?

Summarising my steps here:

  1. Client application redirects user to Ringcentral page to authorize itself with certain scopes.
  2. User logins in and authorizes application.
  3. After authorization, client app gets access token & refresh token.
  4. Client application makes use of these access tokens to access user's resources(it may refetch access token by refresh token)

I want to implement this client application(nodejs) to use SDK to fetch user's data.

I can use this access token to use APIs to fetch user's data. Can't I use SDK to access these APIs just by access_tokens ?

on Jun 26, 2021 at 9:36am   •  0 likes

Then check out this quick start to learn about 3-legged authentication. Once you get the tokens, implement the same way to handle refresh token as shown above.


on Jun 30, 2021 at 10:20am   •  0 likes

@Phong Vu Thanks for the help. Can you please also look at following:

https://community.ringcentral.com/questions/100336/javascript-sdk-returning-empty-response.html



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