想补充下我的 DrawSthHelper 的翻译功能,google translate 的 api 收费了,于是转而投奔 bing translate ,然后发现,真 TMD 麻烦。很少用 MS 的 online 服务,好像是 bing app ID 的模式 deprecated 了,以后要用 Azure Marketplace 的了。然后就有了下面这个麻烦至极的过程。。。
api doc 在这里:http://msdn.microsoft.com/en-us/library/ff512421.aspx
其中,因为 bing app id 已经不再维护了(但还是可用的),还要注册一个 Azure Marketplace ,还要获取一个 access token 。
获取 access token 在这里:http://msdn.microsoft.com/en-us/library/hh454950.aspx
我简单罗列一下步骤,也就是翻译一下啦 @@
1,来这里免费注册个账号 https://datamarket.azure.com/developer/applications/
2,然后通过这个链接:http://go.microsoft.com/?linkid=9782667 ,来订阅(subscribe)Microsoft Translate API 的数据,每个月可以免费翻译 200w 个字符,我相信。。。一般人都够用了。
3,然后注册一个自己的应用,以下需要填的信息:
客户端ID(client_id,等会需要用),名称(随意),客户端机密(这翻译烂的。。。client secret ,等会也需要用),重定向 URL (其实是随意的,就是如果你的应用对外使用了,会显示你的名称,而你的名称的链接就是填的这个)
4,然后,POST 数据去获取 access token
POST 的参数如下:
client_id , 就是刚才那个客户端ID
client_secret ,URL encode 一下,例如,”=” 是 %3D
scope,定值,http://api.microsofttranslator.com/
grant_type,也是定值,client_credentials
然后就 POST 吧
curl -d ‘grant_type=client_credentials&client_id={your client id}&client_secret={your client secret after url encode}&scope=http://api.microsofttranslator.com’ https://datamarket.accesscontrol.windows.net/v2/OAuth2-13 | python -m json.tool
返回的是一个 json ,我用 python 去解析了一下,我们只需要其中的 access token 。
5,翻译一个词看看,这回是 GET ,GET 的参数如下:
text , 需要翻译的文本啊,字符串啊
to ,这个不用解释了吧。。。
from ,这个也不用
不过,需要注意的一点。。。就是要加一个 Authorization 的header ,值是 bearer + ” ” + 刚才的 access token
翻译开始:
curl -H “Authorization: bearer {刚才那一大串access token}” ”http://api.microsofttranslator.com/V2/Http.svc/Translate?text=hello&from=en&to=zh-CH”
bing 返回:
<string xmlns=”http://schemas.microsoft.com/2003/10/Serialization/”>你好</string>
(这个时代还用 XML 。。。)
附 language codes :http://msdn.microsoft.com/en-us/library/hh456380.aspx
