Esempio n. 1
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;
            }));
        }