public PermanentToken GetPermanentTokenCredentials(string apiKey, string sharedSecret, TemporaryToken tempToken, string validator)
        {
            //consumerKey is the appKey you got when you registered your app, same for sharedSecret
            _restClient.Authenticator = _restServiceWrapper.GetAuthenticatorForAccessToken(apiKey, sharedSecret, tempToken, validator);

            RestRequest restRequest = _restServiceWrapper.GetRestRequest("oauth/access_token", Method.GET);

            IRestResponse irestResponse = _restClient.Execute(restRequest);

            var args = ParseQueryString(irestResponse.Content);

            var permanentToken = new PermanentToken
            {
                APIKey       = apiKey,
                SharedSecret = sharedSecret,
                TokenID      = args.First(x => x.Key == "oauth_token").Value,
                TokenSecret  = args.First(x => x.Key == "oauth_token_secret").Value
            };


            if (string.IsNullOrEmpty(permanentToken.TokenID) || string.IsNullOrEmpty(permanentToken.TokenSecret))
            {
                throw new NullReferenceException("Unable to retrieve permanent auth token.  Please check your credentials and try again.");
            }

            return(permanentToken);
        }
Ejemplo n.º 2
0
        public bool AddImageToListing(Listing listing, PermanentToken token)
        {
            if (string.IsNullOrEmpty(listing.ID))
            {
                throw new EtsyWrapperException("AddImageToListing:  Listing ID can not be empty!");
            }
            RestRequest request = _restServiceWrapper.GetRestRequest($"listings/{listing.ID}/images", Method.POST);

            _restClient.Authenticator = _restServiceWrapper.GetAuthenticatorForProtectedResource(token);

            request.AddHeader("Accept", "application/json");
            request.Parameters.Clear();
            foreach (var image in listing.Images)
            {
                if (image == null)
                {
                    continue;
                }
                request.AddFile("image", image.ImagePath);
                request.AddParameter("application/json", JsonConvert.SerializeObject(listing.Images), ParameterType.RequestBody);
                var etsyResponse = _restClient.Execute(request);
                if (etsyResponse.StatusCode != HttpStatusCode.Created)
                {
                    throw new Exception(
                              $"Create Listing Image failed.  Please check your parameters and try again. Error: {etsyResponse.Content}");
                }
            }

            return(true);
        }
Ejemplo n.º 3
0
        public Listing CreateListing(Listing listing, PermanentToken authToken)
        {
            listing.State = "draft";
            if (!authToken.IsValidEtsyToken())
            {
                throw new EtsyWrapperException("Auth token is not valid!  Please authenticate before calling the CreateListing method.");
            }
            RestRequest request = _restServiceWrapper.GetRestRequest("listings", Method.POST);

            _restClient.Authenticator = _restServiceWrapper.GetAuthenticatorForProtectedResource(authToken);

            request.AddHeader("Accept", "application/json");
            request.AddParameter("application/json", JsonConvert.SerializeObject(listing), ParameterType.RequestBody);

            var etsyResponse = _restClient.Execute(request);

            if (etsyResponse.StatusCode != HttpStatusCode.Created)
            {
                throw new EtsyWrapperException(
                          $"Create Listing failed.  Please check your parameters and try again. Error: {etsyResponse.Content}");
            }
            var listingResponse = JsonConvert.DeserializeObject <ListingResponse>(etsyResponse.Content);

            listing.ID = listingResponse.Listing[0].ID;
            return(listing);
        }
Ejemplo n.º 4
0
        public void UpdateListing(Listing listing, PermanentToken authToken)
        {
            RestRequest request = _restServiceWrapper.GetRestRequest($"listings/{listing.ID}", Method.PUT);

            _restClient.Authenticator = _restServiceWrapper.GetAuthenticatorForProtectedResource(authToken);

            request.AddHeader("Accept", "application/json");
            var etsyResponse = _restClient.Execute(request);

            if (etsyResponse.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception(
                          $"Create Listing failed.  Please check your parameters and try again. Error: {etsyResponse.Content}");
            }
        }
Ejemplo n.º 5
0
        private static PermanentToken GetAuthToken(EtsyListItArgs args)
        {
            var permanentToken = new PermanentToken();

            // If API Key or Shared Secret not found either place, throw error.
            if (args.APIKey.IsNullOrEmpty() || args.SharedSecret.IsNullOrEmpty())
            {
                throw new EtsyListItException(
                          "Must specify API Key and Shared Secret to authenticate.  Please input command line arg -a [value] for API Key and -ss [value] for SharedSecret to continue.");
            }

            permanentToken.APIKey       = args.APIKey;
            permanentToken.SharedSecret = args.SharedSecret;
            permanentToken.TokenID      = _settingsUtility.GetEncryptedAppSetting("PermanentAuthToken");
            permanentToken.TokenSecret  = _settingsUtility.GetEncryptedAppSetting("PermanentSecret");
            // If permanent auth token is not found, get it.
            if (permanentToken.TokenID.IsNullOrEmpty() || permanentToken.TokenSecret.IsNullOrEmpty())
            {
                var permissions = new[] { "listings_r", "listings_w" };
                var validator   = string.Empty;
                IEtsyAuthenticationWrapper authenticationWrapper =
                    _container.GetInstance <IEtsyAuthenticationWrapper>();
                var tempCredentials =
                    authenticationWrapper.GetTemporaryCredentials(args.APIKey, args.SharedSecret, permissions);
                Process.Start(tempCredentials.LoginURL);
                while (validator.IsNullOrEmpty())
                {
                    Console.Write("Enter your Etsy validator from the web browser to continue: ");
                    validator = Console.ReadLine();
                }

                permanentToken =
                    authenticationWrapper.GetPermanentTokenCredentials(args.APIKey, args.SharedSecret, tempCredentials, validator);
            }

            _settingsUtility.SetAppSettingWithEncryption("PermanentAuthToken", permanentToken.TokenID);
            _settingsUtility.SetAppSettingWithEncryption("PermanentSecret", permanentToken.TokenSecret);

            return(permanentToken);
        }
Ejemplo n.º 6
0
 public IAuthenticator GetAuthenticatorForProtectedResource(PermanentToken authToken)
 {
     return(OAuth1Authenticator.ForProtectedResource(authToken.APIKey, authToken.SharedSecret, authToken.TokenID,
                                                     authToken.TokenSecret));
 }
Ejemplo n.º 7
0
 public Listing CreateDigitalListingWithImages(Listing listing, PermanentToken authToken)
 {
     listing = CreateListingWithImage(listing, authToken);
     AddDigitalFileToListing(listing, authToken);
     return(listing);
 }
Ejemplo n.º 8
0
 public Listing CreateListingWithImage(Listing listing, PermanentToken authToken)
 {
     listing = CreateListing(listing, authToken);
     AddImageToListing(listing, authToken);
     return(listing);
 }