How to use the Refresh Token?

I noticed a refresh token passed back from my Particle Cloud, but I can’t find any information or documentation on how to use it. Does anyone have an example of using the refresh token to update the access token, or is this not implemented yet by Particle?

1 Like

It’s an OAuth thing. Take a look here: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_refresh_token_oauth.htm

Yup, I’ve done that before. So should I just hit the Particle oauth/token end point and provide the refresh token, client id and client secret? Sorry, I’m doing this all in C# using Xamarin for iOS and Android applications. I’m trying to recreate the iOS SDK and I didn’t see the use of the refresh token in it anywhere.

This is a great question for @Dave I believe. Let’s see if we can get him to chime in

1 Like

Thanks for the help. I solved this and I’ll paste my solution below. Of course I need to beef up my error responses.:

public async Task RefreshToken(string appName) {
	ParticleLoginResponse particleResponse = null;

	var requestParameters = new [] {
		new KeyValuePair<string, string> ("name", appName),
		new KeyValuePair<string, string> ("type", "installed"),
		new KeyValuePair<string, string> ("grant_type", "refresh_token"),
		new KeyValuePair<string, string> ("client_id", OAuthClientId),
		new KeyValuePair<string, string> ("client_secret", OAuthClientSecret),
		new KeyValuePair<string, string> ("access_token", AccessToken.Token),
		new KeyValuePair<string, string> ("refresh_token", AccessToken.RefreshToken),
	};

	var requestContent = new FormUrlEncodedContent (requestParameters);

	try{
		using (var client = new HttpClient (new NativeMessageHandler())) {
			HttpResponseMessage response = await client.PostAsync(
				"https://api.particle.io/oauth/token/?refresh_token=" + AccessToken.RefreshToken,
				requestContent
			);

			using (var reader = new StreamReader(await response.Content.ReadAsStreamAsync())) {
			var responseText = await reader.ReadToEndAsync();
			particleResponse = JsonConvert.DeserializeObject<ParticleLoginResponse>(responseText);
		        }
	        } 
	}catch(Exception e) {
		Console.WriteLine (e.StackTrace.ToString ());
	}

	if (particleResponse.access_token != null || particleResponse.access_token != "") {
		AccessToken = new ParticleAccessToken (particleResponse);
	IsLoggedIn = true;
	Console.WriteLine ("Access Token: " + AccessToken.Token);
	Console.WriteLine ("Refresh Token: " + AccessToken.RefreshToken);
}

}

2 Likes

For the record, here’s the curl version.

Requirements: You need a refresh token that is associated with an OAuth client. The grant type refresh_token won’t work with a regular refresh token generated using the particle:particle credentials.

Then, you can fetch your new credentials like this:

curl https://api.particle.io/oauth/token \
    -d grant_type=refresh_token \
    -d refresh_token='<your-refresh-token>'