Python Flask Request不能使用多个参数

时间:2017-07-11 06:04:00

标签: python heroku flask request twilio-api

我尝试使用python API但是如果我尝试使用多个参数

则它无法正常工作

无效

from flask import Flask, request

    @app.route('/test', methods=['GET', 'POST'])
    def test():

        req_json = request.get_json(force=True)
        UserName = req_json['username']
        UserPassword = req_json['password']
        return str(UserName)

工作

from flask import Flask, request

    @app.route('/test', methods=['GET', 'POST'])
    def test():

        req_json = request.get_json(force=True)
        UserName = req_json['username']
        return str(UserName)

错误

https://www.herokucdn.com/error-pages/application-error.html

日志

State changed from crashed to starting
2017-07-11T06:44:13.760404+00:00 heroku[web.1]: Starting process with command `python server.py`
2017-07-11T06:44:16.078195+00:00 app[web.1]:   File "server.py", line 29
2017-07-11T06:44:16.078211+00:00 app[web.1]:     account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
2017-07-11T06:44:16.078211+00:00 app[web.1]:     ^
2017-07-11T06:44:16.078213+00:00 app[web.1]: IndentationError: unexpected indent
2017-07-11T06:44:16.179785+00:00 heroku[web.1]: Process exited with status 1
2017-07-11T06:44:16.192829+00:00 heroku[web.1]: State changed from starting to crashed

Server.py

    import os
    from flask import Flask, request
    from twilio.jwt.access_token import AccessToken, VoiceGrant
    from twilio.rest import Client
    import twilio.twiml

    ACCOUNT_SID = 'accountsid'
    API_KEY = 'apikey'
    API_KEY_SECRET = 'apikeysecret'
    PUSH_CREDENTIAL_SID = 'pushsid'
    APP_SID = 'appsid'


    app = Flask(__name__)

    @app.route('/test', methods=['GET', 'POST'])
    def test():

        req_json = request.get_json(force=True)
        UserName = req_json['username']
        Password = req_json['password']
        return str(UserName)

    @app.route('/accessToken')
    def token():

req_json = request.get_json(force=True)
        IDENTITY = req_json['identity']

            account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
                api_key = os.environ.get("API_KEY", API_KEY)
                    api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
                        push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID)
                            app_sid = os.environ.get("APP_SID", APP_SID)

                                grant = VoiceGrant(
                                                   push_credential_sid=push_credential_sid,
                                                   outgoing_application_sid=app_sid
                                                   )

                                    token = AccessToken(account_sid, api_key, api_key_secret, IDENTITY)
                                        token.add_grant(grant)

                                            return str(token)

    @app.route('/outgoing', methods=['GET', 'POST'])
    def outgoing():
        resp = twilio.twiml.Response()
            #resp.say("Congratulations! You have made your first oubound call! Good bye.")
            resp.say("Thanks for Calling! Please try again later.")
                return str(resp)

    @app.route('/incoming', methods=['GET', 'POST'])
    def incoming():
        resp = twilio.twiml.Response()
            #resp.say("Congratulations! You have received your first inbound call! Good bye.")
            resp.say("Thanks for Calling! Please try again later.")
                return str(resp)

    @app.route('/placeCall', methods=['GET', 'POST'])
    def placeCall():

        req_json = request.get_json(force=True)
            IDENTITY = req_json['identity']
                CALLER_ID = req_json['callerid']

                    account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
                        api_key = os.environ.get("API_KEY", API_KEY)
                            api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)

                                client = Client(api_key, api_key_secret, account_sid)
                                    call = client.calls.create(url=request.url_root + 'incoming', to='client:' + CALLER_ID, from_='client:' + IDENTITY)
                                        return str(call.sid)

    @app.route('/', methods=['GET', 'POST'])
    def welcome():
        resp = twilio.twiml.Response()
            resp.say("Welcome")
                return str(resp)

    if __name__ == "__main__":
        port = int(os.environ.get("PORT", 5000))
            app.run(host='0.0.0.0', port=port, debug=True)

