I’ve been having trouble using the refresh token obtained during an oauth flow. I’ve actually never been able to get it to work properly, but it’s becoming a bigger issue as our project matures. As per the example at the end of the docs, this is my curl request, I’ve just recently been testing it from command line to make sure there isn’t some unknown variable in another library:
Obviously I’ve replaced the client ID, secret, and refresh token with my actual credentials. This refresh token was obtained 2 days ago, and I always get a response of:
For all practical purposes, it’s impossible to use the refresh token.
The problem is that you need to specify the client ID and secret, which essentially makes it the same as regular token grant, so there’s really no reason to use the refresh token as it’s currently implemented.
I am, in fact, supplying the client ID and secret… How can I implement getting a new token before the existing one expires without interaction from the end user?
Thanks for posting these details. We’ll get to the bottom of this.
One clarification regarding refresh tokens: currently (February 2019) only password grant is supported for the default client (particle:particle). So if you are not using a proper OAuth client, refresh tokens won’t work. If you are using a proper OAuth client, refresh tokens work.
Refresh tokens can only be used once. When you use the refresh token, you’ll get another one that you have to store and use next time.
In the case of @jryd I see the refresh token has been used up. This may be the case for @zaphod-42 as well.
Here’s an example of using refresh tokens.
# create a token with grant type password or client_credentials, with or without a customer=a@b.com scope and get an access_token and refresh_token
$ curl -X POST -u two-legged-auth:________________________________________ \
-d grant_type=client_credentials https://api.particle.io/oauth/token
{
"token_type": "bearer",
"access_token": "e920____________________________________",
"expires_in": 7776000,
"refresh_token": "b45f____________________________________"
}
# use the refresh token
$ curl -X POST -u two-legged-auth:________________________________________ \
-d grant_type=refresh_token \
-d refresh_token=b45f____________________________________ \
https://api.particle.io/oauth/token
{
"token_type": "bearer",
"access_token": "e03c____________________________________",
"expires_in": 7776000,
"refresh_token": "2b74____________________________________"
}
# try using the same refresh token later
$ curl -X POST -u two-legged-auth:________________________________________ \
-d grant_type=refresh_token \
-d refresh_token=b45f____________________________________ \
https://api.particle.io/oauth/token
{
"error": "invalid_grant",
"error_description": "Invalid refresh token"
}
# use the new refresh token
$ curl -X POST -u two-legged-auth:________________________________________ \
-d grant_type=refresh_token \
-d refresh_token=2b74____________________________________ \
https://api.particle.io/oauth/token
{
"token_type": "bearer",
"access_token": "069b____________________________________",
"expires_in": 7776000,
"refresh_token": "51a9____________________________________"
}
Thank you for that, somehow in my troubleshooting I completely overlooked the fact the the refresh token may have been used… I was grabbing the token out as soon as I retrieved it on my server, but had forgotten that I was forcing a refresh for testing immediately