Refresh Token has Expired in Python

  • 1
  • Problem
  • Updated 2 months ago
Attempting to send SMS with python SDK I keep getting "Refresh Token has Expired "
How do I set this to never expire in Python, is it even possible? The documentation does not say.


Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb

Posted 2 months ago

  • 1
Photo of Phong Vu

Phong Vu, Devangelist

  • 3,282 Points 3k badge 2x thumb
After successfully authenticated/logged in, your refresh token with expire in 7 days. Every time your access token expires, you can use the refresh token to request for a new access token. And every time you request for a new access token, the new refresh token expiration time will be set to 7 days again.

If the refresh token also expired, you must direct the user to the login page to login again.

Can you double check if the refresh token really expires or it's because of some bug in the Python SDK?

In this case, if the refresh token really expired after 7 days, you may need to implement a timer to check the expiration periodically and send a request for new access token when it nearly expires. That way you can always have a valid refresh token.

+ phong
Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
Phong
Thanks for your assistance. How do I check the refresh token?
Photo of Phong Vu

Phong Vu, Devangelist

  • 3,282 Points 3k badge 2x thumb
Hi Rick,

I don't use the Python SDK yet so I don't have exact code to show you. But I believe that after logged in, you can get the tokens info from the platform.auth().data(). When you need to refresh, just call platform.refresh() method.

Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
Phong thanks again! Umm that does not work either, it throws same exception. Is any of this written down, anywhere? I'm trying the following:
 
   platform.auth().refresh_token()
 and or
platform.auth().data()
Does RC even work with python?



Photo of Tyler Long

Tyler Long, Official Rep

  • 8,502 Points 5k badge 2x thumb
Hi Rick,

There are several users reported "Refresh Token has Expired" and they are not using Python at all. So I wonder this might be an issue from RC server side.

Could you please do me a favor? Try my Python SDK: https://github.com/tylerlong/ringcentral-python

By the way, there is a way to auto refresh the token: https://github.com/tylerlong/ringcentral-python#authorization-refresh

If you still have the issue, please post the code to reproduce the issue. By then we can confirm it is a RC server side issue or not.

