diff --git a/examples/readme.md b/examples/readme.md index 718784d6..272c9806 100644 --- a/examples/readme.md +++ b/examples/readme.md @@ -27,7 +27,7 @@ Setup - Node.js Setup - Python -------------- -1. `cd` into the `examples/` directory and run `pip install watson_developer_cloud flask` (or `easy_install...`) to install python dependencies +1. `cd` into the `examples/` directory and run `pip install watson_developer_cloud flask python-dotenv pyopenssl` (or `easy_install...`) to install python dependencies. 2. run `bower install` to install client-side dependencies 3. edit `server.py` to include your service credentials (or create a `.env` file) 4. run `python server.py` diff --git a/examples/server.py b/examples/server.py index 69392a9b..1615e312 100644 --- a/examples/server.py +++ b/examples/server.py @@ -1,6 +1,7 @@ import os, json from flask import Flask from watson_developer_cloud import AuthorizationV1 as Authorization +from watson_developer_cloud import IAMTokenManager from watson_developer_cloud import SpeechToTextV1 as SpeechToText from watson_developer_cloud import TextToSpeechV1 as TextToSpeech from dotenv import load_dotenv @@ -9,21 +10,29 @@ load_dotenv('.env') # Text to Speech -TTS_USERNAME = os.environ.get('TTS_USERNAME'); # '' -TTS_PASSWORD = os.environ.get('TTS_PASSWORD'); # '' +TTS_PASSWORD = os.environ.get('TEXT_TO_SPEECH_PASSWORD') # '' # Speech to Text -STT_USERNAME = os.environ.get('STT_USERNAME'); # '' -STT_PASSWORD = os.environ.get('STT_PASSWORD'); # '' +STT_USERNAME = os.environ.get('SPEECH_TO_TEXT_USERNAME') # '' +STT_PASSWORD = os.environ.get('SPEECH_TO_TEXT_PASSWORD') # '' +STT_APIKEY = os.environ.get('SPEECH_TO_TEXT_IAM_APIKEY') # '' +STT_URL = os.environ.get('SPEECH_TO_TEXT_URL') # '' # on bluemix, automatically pull credentials from environment if 'VCAP_SERVICES' in os.environ: stt = json.loads(os.environ['VCAP_SERVICES'])['speech_to_text'][0] STT_USERNAME = stt["credentials"]["username"] STT_PASSWORD = stt["credentials"]["password"] + STT_APIKEY = stt["credentials"]["apikey"] + STT_URL = stt["credentials"]["url"] if stt["credentials"]["url"] else SpeechToText.default_url tts = json.loads(os.environ['VCAP_SERVICES'])['text_to_speech'][0] TTS_USERNAME = tts["credentials"]["username"] TTS_PASSWORD = tts["credentials"]["password"] + TTS_APIKEY = tts["credentials"]["apikey"] + TTS_URL = tts["credentials"]["url"] if tts["credentials"]["url"] else TextToSpeech.default_url app = Flask(__name__, static_url_path='') @@ -33,13 +42,24 @@ def root(): @app.route('/api/speech-to-text/token') def getSttToken(): - print(STT_USERNAME) - authorization = Authorization(username=STT_USERNAME, password=STT_PASSWORD) - return authorization.get_token(url=SpeechToText.default_url) + if (STT_APIKEY): + iamTokenManager = IAMTokenManager(iam_apikey=STT_APIKEY) + token = iamTokenManager.get_token() + else: + authorization = Authorization(username=STT_USERNAME, password=STT_PASSWORD) + token = authorization.get_token(url=STT_URL) + return token @app.route('/api/text-to-speech/token') def getTtsToken(): - authorization = Authorization(username=TTS_USERNAME, password=TTS_PASSWORD) - return authorization.get_token(url=TextToSpeech.default_url) + if (TTS_APIKEY): + iamTokenManager = IAMTokenManager(iam_apikey=TTS_APIKEY) + token = iamTokenManager.get_token() + else: + authorization = Authorization(username=TTS_USERNAME, password=TTS_PASSWORD) + token = authorization.get_token(url=TTS_URL) + return token -app.run(debug=True) +# NOTE: ssl_context='adhoc' fixes response encoding (Flask 400 BAD_REQUEST) errors over SSL +if __name__ == '__main__': + app.run(ssl_context='adhoc', debug=True) \ No newline at end of file