Authentication¶
API users must have a registered Gengo account to acquire a pair of keys - a public key and private key. The api_key
is used to identify a user, and the private_key
is used to authenticate each API call. The combination effectively functions as a user name and password. Therefore you should keep the private_key private.
The keys are created and retrieved from the account settings page.
Every restricted (non public) REST call will need to be authenticated by Gengo as below.
Signing Calls¶
All authenticated calls must be signed:
Get the current Unix epoch time as an integer
Insert the time as the value to a ‘ts’ key in your argument list
Calculate the SHA1 hash of the timestamp against your private key
Append the value of this hash to the argument list as a parameter named api_sig
Example API authenticated call¶
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4from hashlib import sha1
5import hmac
6import json
7import requests
8import time
9
10if __name__ == '__main__':
11 PUBLIC_KEY = 'your public key'
12 PRIVATE_KEY = 'your private key'
13
14 # submit a comment for job ID 20
15 JOB_ID = '20'
16 COMMENT = 'Please use British spelling'
17 URL = "http://api.gengo.com/v2/translate/job/{0}/comment".format(JOB_ID)
18 header = {"Accept": "application/json"}
19
20 data = {
21 # 'data' must be flattened. We use json.dumps()
22 "data": json.dumps({"body": COMMENT}, separators=(',', ':')),
23 "api_key": PUBLIC_KEY,
24 "ts": str(int(time.time()))
25 }
26
27 # use your private_key to create an hmac
28 data["api_sig"] = hmac.new(
29 PRIVATE_KEY,
30 data["ts"],
31 sha1
32 ).hexdigest()
33
34 post_comment = requests.post(URL, data=data, headers=header)
35 res_json = json.loads(post_comment.text)
36
37 if not res_json["opstat"] != "ok":
38 msg = "API error occured.\nerror msg: {0}".format(
39 res_json["err"]
40 )
41 raise AssertionError(msg)
42 else:
43 print(res_json)