Reputation: 197
The title pretty much sums it up, I am making a request to the google translate api like this:
payload = {"key":translate_api_key, "q":str(sentence)}
try:
api_response = requests.get("https://www.googleapis.com/language/translate /v2/detect", params=payload)
except Exception, e:
print e
this works fine normally (by which i mean just running it as a script on my desktop) but on the google app engine server I am getting this response:
{u'error': {u'message': u'SSL is required to perform this operation.', u'code': 403, u'errors': [{u'message': u'SSL is required to perform this operation.', u'domain': u'global', u'reason': u'sslRequired'}]}}
How can I fix this?
EDIT: Seems request's https doesn't play nicely with GAE. Using urlfetch and urllib seems to fix this.
payload = dict(key=translate_api_key, q=sentence)
payload = urllib.urlencode(payload)
url = "https://www.googleapis.com/language/translate/v2/detect?"
api_response = urlfetch.fetch(url+payload)
Upvotes: 0
Views: 824
Reputation: 147
GCP does not support the use of Requests library out of the box. So we will have to make some tweeks to make it work. In order to deploy an application on the Google App Engine, we need to make a main.py(where main python flask app resides) and app.yaml(configuration file needed to run it in GCP). Here is a sample code for the app.yaml file for python 2.7 environment
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
redirect_http_response_code: 301
script: main.app
libraries:
- name: flask
version: 0.12
Now we need to configure the requests library to use URLfetch internally. To use requests, we'll need to install both requests and requests-toolbelt using the vendoring instructions. (https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27#installing_a_library). Basically we need to install our custom libraries.
Edit the appengine_config.py file and provide your library directory to the vendor.add() method. Sample appengine_config.py file
from google.appengine.ext import vendor
vendor.add('lib/requests') vendor.add('lib/requests_toolbelt')
Once installed, use the requests_toolbelt.adapters.appengine module to configure requests to use URLFetch. Copy the below code to the start of your main.py file
import requests from requests_toolbelt.adapters import appengine appengine.monkeypatch(validate_certificate=True)
(https://cloud.google.com/appengine/docs/standard/python/issue-requests)
Now we can easily use the requests library to make get/post requests. Test your app:
dev_appserver.py --port=<port number> app.yaml
Upvotes: 0
Reputation: 725
You can try in the app.yaml
handlers:
- url: /youraccount/.*
script: accounts.py
login: required
secure: always
The secure:always
makes your site ssl. I'm not sure this is that actual problem though.
Upvotes: 0
Reputation: 3714
I have no experience of using the requests library, but it may be that it is not fully implemented on App Engine.
The preferred method on App Engine is to use urlfetch
from google.appengine.api import urlfetch
url = "https://www.googleapis.com/language/translate/v2/detect"
payload = {"key":translate_api_key, "q":str(sentence)}
result = urlfetch.fetch(url=url, payload=payload)
if result.status_code == 200:
api_response = result.content
Upvotes: 1