Code: Select all
; Get access_token
client_id := "Your Registered Application client_id"
client_secret := "Your Registered Application client_secret"
url := "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"
postData := "client_id=" . client_id
. "&client_secret=" . UriEncode(client_secret)
. "&scope=http://api.microsofttranslator.com"
. "&grant_type=client_credentials"
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("POST", url, True)
whr.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
whr.Send(postData)
whr.WaitForResponse()
If !RegExMatch(whr.ResponseText, "i)""access_token"":""\K[^""]+", access_token)
Throw, Get access_token failed
; Translate
text := "test string"
from := "en"
to := "zh-CHS"
url := "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" . UriEncode(text) . "&from=" . from . "&to=" . to
authToken := "Bearer " . access_token
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", url, True)
whr.SetRequestHeader("Authorization", authToken)
whr.Send()
whr.WaitForResponse()
MsgBox, % whr.ResponseText
Return
; ==============================================================================================
; http://www.autohotkey.com/board/topic/75390-ahk-l-unicode-uri-encode-url-encode-function/?p=480216
UriEncode(Uri, Enc = "UTF-8")
{
StrPutVar(Uri, Var, Enc)
f := A_FormatInteger
SetFormat, IntegerFast, H
Loop
{
Code := NumGet(Var, A_Index - 1, "UChar")
If (!Code)
Break
If (Code >= 0x30 && Code <= 0x39 ; 0-9
|| Code >= 0x41 && Code <= 0x5A ; A-Z
|| Code >= 0x61 && Code <= 0x7A) ; a-z
Res .= Chr(Code)
Else
Res .= "%" . SubStr(Code + 0x100, -1)
}
SetFormat, IntegerFast, %f%
Return, Res
}
UriDecode(Uri, Enc = "UTF-8")
{
Pos := 1
Loop
{
Pos := RegExMatch(Uri, "i)(?:%[\da-f]{2})+", Code, Pos++)
If (Pos = 0)
Break
VarSetCapacity(Var, StrLen(Code) // 3, 0)
StringTrimLeft, Code, Code, 1
Loop, Parse, Code, `%
NumPut("0x" . A_LoopField, Var, A_Index - 1, "UChar")
StringReplace, Uri, Uri, `%%Code%, % StrGet(&Var, Enc), All
}
Return, Uri
}
StrPutVar(Str, ByRef Var, Enc = "")
{
Len := StrPut(Str, Enc) * (Enc = "UTF-16" || Enc = "CP1200" ? 2 : 1)
VarSetCapacity(Var, Len, 0)
Return, StrPut(Str, &Var, Enc)
}
"Short" version:
Code: Select all
MSTrans.Get_AccessToken(client_id, client_secret)
MsgBox, % MSTrans.Translate("Getting Started with Microsoft Translator")
Class MSTrans
{
Get_AccessToken(client_id, client_secret) {
url := "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"
data := "client_id=" . client_id
. "&client_secret=" . UriEncode(client_secret)
. "&scope=http://api.microsofttranslator.com"
. "&grant_type=client_credentials"
WinHttpRequest(url, ioData := data)
Return this.authToken := "Bearer " . JSON_ToObj(ioData).access_token
}
Translate(text, from := "en", to := "zh-CHS") {
url := "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" . UriEncode(text) . "&from=" . from . "&to=" . to
WinHttpRequest(url, ioData := "", ioHdr := "Authorization: " this.authToken)
Return RegExReplace(ioData, "<.*?>")
}
}
Without need client_id and client_secret:
http://ahkscript.org/boards/viewtopic.p ... 115#p54115