如何使用 Etsy API 凭证在 python 中获取和发布数据 [英] How to use Etsy API credentials to GET and POST data in python

查看:55
本文介绍了如何使用 Etsy API 凭证在 python 中获取和发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前提

我正在将我的产品配置数据库与我的 POS 和各种电子商务网站链接起来.拼图中的最后一个链接是连接到 Etsy.他们的指南(https://www.etsy.com/developers/documentation/reference/listing) 特定于 PHP,但我正在使用 python 3.7.

状态

由于这个问题,我能够成功获取所需的凭据:如何使用 Python Etsy HTTP API 方法添加新项目? 我现在拥有以下凭据:

  • oauth_token
  • oauth_token_secret
  • oauth_consumer_key
  • 验证者
  • key_string
  • shared_secret

我的尝试

我尝试同时使用 python_etsyetsy_python,但两者似乎都已被放弃且不完整,每次都会产生异常.我尽我最大的能力尝试解决出现的每个问题,但我真的很迷茫.

正如对 上面链接的问题,我尝试使用 OAuth1Session 对象.

<预><代码>>>>etsy = OAuth1Session(... key_string,... client_secret=shared_secret,... resource_owner_key=oauth_token,... resource_owner_secret=oauth_token_secret)>>>esty.get('https://openapi.etsy.com/v2/users/__SELF__')>>>response.status_code403>>>响应文本'oauth_problem=token_rejected'

我只是不知道如何正确使用给定的凭据才能成功调用 Etsy API.

即使在定义 OAuth1Session 对象 etsy 时添加 signature_type=SIGNATURE_TYPE_QUERY 也会产生相同的结果.

我将此作为一个新问题发布,因为 上面链接的问题 特定于使用 API 添加新项目,而我正在寻找有关解释和转换 PHP 示例以在 Python 中使用的一般说明.这是我对其他 API 所做的并取得了良好的成功,但这个特定的 API 引发了循环.

verifier 在哪里发挥作用?

--更新--

代替 oauth 会话,我使用了 requests.get 如下:

<预><代码>>>>进口请求>>>导入 uuid>>>从日期时间导入日期时间>>>url = 'https://openapi.etsy.com/v2/oauth/access_token'>>>参数 = {...'oauth_consumer_key':{oauth_consumer_key},... 'oauth_signature': {verifier},...'oauth_signature_method':{方法},...'oauth_nonce': str(uuid(uuid1()),... 'oauth_timestamp': datetime.timestamp(datetime.utcnow())...'oauth_token':{oauth_token}...}>>>token_response = requests.get(url,params=params)>>>打印(rrr.status_code,rrr.text)400 oauth_problem=verifier_invalid

我使用 oauth_signature_method 的以下每个值尝试了上述操作:

  • 'PLAINTEXT'
  • 'RSA-SHA1'
  • 'HMAC-SHA1'

每个值产生相同的结果.我还尝试将 verifier 放在 oauth_nonce 参数

解决方案

使用 etsy-python2 模块来获取和处理凭据.

pip install etsy2

from etsy2 import Etsy从 etsy2.oauth 导入 EtsyOAuthHelper、EtsyOAuthClient将 urllib.parse 导入为 urlparse从 urllib.parse 导入 parse_qsapi_key = <etsy keystring>shared_secret = #define 权限范围permission_scopes = ['listings_r', 'listings_w']login_url, temp_oauth_token_secret = \EtsyOAuthHelper.get_request_url_and_token_secret(api_key, shared_secret,permission_scopes)查询 = urlparse.urlparse(login_url).querytemp_oauth_token = parse_qs(query)['oauth_token'][0]打印(登录网址)#按照url获取验证器.oauth_token, oauth_token_secret = \EtsyOAuthHelper.get_oauth_token_via_verifier(api_key, shared_secret, temp_oauth_token, temp_oauth_token_secret, input('Verifier:'))etsy_oauth = EtsyOAuthClient(client_key = api_key,client_secret = shared_secret,resource_owner_key = oauth_token,resource_owner_secret = oauth_token_secret)etsy = Etsy(etsy_oauth_client = etsy_oauth)

etsy2 模块是最新的并且正在维护,所以现在它是一个不错的选择.这些方法是在运行时从 Etsy 的 API 服务器动态定义的.定义您的 Etsy 对象后,您可以使用以下方法列出可用的方法:

dir(etsy)

Premise

I am working at linking my product configuration database with my POS and various eCommerce sites. The last link in the puzzle is connecting to Etsy. Their guide (https://www.etsy.com/developers/documentation/reference/listing) is specific to PHP, but I am working with python 3.7.

Status

I have been able to successfully acquire the credentials required thanks to this question: How to add a new item using Python Etsy HTTP API methods? I now have the following credentials:

  • oauth_token
  • oauth_token_secret
  • oauth_consumer_key
  • verifier
  • key_string
  • shared_secret

What I've Tried

I tried using both python_etsy and etsy_python, but both seem to have been abandoned and are incomplete, producing exceptions at every turn. I tried to my best ability to try to fix each issue as it arises, but am just so lost.

As suggested in the answer to the the question linked above, I tried using an OAuth1Session object.

>>> etsy = OAuth1Session(
...    key_string,
...    client_secret=shared_secret,
...    resource_owner_key=oauth_token,
...    resource_owner_secret=oauth_token_secret)
>>> esty.get('https://openapi.etsy.com/v2/users/__SELF__')
>>> response.status_code
403
>>> response.text
'oauth_problem=token_rejected'

I just don't know how to properly use the given credentials in order to make successful API calls to the Etsy API.

Even adding signature_type=SIGNATURE_TYPE_QUERY when defining the OAuth1Session object etsy produces the same result.

I am posting this as a new question, since the question linked above is specific to adding a new item using the API, while I am looking for general instruction on interpreting and converting the PHP examples for use in Python. This is what I have done with other APIs with good success, but this particular one has thrown be for a loop.

Where does the verifier come in to play?

--Update--

In place of an oauth session, I used requests.get as follows:

>>> import requests
>>> import uuid
>>> from datetime import datetime
>>> url = 'https://openapi.etsy.com/v2/oauth/access_token'
>>> params = {
...  'oauth_consumer_key': {oauth_consumer_key},
...  'oauth_signature': {verifier},
...  'oauth_signature_method': {method},
...  'oauth_nonce': str(uuid(uuid1()),
...  'oauth_timestamp': datetime.timestamp(datetime.utcnow())
...  'oauth_token': {oauth_token}
...}
>>> token_response = requests.get(url,params=params)
>>> print(rrr.status_code,rrr.text)
400 oauth_problem=verifier_invalid

I tried the above using each of the following value for oauth_signature_method:

  • 'PLAINTEXT'
  • 'RSA-SHA1'
  • 'HMAC-SHA1'

Each value produced the same result. I also tried placing the verifier in the oauth_nonce paramater

解决方案

Use etsy-python2 module to acquire and process the credentials.

pip install etsy2

from etsy2 import Etsy
from etsy2.oauth import EtsyOAuthHelper, EtsyOAuthClient
import urllib.parse as urlparse
from urllib.parse import parse_qs

api_key = <etsy keystring>
shared_secret = <etsy shared secret>

#define permission scopes
permission_scopes = ['listings_r', 'listings_w']

login_url, temp_oauth_token_secret = \
    EtsyOAuthHelper.get_request_url_and_token_secret(api_key, shared_secret, permission_scopes)

query = urlparse.urlparse(login_url).query
temp_oauth_token = parse_qs(query)['oauth_token'][0]

print(login_url)
#follow the url to acquire the verifier.

oauth_token, oauth_token_secret = \
    EtsyOAuthHelper.get_oauth_token_via_verifier(api_key, shared_secret, temp_oauth_token, temp_oauth_token_secret, input('Verifier: '))

etsy_oauth = EtsyOAuthClient(client_key = api_key,
                             client_secret = shared_secret,
                             resource_owner_key = oauth_token,
                             resource_owner_secret = oauth_token_secret)

etsy = Etsy(etsy_oauth_client = etsy_oauth)

The etsy2 module is current and being maintained, so right now, it is a good option. The methods are dynamicly defined at runtime from Etsy's API server. After defining your Etsy object, you can list the available methods using:

dir(etsy)

这篇关于如何使用 Etsy API 凭证在 python 中获取和发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