如何在Python中正确实例化WePay对象?

时间:2015-07-22 02:38:04

标签: flask payment-processing

我正在尝试获取访问令牌以代表用户执行操作,并且我一直在阅读他们的文档并在其网站上执行此(https://www.wepay.com/developer/create_accounts/oauth2)教程。

首先,我尝试直接使用带有URL中编码参数的GET请求,但是我一直得到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 个答案:

答案 0 :(得分:0)

以下是使用第三方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