Как правильно создать экземпляр объекта WePay в Python?

Я пытаюсь получить токен доступа для выполнения действий от имени пользователей, и я читаю их документацию и следую этому ( https://www.wepay.com/developer/create_accounts/oauth2) учебнику на их сайте.

Сначала я пытался напрямую использовать запрос GET с параметрами, закодированными в URL, однако я продолжал получать 401, а затем попробовал их учебник, теперь эта ошибка в консоли:

in add_merchant_token
    wepay = WePay(production, None)
TypeError: __init__() takes exactly 2 arguments (3 given)

Я могу получить "код" для получения токена, но я просто не могу понять, как получить настоящий токен.

Код:

code = request.args.get('code')
redirect_uri = "http://localhost:5000/add_merchant_token"

# application settings
client_id = XXXXXX
client_secret = 'XXXXXX'
production = False

wepay = WePay(production, None)

# create an account for a user
response = wepay.get_token(redirect_uri, client_id, client_secret, code)

# display the response
print response

return response

Полная трассировка стека:

Traceback (most recent call last):
  File "/Users/Marcus/Developer/GitHub/subbitt/venv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/Marcus/Developer/GitHub/subbitt/venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Users/Marcus/Developer/GitHub/subbitt/venv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/Marcus/Developer/GitHub/subbitt/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/Marcus/Developer/GitHub/subbitt/venv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/Marcus/Developer/GitHub/subbitt/venv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/Marcus/Developer/GitHub/subbitt/venv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/Marcus/Developer/GitHub/subbitt/venv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/Marcus/Developer/GitHub/subbitt/app/views.py", line 622, in add_merchant_token
    wepay = WePay(production, None)
TypeError: __init__() takes exactly 2 arguments (3 given)

1 ответ

Вот пример того, как это сделать с помощью стороннего Python WePay SDK

from wepay import WePay
from wepay.exceptions import WePayHTTPError, WePayConnectionError

# application settings
CLIENT_ID = XXXXXX
CLIENT_SECRET = 'XXXXXX'
PRODUCTION = False
DEFAULT_SCOPE = "manage_accounts,collect_payments,view_user," \
                "preapprove_payments,manage_subscriptions,send_money"
wepay = WePay(production=PRODUCTION)

redirect_uri = "http://localhost:5000/add_merchant_token"
url = wepay.oauth2.authorize(CLIENT_ID, redirect_uri, DEFAULT_SCOPE, 
                             user_name="John Doe", user_email="john@example.com")
# send user to that `url` and then get code from the redirected url 
# query string. Note if 'error' is in a query, handle it appropriately
...
code = request.args.get('code')

# create an account for a user
try:
    response = wepay.oauth2.token(CLIENT_ID, redirect_uri, CLIENT_SECRET, code)
    access_token = response['access_token']
    # display the response
    print response
except WePayHTTPError as exc:
    if exc.error_code == 1012:
        print "code has expired"
    print exc
except WePayConnectionError as exc:
    print exc
    # problem with connecting to wepay, retry api call a bit later
Другие вопросы по тегам