2017-06-30 37 views
0

我試圖使用Etsy API(HTTP方法)在我的商店中添加一個新的列表。它在文檔部分說(下面的部分是怎麼做的)。首先,我從來沒有使用HTTP方法,所以我不知道如何設置代碼,以便它添加一個新的項目。 (鏈接到Etsy的API頁面https://www.etsy.com/developers/documentation/reference/listingpython etsy HTTP方法api幫助需要添加新項目

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 

誰能告訴我一個正確的方法,我可以上出售的Etsy通過蟒蛇添加項目HTTP方法。謝謝

更新:

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中,我不知道如何僅僅使用python。

圖片上傳API幫助。
一切看起來都像上面這樣,我只是改變了有效載荷,但我得到了一個403錯誤。我不確定是什麼原因造成的。我最好的猜測會是oauth1.0的東西,我認爲那裏的網站上說你需要oauth 1.1。這裏是我如何設置它,但我正在逐漸403error:

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 

回答

1

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

根據參考文獻,是。

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

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' 

您的網址應該只到問號,例如結束

+0

for api key我需要導入oauth2還是可以使用etsty爲我的帳戶提供的密鑰和密鑰。現在,我想在我的商店上發佈錯誤消息。 。我希望這個問題有道理,我一直試圖弄清楚這一整天......燒壞了。 –

+0

感謝您的更新。我有點卡在Ouath部分「https://www.etsy.com/developers/documentation/getting_started/oauth」我已經成功地從etsy完成了第1步(稱爲 - 授權請求)我得到一個鏈接,然後我批准了像pictue這樣的請求,它給了我一個引腳。在這一點上,我迷路了,我不知道在哪裏放置etsy給我的pin。我會更新我的頁面,讓你知道我的意思。 –

+0

完美無瑕的解釋你是一個救星。謝謝 –

2

Question: I am trying to upload a picture ... getting a 403 error

您的url Endpoint和​​是不正確的。

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

步驟做一個etsy請求(uploadListingImage):

  1. 閱讀Reference您的方法名稱

    Method Name uploadListingImage 
    HTTP Method POST 
    URI   /listings/:listing_id/images 
    Parameters Name   Required Default Type 
         listing_id   Y     int 
         listing_image_id N     int 
         image    N     imagefile 
         ... 
    Requires OAuth Y 
    

    Respect Supported Sizes Working with Images

    Note: For me, it's unclear what the image Parameter is for.
    And as it's NOT required makes no sense.
    I assume its a Placeholder for the Parameter at Point 4 below: {'image':...

  2. 構建URI

    uri = 'https://openapi.etsy.com/v2/listings/342434342/images' 
    
  3. 根據上述參考

    我推薦使用listing_image_id創建PARAMS快譯通,因爲這似乎刪除一個圖像之後的唯一途徑。

    params = {'listing_id':'342434342', 'listing_image_id': 1} 
    
  4. 創建多部分編碼的文件快譯通

    Image uploads can be performed using a POST request with the Content-Type: multipart/form-dataheader , following RFC1867
    # PHP example from Reference :
    # $params = array('@image' => '@'.$source_file.';type='.$mimetype);

    files = {'image': ("test1.jpg", open('C:\\Users\\abc\\test1.jpg', 'rb'), 'image/jpeg')} 
    
  5. 的要求去做,根據Reference,你必須使用OAuthPOST

    result = etsy.post(uri, params=params, files=files) 
    

請評論這是否適合你或爲什麼不。

+1

它的工作! :)我不知道如何謝謝你。你的幫助非常感謝,並且很容易休閒/理解。再次感謝你。 –