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() ?
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
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:
- Client application redirects user to Ringcentral page to authorize itself with certain scopes.
- User logins in and authorizes application.
- After authorization, client app gets access token & refresh token.
- 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 ?
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.
@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