python - 如何使用 Python Etsy HTTP API 方法添加新项目?

标签 python http

我正在尝试使用 Etsy API 在我的商店中添加新列表。在文件部分它说(下面的部分如何做)。首先,我以前从未使用过 HTTP 方法,所以我不确定如何设置代码以便它添加新项目。

(链接到 Etsy API 页面 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.

我知道的代码是这样的

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 

如何通过 Python HTTP 方法在 Etsy 上添加待售商品?

更新

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')

关于如何使这项工作有任何想法吗?关于 Etsy API 的信息非常少,大部分内容都是用 PHP 编写的,我不知道如何工作。

图片上传API

一切看起来都和上面一样,这次我只是更改了有效负载,但我收到了 403 错误。我不确定是什么原因造成的。我最好的猜测是 oauth1.0,我认为在他们的网站上它说你需要 oauth 1.1。

这是我的设置方式,但出现 403 错误:

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 

最佳答案

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)

创建 etsy OAuth1Session 以重新用于请求:

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

etsy 向 API 发出授权请求:

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

etsy 在身份验证后检查权限范围:

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

etsy 创建新列表

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

根据引用,是的。

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

此外,您的url 应以

结尾
URI /listings

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

您的 url 最多只能到问号,例如:

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.

关于python - 如何使用 Python Etsy HTTP API 方法添加新项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44855616/

相关文章:

python - 为什么 Python 正则表达式不能处理格式化的 HTML 字符串?

javascript - 无法在 JSON-SERVER 中获取对象关键数据

asp.net-mvc - 如何让 Controller 在 MVC asp.net 中允许 http 和 https 流量?

android - HttpContext 不持有 cookie

python - 从 CLI 输入获取文件

python - Python 中池的使用

rest - 同时使用 HTTP 和 WebSockets 时的 API 命名约定

http - 在 HTTP 响应 header 之后重试 - 它会影响什么吗?

python - 在 python 中通过进程名称获取 PID 的跨平台方法

python - 在 Python 中使用大字典优化性能