/// <summary>
        /// Retrieves access token from LWA
        /// </summary>
        /// <param name="lwaAccessTokenRequestMeta">LWA AccessTokenRequest metadata</param>
        /// <returns>LWA Access Token</returns>
        public virtual string GetAccessToken()
        {
            LWAAccessTokenRequestMeta lwaAccessTokenRequestMeta = LWAAccessTokenRequestMetaBuilder.Build(LWAAuthorizationCredentials);
            var accessTokenRequest = new RestRequest(LWAAuthorizationCredentials.Endpoint.AbsolutePath, Method.POST);

            string jsonRequestBody = JsonConvert.SerializeObject(lwaAccessTokenRequestMeta);

            accessTokenRequest.AddParameter(JsonMediaType, jsonRequestBody, ParameterType.RequestBody);

            string accessToken;

            try
            {
                var response = RestClient.Execute(accessTokenRequest);

                if (!IsSuccessful(response))
                {
                    throw new IOException("Unsuccessful LWA token exchange", response.ErrorException);
                }

                JObject responseJson = JObject.Parse(response.Content);

                accessToken = responseJson.GetValue(AccessTokenKey).ToString();
            }
            catch (Exception e)
            {
                throw new SystemException("Error getting LWA Access Token", e);
            }

            return(accessToken);
        }
Exemplo n.º 2
0
        public override bool Equals(object obj)
        {
            LWAAccessTokenRequestMeta other = obj as LWAAccessTokenRequestMeta;

            return(other != null &&
                   this.GrantType == other.GrantType &&
                   this.RefreshToken == other.RefreshToken &&
                   this.ClientId == other.ClientId &&
                   this.ClientSecret == other.ClientSecret &&
                   this.Scope == other.Scope);
        }
        /// <summary>
        /// Builds an instance of LWAAccessTokenRequestMeta modeling appropriate LWA token
        /// request params based on configured LWAAuthorizationCredentials
        /// </summary>
        /// <param name="lwaAuthorizationCredentials">LWA Authorization Credentials</param>
        /// <returns></returns>
        public virtual LWAAccessTokenRequestMeta Build(LWAAuthorizationCredentials lwaAuthorizationCredentials)
        {
            LWAAccessTokenRequestMeta lwaAccessTokenRequestMeta = new LWAAccessTokenRequestMeta()
            {
                ClientId     = lwaAuthorizationCredentials.ClientId,
                ClientSecret = lwaAuthorizationCredentials.ClientSecret,
                RefreshToken = lwaAuthorizationCredentials.RefreshToken
            };

            if (lwaAuthorizationCredentials.Scopes == null || lwaAuthorizationCredentials.Scopes.Count == 0)
            {
                lwaAccessTokenRequestMeta.GrantType = SellerAPIGrantType;
            }
            else
            {
                lwaAccessTokenRequestMeta.Scope     = string.Join(Delimiter, lwaAuthorizationCredentials.Scopes);
                lwaAccessTokenRequestMeta.GrantType = SellerlessAPIGrantType;
            }

            return(lwaAccessTokenRequestMeta);
        }