Sign in to follow this  
Followers 0
Jonniy

Microsoft Translator API (WinHttp) ReceiveResponse Error

5 posts in this topic

#1 ·  Posted (edited)

Hello everyone, I'm having a problem when I try to receive a response from the AccessToken windows.net server.
Everything works fine untill _WinHttpReceiveResponse(). The @error is 1 which stands for DllCall failed.
 

#include <WinHttp.au3>
$MTWinHttpDHandle = 0
Call("MirosoftTranslatorAPIGetAccessToken")

Func MirosoftTranslatorAPIGetAccessToken()
    $MTAccessTokenURL = "https://www.datamarket.accesscontrol.windows.net/v2/OAuth2-13?"
    $MTAccessTokenPost = "client_id=" & __WinHttpURLEncode("//CLIENT_ID//") & "&client_secret=" & __WinHttpURLEncode("//CLIENT_SECRET//") & "&grant_type=client_credentials&scope=http://api.microsofttranslator.com"
    $MTWinHttpHandle = _WinHttpOpen()
    $MTWinHttpCHandle = _WinHttpConnect($MTWinHttpHandle,"www.datamarket.accesscontrol.windows.net",$INTERNET_DEFAULT_HTTPS_PORT)
    $MTWinHttpRHandle = _WinHttpOpenRequest($MTWinHttpCHandle, "POST",$MTAccessTokenURL,Default,Default,Default,$WINHTTP_FLAG_SECURE)
    _WinHttpAddRequestHeaders($MTWinHttpRHandle, "Content-Type: application/x-www-form-urlencoded")
    _WinHttpAddRequestHeaders($MTWinHttpRHandle, "Keep-Alive: 300")
    _WinHttpAddRequestHeaders($MTWinHttpRHandle, "Connection: keep-alive")
    $MTWinHttpSHandle = _WinHttpSendRequest($MTWinHttpRHandle,-1 &@CRLF,$MTAccessTokenPost)
    $MTWinHttpWHandle = _WinHttpReceiveResponse($MTWinHttpRHandle)
    $MTWinHttpDHandle = _WinHttpQueryDataAvailable($MTWinHttpRHandle)
    $MTWinHttpRDHandle = _WinHttpReadData($MTWinHttpRHandle)
    $MTWinHttpRHHandle = _WinHttpQueryHeaders($MTWinHttpRHandle)
    _WinHttpCloseHandle($MTWinHttpHandle)
    _WinHttpCloseHandle($MTWinHttpCHandle)
    _WinHttpCloseHandle($MTWinHttpRHandle)
    $MTWinHttpResponseFile = FileOpen(@AppDataDir&"\tmp_translate_response.ini",10)
    FileWrite($MTWinHttpResponseFile,"[Request.Json.Response]" & @CRLF & "AccessToken=" & $MTWinHttpRDHandle & @CRLF & "[Request.Headers]" & @CRLF & "AccessTokenHeaders=" & $MTWinHttpRHHandle)
    FileFlush(-1)
    FileClose(-1)
EndFunc

Of course, I've replaced the client_id and the client_secret with my own. The required post data is taken from here: http://msdn.microsoft.com/en-us/library/hh454950.aspx . I can't programm in C# or PHP so I can't just convert the example script. Thanks for help, much appreciated.

Edited by Jonniy

Thanks for your help & have a good day. Yours sincerely, -Jonniy-  

Share this post


Link to post
Share on other sites



If I were you I would do it like this:

#include "Winhttp.au3"

; Make sure you set correct values here
Global Const $sId = "Your_ID"
Global Const $sSecret = "Your_Secret"
Global Const $sGrantType = "client_credentials"
Global Const $sScope = "http://api.microsofttranslator.com/"

Global Const $sAuthAddress = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/"


Local $sAuthResponse = AccessTokenAuthentication($sId, $sSecret, $sGrantType, $sScope)

ConsoleWrite("!ERROR = " & @error & @CRLF)
ConsoleWrite("+>$sAuthResponse: " & @CRLF & $sAuthResponse & @CRLF & @CRLF)
MsgBox(4096, "Response", $sAuthResponse)


Func AccessTokenAuthentication($sId, $sSecret, $sGrantType, $sScope)
; Construct the form to fill
    Local $sForm = _
            '<form action="' & $sAuthAddress & '" method="post" enctype="application/x-www-form-urlencoded">' & _
            '    <input name="client_id"/>' & _ ;
            '    <input name="client_secret"/>' & _ ;
            '    <input name="grant_type"/>' & _ ;
            '    <input name="scope"/>' & _ ;
            '</form>'

    Local $hOpen = _WinHttpOpen()
    Local $hConnect = $sForm ; will pass form as string

    ; Fill the form
    Local $sHTML = _WinHttpSimpleFormFill($hConnect, $hOpen, Default, _
            "name:client_id", $sId, _
            "name:client_secret", $sSecret, _
            "name:grant_type", $sGrantType, _
            "name:scope", $sScope)

    Local $iErr = @error ; collect error

    ; Close handles
    _WinHttpCloseHandle($hConnect)
    _WinHttpCloseHandle($hOpen)

    Return SetError($iErr, 0, $sHTML)
EndFunc
1 person likes this

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks, I'll try it out now.

Edit: Works like a charm, again thanks!

Edit2: BTW: Do you know why it didn't work with my script, or have you just did it your own way from the start?

Edited by Jonniy

Thanks for your help & have a good day. Yours sincerely, -Jonniy-  

Share this post


Link to post
Share on other sites

I didn't try your code. You said it doesn't work so why bother :P

I just clicked on the link you provided and read through.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

OK, thanks^^


Thanks for your help & have a good day. Yours sincerely, -Jonniy-  

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0