I had registered my Application at Azure, received a secret and successfully got an access token using TIdHTTP with my Delphi 2010:
paramsList := TStringList.Create;
paramsList.Add('grant_type=client_credentials');
paramsList.Add('client_id=<ClientID>');
paramsList.Add('client_secret=<ClientSecret>');
paramsList.Add('scope=http://api.microsofttranslator.com');
try
Result := idHTTP.Post(uri, lParamList);
finally
FreeAndNill(idHTTP);
FreeAndNill(paramsList);
end;
I then extract the token part of the response using copy. Now, when I try to get the actual translation I receive a Bad Request error. Here is what I try:
idHTTP.Request.CustomHeaders.AddValue('Authorization', headers);
try
stringResult := idHTT.Get('http://api.microsofttranslator.com/v2/Http.svc/Translate?text=Gracias%20a%20l%20vida&from=es&to=en');
finally
FreeAndNil(idHTTP);
end;
I also failed getting a response using post:
paramList := TStiringList.Create;
paramList.Add('Authorization= Bearer ' + Token);
try
idHTTP.Post(uri, paramList);
finally
...
Still same response - 400, Any thoughts?
First, the facts: When requesting an access token at Microsoft Translator you use Post and when accessing the API with the access token you use Get (Thanks @RemyLebeau). Now the code. The code above for receiving the access token works. So the code for getting the translation is also straight forward:
lHTTP.Request.CustomHeaders.FoldLines := false; // avoid split by white space
lHTTP.Request.CustomHeaders.AddValue('Authorization', 'Bearer ' + AuthKey);
myStream = TStringStream.Create;
try
lHTTP.Get(uri, stream);
stream.Position := 0;
Result := stream.ReadString(stream.size);
finally
FreeAndNil(lHTTP);
FreeAndNil(stream);
end;