RefreshTokenAsync() public méthode

Refreshes the access token asynchronous.
public RefreshTokenAsync ( ) : Task
Résultat Task
Exemple #1
0
        /// <summary>
        /// Gets the variable value asynchronous for the provided <paramref name="variable"/>.
        /// </summary>
        /// <param name="variable">The variable.</param>
        /// <returns></returns>
        public async Task <Result <Variable> > GetVariableValueAsync(Variable variable)
        {
            if (variable == null)
            {
                throw new ArgumentNullException(nameof(variable));
            }

            if (String.IsNullOrWhiteSpace(variable.Name))
            {
                throw new ArgumentException(RH.C.GetString("PassedVariableMustHaveAName"), nameof(variable));
            }

            var svariable = await Task.Run(() => variables.FirstOrDefault(i => i == variable));

            if (svariable == null)
            {
                ParticleCloud.SyncContext.InvokeIfRequired(() =>
                {
                    variables.Add(variable);
                });
            }
            try
            {
                var response = await cloud.MakeGetRequestAsync($"devices/{Id}/{Uri.EscapeDataString(variable.Name)}");

                if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                {
                    await cloud.RefreshTokenAsync();

                    response = await cloud.MakeGetRequestAsync($"devices/{Id}/{Uri.EscapeDataString(variable.Name)}");

                    if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
                    {
                        return(response.AsResult <Variable>());
                    }
                }

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var tresult = response.Response.SelectToken("result");
                    variable.Value = tresult.Value <Object>().ToString();

                    /*
                     * "name": "temperature",
                     * "result": 46,
                     * "coreInfo": {
                     * "name": "weatherman",
                     * "deviceID": "0123456789abcdef01234567",
                     * "connected": true,
                     * "last_handshake_at": "2015-07-17T22:28:40.907Z",
                     * "last_app": ""
                     * }*/

                    return(new Result <Variable>
                    {
                        Success = true,
                        Data = variable
                    });
                }
                else
                {
                    return(response.AsResult <Variable>());
                }
            }
            catch (HttpRequestException re)
            {
                return(new Result <Variable>
                {
                    Success = false,
                    Error = re.Message,
                    Exception = re
                });
            }
        }
		public async Task RefreshToken_Test()
		{
			using (var cloud = new ParticleCloud())
			{
				var status = await cloud.LoginWithUserAsync(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"], 1);
				Assert.IsTrue(status.Success);
				await Task.Delay(2000);

				var req = await cloud.MakeGetRequestAsync("devices");
				Assert.AreEqual(HttpStatusCode.Unauthorized, req.StatusCode);

				status = await cloud.RefreshTokenAsync();
				Assert.IsTrue(status.Success);
			}
		}