Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new item using Python Etsy HTTP API methods?

Tags:

python

http

I am trying to use Etsy API to add a new listing on my store. In the documents section it says (below section how to do it). First fyi I have never used HTTP Method before so I am not sure how to setup the code so that it adds a new item.

(Link to the Etsy API page https://www.etsy.com/developers/documentation/reference/listing).

Method Name createListing
Synopsis    Creates a new Listing.
HTTP Method POST
URI /listings
Parameters  
Name    Required    Default Type
quantity    Y       int
title   Y       string
description Y       text
price   Y       float
materials   N       array(string)
shipping_template_id    N       int
shop_section_id N       int
image_ids   N       array(int)
is_customizable N       boolean
non_taxable N       boolean
image   N       image
state   N   active  enum(active, draft)
processing_min  N       int
processing_max  N       int
category_id N       int
taxonomy_id N       int
tags    N       array(string)
who_made    Y       enum(i_did, collective, someone_else)
is_supply   Y       boolean
when_made   Y       enum(made_to_order, 2010_2017, 2000_2009, 1998_1999, before_1998, 1990_1997, 1980s, 1970s, 1960s, 1950s, 1940s, 1930s, 1920s, 1910s, 1900s, 1800s, 1700s, before_1700)
recipient   N       enum(men, women, unisex_adults, teen_boys, teen_girls, teens, boys, girls, children, baby_boys, baby_girls, babies, birds, cats, dogs, pets, not_specified)
occasion    N       enum(anniversary, baptism, bar_or_bat_mitzvah, birthday, canada_day, chinese_new_year, cinco_de_mayo, confirmation, christmas, day_of_the_dead, easter, eid, engagement, fathers_day, get_well, graduation, halloween, hanukkah, housewarming, kwanzaa, prom, july_4th, mothers_day, new_baby, new_years, quinceanera, retirement, st_patricks_day, sweet_16, sympathy, thanksgiving, valentines, wedding)
style   N       array(string)
Requires OAuth  Y
Permission Scope    listings_w
Notes   
A shipping_template_id is required when creating a listing.
All listings created on www.etsy.com must be actual items for sale. Please see our guidelines for testingwith live listings.
Creating a listing creates a single inventory products with the supplied price and quantity. Use updateInventory to add more products.

The code I have right know looks like this

import urllib
import requests
url = 'https://openapi.etsy.com/v2/listings/active?api_key={YOUR KEY HERE)' # I put my API key here 
r = requests.get(url)

payload = {'quantity': '1', 'title': 'testdfsdfdfs0','description': 'dfsdfsdfsdfdsf','price': '2.55','who_made': 'i_did','is_supply': '0','when_made': '2010_2017'}
rrr = requests.post(url,payload)
print rrr # I get an error 404 

How can I add an item for sale on Etsy through Python HTTP method?

Update

from requests_oauthlib import OAuth1Session
import requests
from requests_oauthlib import OAuth1
import json


tempory_token_url = []
oauth_response_bucket = []     
client_key = '.......'
client_secret = '......'

oauth = OAuth1Session(client_key, client_secret=client_secret)

request_token_url = 'https://openapi.etsy.com/v2/oauth/request_token?scope=email_r%20listings_r'
fetch_response = oauth.fetch_request_token(request_token_url)    
resource_owner_key = fetch_response.get('oauth_token') # Have it
resource_owner_secret = fetch_response.get('oauth_token_secret')
oauth_url_temp = tempory_token_url[0]['login_urI']
base_authorization_url = oauth_url_temp
authorization_url = oauth.authorization_url(base_authorization_url)
redirect_response = raw_input('Paste the full redirect URL here: ') 
oauth_response = oauth.parse_authorization_response(redirect_response)
verifier = oauth_response.get('oauth_verifier')
access_token_url = redeirect_response 
oauth = OAuth1Session(client_key=client_secret=client_secret,resource_owner_key=resource_owner_key,resource_owner_secret=resource_owner_secret,verifier=verifier)

oauth_tokens = oauth.fetch_access_token(access_token_url)
resource_owner_key = oauth_tokens.get('oauth_token')
resource_owner_secret = oauth_tokens.get('oauth_token_secret')

Any ideas how to make this work? There is very little info regarding Etsy API and most of the stuff is in PHP which I have no clue how to work.

Image Uploading API

Everything looks the same like above this time I just changed the payload but I am getting a 403 Error. I am not sure what is causing it. My best guess would be something with oauth1.0 i think on their website it says you need oauth 1.1.

Here is how I set it up but I am getting 403 error:

url = 'https://openapi.etsy.com/v2/listings'
payload = {'listing_id':'342434342', 'image': ("test1.jpg", open('C:\\Users\\abc\\test1.jpg'),'image/jpeg'),'type':'image/jpeg'}
result = etsy.put(url, params=payload)
print result 
like image 954
ben olsen Avatar asked Jan 30 '23 02:01

ben olsen


1 Answers

Comment: ... at this point I am lost I have no idea where to put the pin# that etsy gave me


etsy oauth#reference
The token credentials you receive for a account do not expire, and can be used over and over again to make authenticated API requests. You should keep the token secret in a secure location and never send it as a plaintext parameter (it's only used for signing your requests, and never needs to be sent in an API request on its own.) You will not need to step through the OAuth authorization again, unless you decides to revoke access, or unless you add features that require additional permission scopes.


Note: Didn't find a equivalent Replacement for PHP OAUTH_AUTH_TYPE_URI.
OAuth1Session Defaults to signature_type=u'AUTH_HEADER', so this could be wrong.
If this fails, you could try:

from oauthlib.oauth1 import SIGNATURE_TYPE_QUERY, SIGNATURE_TYPE_BODY
OAuth1Session(..., signature_type=SIGNATURE_TYPE_QUERY)

Create etsy OAuth1Session to reuse for Requests:

etsy = OAuth1Session(client_key,
                     client_secret=client_secret,
                     resource_owner_key=resource_owner_key,
                     resource_owner_secret=resource_owner_secret)

etsy Making an Authorized Request to the API:

response = etsy.get("https://openapi.etsy.com/v2/users/__SELF__")
user_data = json.loads(response.body_as_unicode())

etsy Checking Permission Scopes After Authentication:

response = etsy.get("https://openapi.etsy.com/v2/oauth/scopes")
meta = json.loads(response.body_as_unicode())

etsy Creates a new Listing

url = 'https://openapi.etsy.com/v2/listings'
payload = {'quantity': '1', 'title':...}
result = etsy.post(url, params=payload)

Comment: for api key do I need to import oauth2

According to Reference, Yes.

For write access and for accessing private user data, an OAuth access token is required. Your application key is required to start the OAuth authentication process.

Requires OAuth  Y

Also your url should end with

URI /listings

url = 'https://openapi.etsy.com/v2/listings'

Your url should only up to the Question mark, for example:

url = 'https://openapi.etsy.com/v2/listings/active'
payload = {'api_key':YOUR KEY HERE, 'quantity': '1', ...
rrr = requests.post(url, params=payload)

Requests Quickstart: Passing Parameters In URLs
You often want to send some sort of data in the URL's query string.
If you were constructing the URL by hand, this data would be given as key/value pairs in the URL after a question mark, e.g. \http://bin.org/get?key=val.
Requests allows you to provide these arguments as a dictionary of strings, using the params keyword argument.

like image 123
stovfl Avatar answered Feb 01 '23 17:02

stovfl