3 个答案:

答案 0 :(得分:2)

老实说,我不能告诉你的缩进问题在哪里以及是否误解了python中的whitespacing如何工作或者在stackoverflow上发布代码块(我的猜测是两者的组合)。所以我把你的代码放在PyCharm中并正确缩进并将代码粘贴到我刚发现的这个好tool中,这样我就可以正确地提交它了。这应该有希望解决您的问题。只需复制并粘贴它,然后更改所有必要的值。

import os
from flask import Flask, request
from twilio.jwt.access_token import AccessToken, VoiceGrant
from twilio.rest import Client
import twilio.twiml

ACCOUNT_SID = 'accountsid'
API_KEY = 'apikey'
API_KEY_SECRET = 'apikeysecret'
PUSH_CREDENTIAL_SID = 'pushsid'
APP_SID = 'appsid'


app = Flask(__name__)

@app.route('/test', methods=['GET', 'POST'])
def test():
    req_json = request.get_json(force=True)
    UserName = req_json['username']
    Password = req_json['password']
    return str(UserName)

@app.route('/accessToken')
def token():
    req_json = request.get_json(force=True)
    IDENTITY = req_json['identity']

    account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
    api_key = os.environ.get("API_KEY", API_KEY)
    api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
    push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID)
    app_sid = os.environ.get("APP_SID", APP_SID)

    grant = VoiceGrant(
        push_credential_sid=push_credential_sid,
        outgoing_application_sid=app_sid
    )

    token = AccessToken(account_sid, api_key, api_key_secret, IDENTITY)
    token.add_grant(grant)

    return str(token)

@app.route('/outgoing', methods=['GET', 'POST'])
def outgoing():
    resp = twilio.twiml.Response()
    #resp.say("Congratulations! You have made your first oubound call! Good bye.")
    resp.say("Thanks for Calling! Please try again later.")
    return str(resp)

@app.route('/incoming', methods=['GET', 'POST'])
def incoming():
    resp = twilio.twiml.Response()
    #resp.say("Congratulations! You have received your first inbound call! Good bye.")
    resp.say("Thanks for Calling! Please try again later.")
    return str(resp)

@app.route('/placeCall', methods=['GET', 'POST'])
def placeCall():

    req_json = request.get_json(force=True)
    IDENTITY = req_json['identity']
    CALLER_ID = req_json['callerid']

    account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
    api_key = os.environ.get("API_KEY", API_KEY)
    api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)

    client = Client(api_key, api_key_secret, account_sid)
    call = client.calls.create(url=request.url_root + 'incoming', to='client:' + CALLER_ID, from_='client:' + IDENTITY)
    return str(call.sid)

@app.route('/', methods=['GET', 'POST'])
def welcome():
    resp = twilio.twiml.Response()
    resp.say("Welcome")
    return str(resp)

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(host='0.0.0.0', port=port, debug=True) 

答案 1 :(得分:1)

正如您在日志中看到的那样,应用程序因缩进错误而崩溃。 请在代码中检查account_sid变量的缩进。

答案 2 :(得分:0)

提示在您的日志中。

2017-07-11T06:44:16.078195+00:00 app[web.1]:   File "server.py", line 29
2017-07-11T06:44:16.078211+00:00 app[web.1]:     account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
2017-07-11T06:44:16.078211+00:00 app[web.1]:     ^
2017-07-11T06:44:16.078213+00:00 app[web.1]: IndentationError: unexpected indent

第29行的server.py中有错误的缩进。

req_json = request.get_json(force=True)
        IDENTITY = req_json['identity']

            account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
                api_key = os.environ.get("API_KEY", API_KEY)
                    api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
                        push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID)
                            app_sid = os.environ.get("APP_SID", APP_SID)

应该是这样的:

req_json = request.get_json(force=True)
IDENTITY = req_json['identity']
account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
api_key = os.environ.get("API_KEY", API_KEY)
api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID)
app_sid = os.environ.get("APP_SID", APP_SID)

看起来你还有许多其他严重缩进的线条。