M
M
mitra_mc2018-04-02 17:24:58
API
mitra_mc, 2018-04-02 17:24:58

How to make a request in ABBYY Lingvo API?

I want to work with ABBYY Lingvo API . But I do not understand how to make a request from the URL. I got an access key and I don't know how to work with the API further.
Here, in the documentation they give for example:
GET api/v1/WordForms?text={text}〈={lang}
Describes what is needed in the request, and gives an example:
?text=колено&lang=1049
How should it look in full?

https://developers.lingvolive.com/api/v1/WordForms?text=колено&lang=1049
- does not work

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mark-Centurion, 2019-03-18
@Mark-Centurion

I understand that a year has passed since the question was asked, but today I spent several hours looking for an answer and I think it's worth sharing because ABBYY's APIs are good, but the documentation for them is disgusting!
The most obvious solution did not work for me either, but I realized that you need to not only send a request, but you need to send the correct headers with the request.
And so here is my working PHP example:

//Подготовка заголовков к авторизации
$header = array();
$header[] = 'Content-length: 0';
$header[] = 'Content-type: application/json';
$header[] = 'Authorization: Basic {Ваш ключ для доступа к API}';

//Авторизация в ABBYY Lingvo API посредством отправки ключа авторизации
$myCurl = curl_init();
curl_setopt_array($myCurl, array(
  CURLOPT_URL => 'https://developers.lingvolive.com/api/v1.1/authenticate',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_POST => true,
  CURLOPT_HTTPHEADER => $header
));
$bearer_token = curl_exec($myCurl);
curl_close($myCurl);

//Модификация заголовков для получения данных
$header[2] = 'Authorization: Bearer '.$bearer_token;

//Получение словарной статьи
$myCurl = curl_init();
curl_setopt_array($myCurl, array(
  CURLOPT_URL => 'https://developers.lingvolive.com/api/v1/Minicard?text=plum&srcLang=1033&dstLang=1049',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => $header
));
$response = curl_exec($myCurl);
curl_close($myCurl);

//Тут я просто вывожу ответ сервера чтобы убедиться, что всё работает.
echo '<pre>';
print_r(json_decode($response));
echo '</pre>';

A
ArtAVi, 2019-10-31
@ArtAVi

Example in Python

import requests
import json

URL_AUTH = 'https://developers.lingvolive.com/api/v1.1/authenticate'
URL_TRANSLATE = 'https://developers.lingvolive.com/api/v1/Minicard'
KEY = 'Ключ для доступа к API'

def get_a_word_translation(key: str) -> str:
    headers_auth = {'Authorization': 'Basic ' + KEY}
    auth = requests.post(URL_AUTH, headers=headers_auth)
    if auth.status_code == 200:
        token = auth.text
        headers_translate = {
            'Authorization': 'Bearer ' + token
        }
        params = {
            'text': key,
            'srcLang': 1033,
            'dstLang': 1049
        }
        req = requests.get(
            URL_TRANSLATE, headers=headers_translate, params=params)
        res = req.json()
        try:
            value = res['Translation']['Translation']
            return value
        except TypeError:
            if res == 'Incoming request rate exceeded for 50000 chars per day pricing tier':
                return res
            else:
                return None
    else:
        print('Error!' + str(auth.status_code))

if __name__ == "__main__":
    not_translated_words_test = ['victim', 'home', 'root']
    translated_words_test = {}
    for en in not_translated_words_test:
        ru = get_a_word_translation(en)
        if ru == 'Incoming request rate exceeded for 50000 chars per day pricing tier':
            break
        translated_words_test[en] = ru
        save_data_to_json('data/translated_words_test.json',
                          translated_words_test)

A
aleks_d, 2020-08-10
@aleks_d

Modified code in Python 3. In the example, there is no reference to the missing function for saving results and authorization is moved to a separate function to avoid redundant repeated requests, since the token can be obtained once and it will be valid for 24 hours.

URL_AUTH = 'https://developers.lingvolive.com/api/v1.1/authenticate'
URL_TRANSLATE = 'https://developers.lingvolive.com/api/v1/Minicard'
KEY = 'Ключ для доступа к API'


def get_auth_token(key: str, url_auth: str) -> str:
    headers_auth = {'Authorization': 'Basic ' + key}
    auth = requests.post(url=url_auth, headers=headers_auth)
    if auth.status_code == 200:
        cur_token = auth.text
        return cur_token
    else:
        print('Error - ' + str(auth.status_code))
        return ''


def get_a_word_translation(cur_token: str, url_tr: str, word: str) -> str:
    headers_translate = {
        'Authorization': 'Bearer ' + cur_token
    }
    params = {
        'text': word,
        'srcLang': 1033,
        'dstLang': 1049
    }
    req = requests.get(
        url_tr, headers=headers_translate, params=params)
    if req.status_code == 200:
        res = req.json()
        try:
            value = res['Translation']['Translation']
            return value
        except TypeError:
            if res == 'Incoming request rate exceeded for 50000 chars per day pricing tier':
                print('Error - Incoming request rate exceeded for 50000 chars per day pricing tier')
                return res
            else:
                return 'No translation available'
    else:
        print('Error!' + str(req.status_code))


if __name__ == "__main__":
    token = get_auth_token(key=KEY, url_auth=URL_AUTH)
    not_translated_words_test = ['victim', 'home', 'root']
    for en_word in not_translated_words_test:
        ru_translation = get_a_word_translation(cur_token=token, url_tr=URL_TRANSLATE, word=en_word)
        if ru_translation == 'Incoming request rate exceeded for 50000 chars per day pricing tier':
            break
        print(ru_translation)

"""
Result output:
living being, sacrificed to the deity
house, dwelling
root
"""

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question