/// <summary>
        /// Refreshes a token.
        /// </summary>
        /// <param name="refreshToken">The refresh token value.</param>
        /// <returns>A new token information.</returns>
        public DriveResponse <DriveTokenInfo> RefreshToken(string refreshToken)
        {
            const string refreshTokenArgumentName = "refreshToken";

            if (string.IsNullOrEmpty(refreshToken))
            {
                throw new ArgumentException(string.Format(LocalStrings.ArgumentNullOrEmptyErrorMessage1, refreshTokenArgumentName), refreshTokenArgumentName);
            }

            var authData = AuthData;
            var request  = new RestRequest(ServiceDefs.Auth.TokenResource, Method.POST);

            request.AddParameter(ServiceDefs.Auth.RefreshTokenParameterName, refreshToken)
            .AddParameter(ServiceDefs.Auth.ClientIdParameterName, authData.ClientId)
            .AddParameter(ServiceDefs.Auth.ClientSecretParameterName, authData.ClientSecret)
            .AddParameter(ServiceDefs.Auth.GrantTypeParameterName, ServiceDefs.Auth.GrantTypeRefreshTokenParameterValue);
            var client   = AccountClient;
            var response = RequestHandler.Request <DriveTokenInfo>(client, request, HttpStatusCode.OK);
            var result   = new DriveResponse <DriveTokenInfo>(response, t =>
            {
                t.CreatedAt = DateTime.UtcNow;

                if (string.IsNullOrEmpty(t.RefreshToken))
                {
                    t.RefreshToken = refreshToken;
                }
            });

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Executes a Drive request.
        /// </summary>
        /// <typeparam name="T">The requested data type.</typeparam>
        /// <param name="request">The request.</param>
        /// <returns>A response.</returns>
        public DriveResponse <T> Execute <T>(IDriveRequest <T> request) where T : IDriveData, new()
        {
            Log.Debug("Execute.");
            DriveResponse <T>  result = null;
            RequestErrorAction errorAction;

            do
            {
                try
                {
                    result = request.Execute(this);
                    break;
                }
                catch (InteractionException exception)
                {
                    Log.TraceException("Execute. Exception", exception);
                    errorAction = RaiseRequestError(exception, RequestErrorAction.Alert);
                    Log.Trace("Execute. NextAction: \"{0}\"", errorAction);

                    if (errorAction == RequestErrorAction.Alert)
                    {
                        throw;
                    }
                }
            } while (errorAction == RequestErrorAction.Retry);

            return(result);
        }
        /// <summary>
        /// Refreshes a token.
        /// </summary>
        /// <param name="tokenInfo">The token information.</param>
        /// <returns>A new token information.</returns>
        public DriveResponse <DriveTokenInfo> RefreshToken(DriveTokenInfo tokenInfo)
        {
            const string tokenInfoArgumentName = "tokenInfo";

            if (tokenInfo == null)
            {
                throw new ArgumentNullException(tokenInfoArgumentName);
            }

            DriveResponse <DriveTokenInfo> result = RefreshToken(tokenInfo.RefreshToken);

            return(result);
        }
        /// <summary>
        /// Confirms an authorization code.
        /// </summary>
        /// <param name="code">The authorization code.</param>
        /// <returns>A response with token information.</returns>
        public DriveResponse <DriveTokenInfo> ConfirmAuthorization(string code)
        {
            const string codeArgumentName = "code";

            if (string.IsNullOrEmpty(code))
            {
                throw new ArgumentException(string.Format(LocalStrings.ArgumentNullOrEmptyErrorMessage1, codeArgumentName), codeArgumentName);
            }

            var authData = AuthData;

            var request = new RestRequest(ServiceDefs.Auth.TokenResource, Method.POST);

            request.AddParameter(ServiceDefs.Auth.CodeParameterName, code)
            .AddParameter(ServiceDefs.Auth.ClientIdParameterName, authData.ClientId)
            .AddParameter(ServiceDefs.Auth.ClientSecretParameterName, authData.ClientSecret)
            .AddParameter(ServiceDefs.Auth.RedirectUriParameterName, authData.RedirectUri)
            .AddParameter(ServiceDefs.Auth.GrantTypeParameterName, ServiceDefs.Auth.GrantTypeAuthCodeParameterValue);
            var client   = AccountClient;
            var response = RequestHandler.Request <DriveTokenInfo>(client, request, HttpStatusCode.OK);
            var result   = new DriveResponse <DriveTokenInfo>(response, t => t.CreatedAt = DateTime.UtcNow);

            return(result);
        }