简体   繁体   中英

Azure tranlation api don't deliver results while handover data from arangoDB

I struggle a little with getting an return on my azure translation api call. My code is based on this code https://github.com/MicrosoftTranslator/PythonConsole and it work perfectly.

I furthermore have a arangoDB with some test data. Which does it work and give me this: Result on db test

However, if i combine both as follow:

from xml.etree import ElementTree
from auth import AzureAuthClient
from arango import ArangoClient
import requests

client = ArangoClient(
    protocol='http',
    host='localhost',
    port=32768,
    username='root',
    password='password',
    enable_logging=True
)
db = client.database('testdb')
test = db.collection('testcol')

def GetTextAndTranslate(finalToken):

    fromLangCode = "en"
    toLangCode = "de"
    textToTranslate = " "

    for t in test:
        #text to translate
        textToTranslate = t['name']

        # Call to Microsoft Translator Service
        headers = {"Authorization ": finalToken}
        translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text={}&to={}".format(textToTranslate, toLangCode)

        translationData = requests.get(translateUrl, headers = headers)
        # parse xml return values
        translation = ElementTree.fromstring(translationData.text.encode('utf-8'))
        # display translation if needed
        print (translation.text)


if __name__ == "__main__":
    #Add your client secret in the next line 
    client_secret = 'azurepassword'
    auth_client = AzureAuthClient(client_secret)
    bearer_token = 'Bearer ' + auth_client.get_access_token()

I just get nothing. The console needs less then a second and then I can enter new command on the terminal. But no result displayed, also tried to put it into a file. Azure tell me that I called the API, but I can't see what was processed there.

Thanks for your help!

I tried to test your code for calling Azure Translator API, but I discovered the translator part of your code works fine and the Arango part also works fine. Under your code is not complete for me, the only issue I guess is that the function GetTextAndTranslate(finalToken) shoud be defined as GetTextAndTranslate(test, finalToken) which can be passed the argument test collection like below.

def GetTextAndTranslate(test, finalToken):
    # Your code
    ........

if __name__ == "__main__":
    client = ArangoClient(
        protocol='http',
        host='localhost',
        port=32768,
        username='root',
        password='password',
        enable_logging=True
    )
    db = client.database('testdb')
    test = db.collection('testcol')
    #Add your client secret in the next line 
    client_secret = 'azurepassword'
    auth_client = AzureAuthClient(client_secret)
    bearer_token = 'Bearer ' + auth_client.get_access_token()
    GetTextAndTranslate(test, bearer_token)

Hope it helps. Any update, please feel free to let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM