Python 函数与POST一起工作,但与GET请求一起失败
我正在构建一个api,它可以与各种sms提供商交互,并允许用户发送和接收文本。我正在使用Python 函数与POST一起工作,但与GET请求一起失败,python,python-3.x,flask,twilio,Python,Python 3.x,Flask,Twilio,我正在构建一个api,它可以与各种sms提供商交互,并允许用户发送和接收文本。我正在使用Flask和python3.4。我的问题是,当我尝试验证一个帐户时,如果我使用POST请求发送数据,它工作正常。但是如果我使用GET请求,我会收到一个错误,告诉我进行身份验证。 以下是我的功能: @coma_inbound.route("/twilio/verify/account",methods=["GET","POST"]) def verifyAccount(): #pdb.set_trace
Flask
和python3.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 -vvv http://127.0.0.1:5000/twilio/verify/account?account=ACf7e45c1e1547c066005efe64f933aa45&credentials=6d76c0bab837a10e6763a61aabacf7f2&targetAcct=ACf7e45c1e1547c066005efe64f933aa45
从curl
输出该值(预期结果为活动):
将其输出到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
我的错误是:
* 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.
请看一下旋度参数。 在第二种情况下,不使用双引号运行它-因此shell解析字符串。 但是“&”有一个特殊的含义——在后台运行程序。 因此,在本例中,您运行一个脚本 curl-vvv 在后台模式下 还有一个 凭证=6D76C0BAB837A10E6763A61ABACF7F2 在前景中 因此,您的python脚本无法获取凭据并失败: GET/twilio/verify/account?account=ACF7E45C1547C066005EFE64F933AA45 HTTP/1.1 单引号或双引号会有所帮助 你的,
Eugene您使用的输入是什么?post和get参数?@JaseRieger我将所有这些都添加到了问题中。错误发生在哪个try/except块中?这真的很奇怪。我能想到的唯一一件事是,是否有一些不必要的空格进入了您的帐户SID或身份验证令牌,从而导致身份验证失败?不管怎样,我猜我错了。如果我在curl中双引用url,现在就可以了。对不起,浪费了大家的时间。谢谢大家。
^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.