/// <summary>
        /// Gets a new access token from the specified <code>refreshToken</code>.
        /// </summary>
        /// <param name="refreshToken">The refresh token of the user.</param>
        /// <returns>Returns an access token based on the specified <code>refreshToken</code>.</returns>
        public MicrosoftTokenResponse GetAccessTokenFromRefreshToken(string refreshToken)
        {
            // Some validation
            if (String.IsNullOrWhiteSpace(ClientId))
            {
                throw new PropertyNotSetException("ClientId");
            }
            if (String.IsNullOrWhiteSpace(ClientSecret))
            {
                throw new PropertyNotSetException("ClientSecret");
            }
            if (String.IsNullOrWhiteSpace(RedirectUri))
            {
                throw new PropertyNotSetException("RedirectUri");
            }
            if (String.IsNullOrWhiteSpace(refreshToken))
            {
                throw new ArgumentNullException("refreshToken");
            }

            // Initialize the POST data
            NameValueCollection data = new NameValueCollection {
                { "client_id", ClientId },
                { "redirect_uri", RedirectUri },
                { "client_secret", ClientSecret },
                { "refresh_token", refreshToken },
                { "grant_type", "refresh_token" }
            };

            // Make the call to the API
            SocialHttpResponse response = SocialUtils.Http.DoHttpPostRequest("https://login.live.com/oauth20_token.srf", null, data);

            // Parse the response
            return(MicrosoftTokenResponse.ParseResponse(response));
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance based on the specified refresh token.
        /// </summary>
        /// <param name="clientId">The client ID.</param>
        /// <param name="clientSecret">The client secret.</param>
        /// <param name="refreshToken">The refresh token of the user.</param>
        public static MicrosoftService CreateFromRefreshToken(string clientId, string clientSecret, string refreshToken)
        {
            // Some validation
            if (String.IsNullOrWhiteSpace(clientId))
            {
                throw new ArgumentNullException("clientId");
            }
            if (String.IsNullOrWhiteSpace(clientSecret))
            {
                throw new ArgumentNullException("clientSecret");
            }
            if (String.IsNullOrWhiteSpace(refreshToken))
            {
                throw new ArgumentNullException("refreshToken");
            }

            // Initialize a new OAuth client
            MicrosoftOAuthClient client = new MicrosoftOAuthClient(clientId, clientSecret);

            // Get an access token from the refresh token.
            MicrosoftTokenResponse response = client.GetAccessTokenFromRefreshToken(refreshToken);

            // Update the OAuth client with the access token
            client.AccessToken = response.Body.AccessToken;

            // Initialize a new service instance
            return(new MicrosoftService(client));
        }