函数与POST一起使用,但在GET请求

时间:2016-03-03 21:15:19

标签: python python-3.x flask twilio

我正在建立一个与各种短信提供商接口的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输出此信息(活动是预期结果):

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

1 个答案:

答案 0 :(得分:3)

请看一下卷曲参数。 在第二种情况下,你运行它没有双引号 - 所以shell解析字符串。 但“&amp;”有一个特殊的含义 - 在后台运行程序。 所以在这种情况下你运行一个脚本

  

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

在后台模式下 还有一个

  

凭证= 6d76c0bab837a10e6763a61aabacf7f2

在前景中。

因此,您的python脚本无法获取凭据并失败:

  

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

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

此致, 尤金

相关问题