【问题标题】:call microsoft cognitive services translator with python用 python 调用微软认知服务翻译器
【发布时间】:2017-06-23 15:29:08
【问题描述】:

我现在在 python3 中尝试来自 azure notebook 的以下代码 导入请求

# Getting the key from tab Keys on Azure portal
 key = "xxxxxxxxxxx" 


 url4authentication = 'https://api.cognitive.microsoft.com/sts/v1.0/issueToken'
 headers4authentication = {'Ocp-Apim-Subscription-Key': key}
 resp4authentication = requests.post(url4authentication,  headers=headers4authentication)
 token = resp4authentication.text

 print(token)
 text="hello there how are you"
 come = "en"
 to = "es"
 url4translate = 'https://api.microsofttranslator.com/v2/http.svc/Translate'
 from xml.etree import ElementTree
 response = requests.get('https://api.microsofttranslator.com/v2/http.svc', params = param)
 print(response)

我收到以下错误:

OSError                                   Traceback (most recent call last)
/home/nbuser/anaconda3_23/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, **response_kw)
    587             if is_new_proxy_conn:
--> 588                 self._prepare_proxy(conn)
    589 

/home/nbuser/anaconda3_23/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py in _prepare_proxy(self, conn)
    800 
--> 801         conn.connect()
    802 

/home/nbuser/anaconda3_23/lib/python3.4/site-packages/requests/packages/urllib3/connection.py in connect(self)
    290             # self._tunnel_host below.
--> 291             self._tunnel()
    292             # Mark this connection as not reusable

/home/nbuser/anaconda3_23/lib/python3.4/http/client.py in _tunnel(self)
    856             raise OSError("Tunnel connection failed: %d %s" % (code,
--> 857                                                                message.strip()))
    858         while True:

OSError: Tunnel connection failed: 403 Forbidden

【问题讨论】:

    标签: python azure microsoft-cognitive


    【解决方案1】:

    作为参考,我建议您首先可以尝试参考我对其他类似 SO 线程 Response from Microsoft Translate API in Python 的回答来解决您的问题。如果您有更多顾虑,请随时告诉我。

    【讨论】:

    • 非常感谢您的回答。现在我有另一个错误:OSError Traceback(最近一次调用最后一次)知道会是什么
    • @hinda 请在您的帖子中更新与OSError 相关的代码内容以及详细信息。
    猜你喜欢
    • 2018-08-11
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多