Esempio n. 1
0
        /// <summary>
        /// Refresh the ParticleCloud Access Token. The AccessToken on the ParticleCloud instance will be updated.
        /// A return value of Null indicates the refresh was unsuccessful.
        /// </summary>
        /// <returns>The AccessToken.</returns>
        /// <param name="appName">App name.</param>
        public async Task <ParticleAccessToken> RefreshTokenAsync(string appName)
        {
            var requestContent = new FormUrlEncodedContent(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),
            });

            try
            {
                var response = await client.PostAsync(
                    TOKEN_URI_ENDPOINT + "?refresh_token=" + AccessToken.RefreshToken,
                    requestContent
                    );

                var particleResponse = DeserializeObject <ParticleLoginResponse>(await response.Content.ReadAsStringAsync());

                if (particleResponse.AccessToken != null || !IsNullOrEmpty(particleResponse.AccessToken))
                {
                    AccessToken = new ParticleAccessToken(particleResponse);
                    IsLoggedIn  = true;
                    return(AccessToken);
                }
            }
            catch (Exception e)
            {
                WriteLine(e.Message);
            }

            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// Log in with a given client id and client secret.
        /// </summary>
        /// <returns>A boolean indicating whether the login attempt was successful or not.</returns>
        /// <param name="username">Username.</param>
        /// <param name="password">Password.</param>
        public async Task <bool> LoginWithClientCredentialsAsync(string clientid, string clientsecret)
        {
            var requestContent = new FormUrlEncodedContent(new[] {
                new KeyValuePair <string, string> ("grant_type", "client_credentials"),
                new KeyValuePair <string, string> ("client_id", clientid),
                new KeyValuePair <string, string> ("client_secret", clientsecret),
            });

            try
            {
                using (var client = new HttpClient(new NativeMessageHandler()))
                {
                    var response = await client.PostAsync(TOKEN_URI_ENDPOINT, requestContent);

                    var particleResponse = DeserializeObject <ParticleLoginResponse>(await response.Content.ReadAsStringAsync());

                    if (particleResponse.AccessToken != null || !IsNullOrEmpty(particleResponse.AccessToken))
                    {
                        AccessToken      = new ParticleAccessToken(particleResponse);
                        LoggedInUsername = clientid;
                        IsLoggedIn       = true;
                        return(true);
                    }
                }
            }
            catch (Exception e)
            {
                WriteLine(e.Message);
            }
            return(false);
        }
Esempio n. 3
0
        /// <summary>
        /// Log in with a given username or password.
        /// </summary>
        /// <returns>A boolean indicating whether the login attempt was successful or not.</returns>
        /// <param name="username">Username.</param>
        /// <param name="password">Password.</param>
        public async Task <bool> LoginWithUserAsync(string username, string password)
        {
            var requestContent = new FormUrlEncodedContent(new[] {
                new KeyValuePair <string, string> ("grant_type", "password"),
                new KeyValuePair <string, string> ("username", username),
                new KeyValuePair <string, string> ("password", password),
                new KeyValuePair <string, string> ("client_id", OAuthClientId),
                new KeyValuePair <string, string> ("client_secret", OAuthClientSecret),
            });

            try
            {
                var response = await client.PostAsync(TOKEN_URI_ENDPOINT, requestContent);

                var particleResponse = DeserializeObject <ParticleLoginResponse>(await response.Content.ReadAsStringAsync());

                if (particleResponse.AccessToken != null || !IsNullOrEmpty(particleResponse.AccessToken))
                {
                    AccessToken      = new ParticleAccessToken(particleResponse);
                    LoggedInUsername = username;
                    IsLoggedIn       = true;
                    return(true);
                }
            }
            catch (Exception e)
            {
                WriteLine(e.Message);
            }
            return(false);
        }
Esempio n. 4
0
 public ParticleCloud(string accessToken, string refreshToken, DateTime expiration)
 {
     AccessToken       = new ParticleAccessToken(accessToken, refreshToken, expiration);
     OAuthClientId     = "particle";
     OAuthClientSecret = "particle";
 }