我在与我的天蓝色翻译api通话中获得回报时有点挣扎。 我的代码基于此代码https://github.com/MicrosoftTranslator/PythonConsole,并且完美无缺。
我还有一个带有一些测试数据的arangoDB。这有什么作用并给我这个:Result on db test
但是,如果我将两者结合起来如下:
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()
我什么都没得到。控制台需要不到一秒钟,然后我可以在终端上输入新命令。但是没有显示结果,也试图将其放入文件中。 Azure告诉我,我调用了API,但我无法看到那里处理的内容。
感谢您的帮助!
答案 0 :(得分:0)
我尝试测试您的代码以调用Azure Translator API,但我发现代码的翻译部分工作正常,Arango部分也可以正常工作。根据你的代码对我来说不完整,我猜的唯一问题是函数GetTextAndTranslate(finalToken)
应定义为GetTextAndTranslate(test, finalToken)
,可以传递参数test
集合,如下所示。
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)
希望它有所帮助。任何更新,请随时告诉我。