question

rick-smith avatar image
rick-smith asked ·

Refresh Token has Expired in Python

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.


topic-default
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.

Phong Vu avatar image
Phong Vu answered ·
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
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.

rick-smith avatar image
rick-smith answered ·
Phong
Thanks for your assistance. How do I check the refresh token?
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.

Phong Vu avatar image
Phong Vu answered ·
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.

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.

rick-smith avatar image
rick-smith answered ·
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?



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.

Tyler Liu avatar image
Tyler Liu answered ·
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.

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.

rick-smith avatar image
rick-smith answered ·
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





1 comment 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.

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.

0 Likes 0 · ·
rick-smith avatar image
rick-smith answered ·
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.

6 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.

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
0 Likes 0 · ·
Could you please post your code here?
0 Likes 0 · ·
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.
0 Likes 0 · ·
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)

0 Likes 0 · ·
Show more comments
rick-smith avatar image
rick-smith answered ·
Tyler forgot to include
rc.authorize("Username", "password")
Sorry about that.

1 comment 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.

Correct way to authorize is rc.authorize(username, extension, password)

If you use direct number as username, just use empty string as extension.
0 Likes 0 · ·
rick-smith avatar image
rick-smith answered ·
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.


5 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.

P.S. Where is BaseTestCase? pip won't import it?
0 Likes 0 · ·
Never mind I pulled it out went to the bare minimum just to send a SMS...
0 Likes 0 · ·
**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
0 Likes 0 · ·
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)




0 Likes 0 · ·
rick-smith avatar image
rick-smith answered ·
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!!
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.

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.