Exemplo n.º 1
0
        /// <summary>
        ///	Returns access token for making authorized API calls
        /// </summary>
        /// <returns></returns>
        public Task <OAuthCredentials> GetPermanentCredentials(string temporaryToken, string temporaryTokenSecret, string verifierCode)
        {
            Condition.Requires(temporaryToken).IsNotNullOrEmpty();
            Condition.Requires(temporaryTokenSecret).IsNotNullOrEmpty();
            Condition.Requires(verifierCode).IsNotNullOrEmpty();

            var mark = Mark.CreateNew();

            var requestParameters = new Dictionary <string, string>
            {
                { "oauth_token", temporaryToken },
                { "oauth_verifier", verifierCode }
            };

            return(Policy.HandleResult <OAuthCredentials>(credentials => credentials == null)
                   .WaitAndRetryAsync(Config.RetryAttempts,
                                      retryCount => TimeSpan.FromSeconds(ActionPolicy.GetDelayBeforeNextAttempt(retryCount)),
                                      (entityRaw, timeSpan, retryCount, context) =>
            {
                string retryDetails = CreateMethodCallInfo(EtsyEndPoint.GetAccessTokenUrl, mark, additionalInfo: this.AdditionalLogInfo());
                EtsyLogger.LogTraceRetryStarted(timeSpan.Seconds, retryCount, retryDetails);
            })
                   .ExecuteAsync(async() =>
            {
                OAuthCredentials credentials = null;
                string url = Config.ApiBaseUrl + EtsyEndPoint.GetAccessTokenUrl;

                try
                {
                    var oauthParameters = Authenticator.GetOAuthRequestParameters(url, "GET", temporaryTokenSecret, requestParameters);
                    url = Authenticator.GetUrl(url, oauthParameters);

                    EtsyLogger.LogStarted(this.CreateMethodCallInfo(url, mark, additionalInfo: this.AdditionalLogInfo()));

                    HttpResponseMessage response = await HttpClient.GetAsync(url).ConfigureAwait(false);
                    var result = response.Content.ReadAsStringAsync().Result;

                    ThrowIfError(response, result);

                    EtsyLogger.LogEnd(this.CreateMethodCallInfo(url, mark, methodResult: result.ToJson(), additionalInfo: this.AdditionalLogInfo()));

                    var queryParams = Misc.ParseQueryParams(result);
                    queryParams.TryGetValue("oauth_token", out var token);
                    queryParams.TryGetValue("oauth_token_secret", out var tokenSecret);

                    if (!(string.IsNullOrEmpty(token) ||
                          string.IsNullOrEmpty(tokenSecret)))
                    {
                        credentials = new OAuthCredentials(null, token, tokenSecret);
                    }
                }
                catch (Exception exception)
                {
                    var etsyException = new EtsyException(this.CreateMethodCallInfo(url, mark, additionalInfo: this.AdditionalLogInfo()), exception);
                    EtsyLogger.LogTraceException(etsyException);
                }

                return credentials;
            }));
        }
Exemplo n.º 2
0
        /// <summary>
        ///	Returns temporary credentials and login url for customer
        /// </summary>
        /// <param name="scopes">Permissions</param>
        /// <returns></returns>
        public Task <OAuthCredentials> GetTemporaryCredentials(string[] scopes)
        {
            Condition.Requires(scopes).IsNotEmpty();

            var mark = Mark.CreateNew();

            var requestParameters = new Dictionary <string, string>
            {
                { "scopes", string.Join(" ", scopes) },
                { "oauth_callback", "oob" }
            };

            return(Policy.HandleResult <OAuthCredentials>(credentials => credentials == null)
                   .WaitAndRetryAsync(Config.RetryAttempts,
                                      retryCount => TimeSpan.FromSeconds(ActionPolicy.GetDelayBeforeNextAttempt(retryCount)),
                                      (entityRaw, timeSpan, retryCount, context) =>
            {
                string retryDetails = CreateMethodCallInfo(EtsyEndPoint.GetRequestTokenUrl, mark, additionalInfo: this.AdditionalLogInfo());
                EtsyLogger.LogTraceRetryStarted(timeSpan.Seconds, retryCount, retryDetails);
            })
                   .ExecuteAsync(async() =>
            {
                OAuthCredentials credentials = null;

                string absoluteUrl = Config.ApiBaseUrl + EtsyEndPoint.GetRequestTokenUrl;

                var oauthParameters = Authenticator.GetOAuthRequestParameters(absoluteUrl, "GET", null, requestParameters);
                string url = Authenticator.GetUrl(absoluteUrl, oauthParameters);

                try
                {
                    EtsyLogger.LogStarted(this.CreateMethodCallInfo(url, mark, additionalInfo: this.AdditionalLogInfo()));

                    HttpResponseMessage response = await HttpClient.GetAsync(url).ConfigureAwait(false);

                    var result = response.Content.ReadAsStringAsync().Result;

                    EtsyLogger.LogEnd(this.CreateMethodCallInfo(url, mark, methodResult: result.ToJson(), additionalInfo: this.AdditionalLogInfo()));

                    ThrowIfError(response, result);

                    if (!string.IsNullOrEmpty(result) &&
                        result.IndexOf("login_url", StringComparison.InvariantCulture) > -1)
                    {
                        string loginUrl = Uri.UnescapeDataString(result.Replace("login_url=", ""));

                        string[] temp = loginUrl.Split('?');

                        if (temp.Length == 2)
                        {
                            var queryParams = Misc.ParseQueryParams(temp[1]);
                            queryParams.TryGetValue("oauth_token", out var token);
                            queryParams.TryGetValue("oauth_token_secret", out var tokenSecret);

                            if (token != null && tokenSecret != null)
                            {
                                credentials = new OAuthCredentials(loginUrl, token, tokenSecret);
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    var etsyException = new EtsyException(this.CreateMethodCallInfo(url, mark, additionalInfo: this.AdditionalLogInfo()), exception);
                    EtsyLogger.LogTraceException(etsyException);
                }

                return credentials;
            }));
        }