(Edited)
Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
Ok some progress
this
r = rc.post('https://platform.ringcentral.com/restapi/v1.0/account/1NNNNN/104/NNNNNNN/sms-log?dateFrom=2...', {
throws
Exception: HTTP status code: 404

  "errorCode" : "AGW-404",
  "message" : "Resource not found",

Thanks





Photo of Tyler Long

Tyler Long, Official Rep

  • 8,422 Points 5k badge 2x thumb
The error message simply means that the url you try to access doesn't exist.

How did you get this url:
https://platform.ringcentral.com/restapi/v1.0/account/1NNNNN/104/NNNNNNN/sms-log  ?

I don't remember that there is any endpoint ends with sms-log.

Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
Yes, I shortened it to this https://platform.ringcentral.com/restapi/v1.0/ as well as several other variations and no go? Any idea what its supposed to be? I got the above from ringcentrals own documentation?!? So either i'm reading it wrong, or they have another error. All this thing has to do is send an SMS.

Photo of John Wang

John Wang, Official Rep

  • 5,654 Points 5k badge 2x thumb
There is no "sms-log" endpoint in our API Reference or Swagger spec:

* Reference: https://developer.ringcentral.com/api-reference
* Spec: https://netstorage.ringcentral.com/dpw/api-reference/specs/swagger-ring_advanced.yml?v=20181013

If you let us know where you saw the "sms-log" endpoint, we'll try to correct it.

If you are trying to pull SMS log data, you can get it from the "message-store" endpoint:

* Reference: https://developer.ringcentral.com/api-reference#SMS-and-MMS-listMessages
Photo of Tyler Long

Tyler Long, Official Rep

  • 8,422 Points 5k badge 2x thumb
Could you please post your code here?
Photo of Tyler Long

Tyler Long, Official Rep

  • 8,422 Points 5k badge 2x thumb
By the way, account/{accountId}/extension/{extensionId}, accountId is not phone number and extensionId is not extension number. They are internal ID assigned by RingCentral.
Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
Tyler,
Thankls here is what I have so far.

from ringcentral_client import RestClient, SANDBOX_SERVER

rc = RestClient("pafw1cB1SJ-xxxxxx", "033e2rxxxxxxxxxxxx", "https://platform.devtest.ringcentral.com")
#/restapi/v1.0/account/~/extension/~/sms
r = rc.post('https://platform.devtest.ringcentral.com/restapi/v1.0/account/nnnnnnnn/extension/nnnn/sms', {
    'to': [{'phoneNumber': "+186nnnnnn"}],
    'from': {'phoneNumber': "+131nnnnn"},
    'text': "test sms."})
print(r.text)

Photo of Tyler Long

Tyler Long, Official Rep

  • 8,422 Points 5k badge 2x thumb
I assume you are using this https://github.com/tylerlong/ringcentral-python


Here is sample code for sending sms: https://github.com/tylerlong/ringcentral-python/blob/master/test/test_subscription.py#L27-L33


rc.post doesn't need to specify full uri.   "/restapi/v1.0/account/~/extension/~/sms" is sufficient.

And you'd better use   /account/~/extension/~  instead of /account/nnnnnnn/extension/nnnnnn.  because ~ means currently logged in user. And most likely you are only allowed to send sms on behalf of the currently logged in user.
Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
Tyler forgot to include
rc.authorize("Username", "password")
Sorry about that.

Photo of Tyler Long

Tyler Long, Official Rep

  • 8,422 Points 5k badge 2x thumb
Correct way to authorize is rc.authorize(username, extension, password)

If you use direct number as username, just use empty string as extension.
Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
Hmm, well now Im getting
Exception: HTTP status code: 400

{
  "error" : "unauthorized_client",
  "error_description" : "Unauthorized for this grant type",
  "errors" : [ {
    "errorCode" : "OAU-251",
    "message" : "Unauthorized for this grant type"
  } ]
}
when I call rc.authorize('+NNNNNNN', '', 'password')
This is absurd I've done this with twilio send and receive in under 30mins...Not sure what the deal is but Im ready to cry uncle.


Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
P.S. Where is BaseTestCase? pip won't import it?
Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
Never mind I pulled it out went to the bare minimum just to send a SMS...
Photo of Tyler Long

Tyler Long, Official Rep

  • 8,422 Points 5k badge 2x thumb
**Q: Why does OAuth return "unauthorized for this grant type" error?**

**A:** The error "unauthorized for grant type" occurs when your application is attempting to use an OAuth grant type that it is not approved to use. This often happens when your app requires the Authorization Code Flow but is attempting to use the Password Flow. See below for information in implementing Authorization Code Flow. For more information see [this Developer Community article](https://devcommunity.ringcentral.com/ringcentraldev/topics/unauthorized-for-this-grant-type-error).


**Q: How to deal with "Unauthorized for this grant type" issue?**<br>

**A:** It probably means your app doesn't support password authorization flow. Please logon https://developer.ringcentral.com and confirm that your app has the following configuration: https://cloud.githubusercontent.com/assets/733544/18026560/b93e5356-6c7d-11e6-92a9-d35d28325e69.png
(Edited)
Photo of Tyler Long

Tyler Long, Official Rep

  • 8,422 Points 5k badge 2x thumb
Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
Tyler thanks
Yes it does with the exception that  Authorization Flow has "refresh access token" as well as Password Flow. Other than that its the same.

P.S. This link does not work https://devcommunity.ringcentral.com/ringcentraldev/topics/unauthorized-for-this-grant-type-error)




Photo of Rick Smith

Rick Smith

  • 412 Points 250 badge 2x thumb
Tyler, I got it worjing! Thanks so much friend, you rock! Had to delete it and start from scratch but it works now! Again thanks 1000% for your patience!!