question

rick-smith2386 avatar image
rick-smith2386 asked rick-smith2386 answered

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.



getting started
1 |3000

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
1 |3000

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

rick-smith2386 avatar image
rick-smith2386 answered
Phong
Thanks for your assistance. How do I check the refresh token?
1 |3000

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.

1 |3000

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

rick-smith2386 avatar image
rick-smith2386 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?



1 |3000

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.

1 |3000

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

rick-smith2386 avatar image
rick-smith2386 answered Tyler Liu commented
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
1 |3000

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 ♦ commented ·
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-smith2386 avatar image
rick-smith2386 answered Tyler Liu commented
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
1 |3000

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

John Wang avatar image John Wang ♦♦ commented ·
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 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
Could you please post your code here?
0 Likes 0 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
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 ·
rick-smith2386 avatar image rick-smith2386 commented ·
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 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
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.
0 Likes 0 ·
rick-smith2386 avatar image
rick-smith2386 answered Tyler Liu commented
Tyler forgot to include
rc.authorize("Username", "password")
Sorry about that.

1 comment
1 |3000

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 ♦ commented ·
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-smith2386 avatar image
rick-smith2386 answered rick-smith2386 commented
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
1 |3000

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

rick-smith2386 avatar image rick-smith2386 commented ·
P.S. Where is BaseTestCase? pip won't import it?
0 Likes 0 ·
rick-smith2386 avatar image rick-smith2386 commented ·
Never mind I pulled it out went to the bare minimum just to send a SMS...
0 Likes 0 ·
Tyler Liu avatar image Tyler Liu ♦ commented ·
**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 Liu avatar image Tyler Liu ♦ commented ·
0 Likes 0 ·
rick-smith2386 avatar image rick-smith2386 commented ·
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-smith2386 avatar image
rick-smith2386 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!!
1 |3000

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

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys