百度翻译api id和密钥分享

1.VB 求 可用的 百度,谷歌,有道,词霸翻译 API代码

Public Const BAIDU_APP_ID = "XXXX" '百度申请后得到

Public Const BAIDU_APP_KEY = "XXXX" '百度申请后得到

Public Type MD5_CTX

dwNUMa As Long

dwNUMb As Long

Buffer(15) As Byte

cIN(63) As Byte

cDig(15) As Byte

End Type

'-------------------以上放入类模块

Public Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long

Public Declare Sub MD5Final Lib "advapi32" (lpContext As MD5_CTX)

Public Declare Sub MD5Init Lib "advapi32" (lpContext As MD5_CTX)

Public Declare Sub MD5Update Lib "advapi32" (lpContext As MD5_CTX, ByRef lpBuffer As Any, ByVal BufSize As Long)

Public Function Translate(ByVal Text As String, Optional ByVal Source As String = "auto", Optional ByVal Target As String = "auto", Optional ByVal AppID As String = BAIDU_APP_ID, Optional ByVal Key As String = BAIDU_APP_KEY) As String

Dim XML As Object, stcContext As MD5_CTX, URL As String, PostData As String, Salt As String

Dim Arr() As Byte, I As Long, Result As String

URL = "http://api.fanyi.baidu.com/api/trans/vip/translate"

Randomize

Salt = Replace(Rnd, ".", "")

MD5Init stcContext

PostData = "q=" & Text

PostData = PostData & "&appid=" & AppID

PostData = PostData & "&salt=" & Salt

PostData = PostData & "&from=" & Source

PostData = PostData & "&to=" & Target

PostData = PostData & "&sign="

I = Len(AppID & Text & Salt & Key)

ReDim Arr(I * 3)

I = WideCharToMultiByte(65001, 0, StrPtr(AppID & Text & Salt & Key), I, Arr(0), I * 3 + 1, vbNullString, 0)

If I < 1 Then Exit Function

MD5Update stcContext, Arr(0), I

MD5Final stcContext

For I = 0 To UBound(stcContext.cDig)

PostData = PostData & LCase(IIf(stcContext.cDig(I) < 16, "0" & Hex(stcContext.cDig(I)), Hex(stcContext.cDig(I))))

Next

Set XML = CreateObject("WinHttp.WinHttpRequest.5.1")

XML.Option(6) = False

XML.Option(4) = 13056

XML.Open "POST", URL

XML.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"

XML.SetRequestHeader "Content-Length", LenB(StrConv(PostData, vbFromUnicode))

XML.Send PostData

PostData = XML.ResponseText

Set XML = Nothing

I = InStr(PostData, "error_code")

If I > 0 Then

Result = "错误代码:" & Mid(PostData, I + 13, InStr(I + 13, PostData, """") - I - 13)

I = InStr(PostData, "error_msg")

Result = Result & ",说明:" & Mid(PostData, I + 12, InStr(I + 12, PostData, """") - I - 12)

Else

I = 1

PostData = Replace(PostData, "\""", "\'")

Do Until InStr(I, PostData, """dst"":""") = 0

I = InStr(I, PostData, """dst"":""") + 7

Result = IIf(Len(Result) = 0, "", Result & vbCrLf) & Mid(PostData, I, InStr(I, PostData, """") - I)

Loop

Result = Replace(Result, "\'", """")

ReDim Arr(1)

Do Until InStr(Result, "\u") = 0

I = InStr(Result, "\u")

Result = Replace(Result, Mid(Result, I, 6), ChrW("&H" & Mid(Result, I + 2, 4)))

Loop

End If

Translate = Result

End Function

调用:Text1 = Translate("Hello World!")

申请ID和KEY地址:http://api.fanyi.baidu.com

百度翻译api id和密钥分享  第1张

2.如何获取API 密钥

商户ID是申请微信支付后才获取的,你申请微信支付 资料审核通过时,腾讯会给一个支付平台的帐户,里面ID、密钥等信息 你登录即可查看

百度翻译api id和密钥分享  第2张

3.如何用百度翻译API实现自动翻译word文档的功能?

这个不难,需要先注册一下百度翻译开发者,然后后申请一下APP ID和密钥,最后就能在代码中调用API来实现翻译word文档功能了,下面我简单介绍一下实现过程,实验环境win10+python3.6+pycharm5.0,主要内容如下:1.注册开发者,这个直接到百度翻译开放平台注册就行,或者直接使用自己的百度账号也行,注册成功后,就会生成开发者的申请信息,包括APP ID和密钥,我们需要记住这2个字段的值,后面的代码中需要用到:2.注册完成后,我们就可以编程来实现调用百度翻译API实现翻译word文档功能,主要步骤及截图如下:这里为了更好的说明问题,我新建了一个word文档,主要内容如下,后面的翻译就是围绕这个文档展开:为了方便读写word文档,这里需要先安装python-docx库,专门用于操作word文档,至于安装的话,直接在cmd窗口输入命令“pip install python-docx”就行,如下:接着就是编写具体代码了,官网给出的是python2的版本示例(也有其他编程语言的示例,像java,php,js,c等),比较旧,这里我改成了python3的版本,为了更方便的说明,我分成了3个部分,主要如下:第一部分:读取需要翻译的word文档,具体代码如下:第二部分:调用API翻译word文档内容,具体代码如下(参数fromLang代表原始语种,toLang是翻译语种,paragraphs是需要翻译的内容):第三部分:将翻译的结果写入word文档,具体代码如下:点击运行程序,就会在当前目录下生成一个result文档,打开这个文档,就是翻译的最终结果,如下:至此,我们就完成了调用百度翻译API来实现翻译word文档的功能。总的来说,整个过程很简单,思路很清晰,就是代码量有些多,只要你有一定的编程基础(python基础),熟悉一下相关代码和示例,多练习几遍,很快就能掌握的,网上也有相关教程和资料,感兴趣的话,可以搜一下,希望以上分享的内容能对有所帮助吧,也欢迎大家评论、留言。

百度翻译api id和密钥分享  第3张

4.HesoId翻译

HesoId翻译成中文是:海索,见下图百度翻译

百度翻译api id和密钥分享  第4张

上一篇:怎样查找自己想要的网站
下一篇:数据库下载