gpt4 book ai didi

python - 函数适用于 POST 但在 GET 请求时失败

转载 作者:行者123 更新时间:2023-11-28 16:27:03 25 4
gpt4 key购买 nike

我正在构建一个与各种短信提供商交互并允许用户发送和接收文本的 API。我正在使用 FlaskPython 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 输出(active 是预期结果):

* 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.

最佳答案

请查看 curl 参数。在第二种情况下,你运行它时不带双引号——所以 shell 会解析字符串。但是“&”有一个特殊的含义——在后台运行程序。所以在这种情况下你运行一个脚本

curl -vvv http://127.0.0.1:5000/twilio/verify/account?account=ACf7e45c1e1547c066005efe64f933aa45

在后台模式还有一个

credentials=6d76c0bab837a10e6763a61aabacf7f2

在前景中。

因此,您的 python 脚本未获得凭证并失败:

GET /twilio/verify/account?account=ACf7e45c1e1547c066005efe64f933aa45 HTTP/1.1

单引号或双引号会有帮助。

您的,尤金

关于python - 函数适用于 POST 但在 GET 请求时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35783169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com