question

Super Admin avatar image
Super Admin asked ·

Java SDK Subscription Refresh Causing 401 Error

I'm able to successfully create a new subscription using the Java SDK, but when the subscription attempts to refresh itself it's failing with a 401 error. Either there is a bug with the Java SDK or I'm missing something.

When the subscription is created, it starts a timer to regularly refresh itself:

https://github.com/ringcentral/ringcentral-java/blob/master/src/main/java/com/ringcentral/Subscription.java#L89


Bug when this code runs:

https://github.com/ringcentral/ringcentral-java/blob/master/src/main/java/com/ringcentral/Subscription.java#L110


This error results:

com.ringcentral.RestException: HTTP status code: 401


{

"errorCode" : "TokenInvalid",

"message" : "Token not found",

"errors" : [ {

"errorCode" : "OAU-213",

"message" : "Token not found"

} ]

}

at com.ringcentral.RestClient.request(RestClient.java:152)

at com.ringcentral.HttpClient.put(HttpClient.java:43)

at com.ringcentral.HttpClient.put(HttpClient.java:77)

at com.ringcentral.Subscription.refresh(Subscription.java:115)

at com.ringcentral.Subscription$2.run(Subscription.java:94)

at java.util.TimerThread.mainLoop(Timer.java:555)

at java.util.TimerThread.run(Timer.java:505)


Am I doing something wrong or is this an issue with the SDK?


subscriptionssdkjava
1 |1000 characters needed characters left characters exceeded

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

1 Answer

· Write an Answer
Phong Vu avatar image
Phong Vu answered ·

Hi Dan,

Yes, the subscription object renews (refresh) the PubNub notifications automatically. It uses the RestClient object, which uses the access token to renew the subscription periodically.

The RestClient is also, by default, auto-refresh, which uses the refresh token to request for a new access token if the access token expires.

In your case, it could be either, the refresh token is also expired if you ran your app for more than a week, which may not be likely the situation. Or, somehow, the access token was revoked.

I ran my test code for a few hours and the notification works fine.

Can you double check, make a simple test and let me know.

4 comments Share
1 |1000 characters needed characters left characters exceeded

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

I'm wondering if it has something to do with how I'm instantiating RestClient(s). I'm currently creating a new RestClient and authorizing whenever my service handles a request. Should I be creating a singleton instead? Is it thread safe?

0 Likes 0 · ·

I think that could be the cause. You can disable the auto-refresh from the RestClient object, get the tokens data from the RestClient after your first login, save it in your database and use it to set for a new RestClient object whenever your service handles a request. You must implement a timer to check the expiration time then use the refresh token to request for a new access token before it expires. That way, you don't need to authorize each time you call an API. And you can share the tokens between apps, threads, with each of them can have own instance of the RestClient object.

0 Likes 0 · ·

I'd prefer to use a singleton and let the RestClient auto-refresh itself if possible, is the RestClient thread safe?

0 Likes 0 · ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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