【发布时间】:2016-03-03 21:15:19
【问题描述】:
我正在构建一个与各种短信提供商交互并允许用户发送和接收文本的 API。我正在使用Flask 和Python 3.4。我的问题是,当我尝试验证帐户时,如果我使用POST 请求发送数据,它工作正常。但是如果我使用GET 请求,我会收到一个错误,告诉我进行身份验证。
这是我的功能:
@coma_inbound.route("/twilio/verify/account",methods=["GET","POST"])
def verifyAccount():
#pdb.set_trace()
account_sid = request.values.get("account")
auth_token = request.values.get("credentials")
targetAcct = request.values.get("targetAcct")
print(account_sid, auth_token, targetAcct)
try:
client = TwilioRestClient(account_sid, auth_token)
print(client.auth)
print("authenticated")
except TwilioRestException as e:
print(e)
print("Updating Status 1")
status = str(e.msg)[:250]
print(status)
return status
try:
print(account_sid, auth_token, targetAcct)
print(client.auth)
account = client.accounts.get(targetAcct)
status = account.status
except TwilioRestException as e:
print(e)
print("Updating Status 2")
status = str(e.msg)[:250]
print(status)
return status
print(status)
return status
我的POST 请求是这样的:
curl -vvv --data "account=ACf7e45c1e1547c066005efe64f933aa45&credentials=6d76c0bab837a10e6763a61aabacf7f2&targetAcct=ACf7e45c1e1547c066005efe64f933aa45" http://127.0.0.1:5000/twilio/verify/account
从curl 输出这个(活动是预期的结果):
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST /twilio/verify/account HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:5000
> Accept: */*
> Content-Length: 133
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 133 out of 133 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/html; charset=utf-8
< Content-Length: 6
< Server: Werkzeug/0.11.3 Python/3.4.3
< Date: Fri, 04 Mar 2016 14:43:28 GMT
<
* Closing connection 0
active
我的GET 请求是这样的:
curl -vvv http://127.0.0.1:5000/twilio/verify/account?account=ACf7e45c1e1547c066005efe64f933aa45&credentials=6d76c0bab837a10e6763a61aabacf7f2&targetAcct=ACf7e45c1e1547c066005efe64f933aa45
哪个输出到curl:
[1] 6875
[2] 6876
anon@anon-VirtualBox:~/Coma$ * Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> GET /twilio/verify/account?account=ACf7e45c1e1547c066005efe64f933aa45 HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:5000
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 500 INTERNAL SERVER ERROR
< Content-Type: text/html
< Content-Length: 291
< Server: Werkzeug/0.11.3 Python/3.4.3
< Date: Fri, 04 Mar 2016 15:01:51 GMT
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
* Closing connection 0
此时它挂起,直到我 ctrl+c 然后输出:
^C
[1]- Done curl -vvv http://127.0.0.1:5000/twilio/verify/account?account=ACf7e45c1e1547c066005efe64f933aa45
[2]+ Done credentials=6d76c0bab837a10e6763a61aabacf7f2
我的错误是:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 159-528-786
Received: ACf7e45c1e1547c066005efe64f933aa45 None None
127.0.0.1 - - [04/Mar/2016 09:53:28] "GET /twilio/verify/account?account=ACf7e45c1e1547c066005efe64f933aa45 HTTP/1.1" 500 -
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/anon/Coma/Inbound/FlaskComa/views.py", line 68, in verifyAccount
client = TwilioRestClient(account_sid, auth_token)
File "/usr/local/lib/python3.4/dist-packages/twilio/rest/client.py", line 49, in __init__
timeout)
File "/usr/local/lib/python3.4/dist-packages/twilio/rest/base.py", line 57, in __init__
""")
twilio.exceptions.TwilioException:
Twilio could not find your account credentials. Pass them into the
TwilioRestClient constructor like this:
client = TwilioRestClient(account='AC38135355602040856210245275870',
token='2flnf5tdp7so0lmfdu3d')
Or, add your credentials to your shell environment. From the terminal, run
echo "export TWILIO_ACCOUNT_SID=AC3813535560204085626521" >> ~/.bashrc
echo "export TWILIO_AUTH_TOKEN=2flnf5tdp7so0lmfdu3d7wod" >> ~/.bashrc
and be sure to replace the values for the Account SID and auth token with the
values from your Twilio Account at https://www.twilio.com/user/account.
【问题讨论】:
-
您使用的输入是什么? post 和 get 参数?
-
@JaseRieger 我将所有这些都添加到了问题中。
-
错误发生在哪个 try/except 块中?
-
这真的很奇怪。我唯一能想到的是是否有一些不必要的空格进入您的帐户 SID 或 Auth Token 导致身份验证失败?
-
没关系,我猜我错了。如果我在 curl 中双引号 url,它现在可以正常工作了。很抱歉浪费了大家的时间。谢谢大家。
标签: python python-3.x flask twilio