I had to do this recently, and since I had to translate long lines, I could not use the URL parameters, but rather used a data payload. Thought it was the best place to share this solution.
The trick is basically to use Python's excellent Requests post, but since Google requires a GET request, use the "X-HTTP-Method-Override" header to override the request method for GET.
(explicitly using request.get loads the data payload)
code:
import requests def translate_es_to_en(text): url = "https://www.googleapis.com/language/translate/v2" data = { 'key': '<your-server-google-api-key>' 'source': 'es', 'target': 'en', 'q': text } headers = {'X-HTTP-Method-Override': 'GET'} response = requests.post(url, data=data, headers=headers) return response.json()
Hope this helps anyone who is still doing this.
source share