Question

Before you can call the Microsoft translation services, you need a token. I am trying to follow step 3 of these instructions. A call to the token service is required to get a token. I have a clientId and secret pass. I have looked at the signature of the service http://api.microsofttranslator.com/V2/soap.svc. I can see a GetAppIdToken method. Its signature is quite different from what's implied by the documentation.

Does anybody know what service operation is referred to on the instruction page? The WSDL and documentation don't seem to match. Is there another service?

The instructions


Explanation of how to solve issues

EDIT : I found this tool on the WEB to help track the traffic that should work http://oauthdevconsole.cloudapp.net/PartialOAuth

EDIT2: A better explanation of how to register here http://blogs.msdn.com/b/translation/p/gettingstarted1.aspx

Was it helpful?

Solution

The code sample included in the article shows how to do this. You have to dig around it a bit to figure out how it all comes together, but in a nutshell they use a WebRequest with a method of "POST" and pass the 4 required values in that POST. I suggest looking at the sample code, but to distill it down to its basic elements it might look something like this:

string request = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", HttpUtility.UrlEncode(clientId), HttpUtility.UrlEncode(clientSecret));
WebRequest webRequest = WebRequest.Create("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13");
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(request);

webRequest.ContentLength = bytes.Length;

using (Stream outputStream = webRequest.GetRequestStream())
{
    outputStream.Write(bytes, 0, bytes.Length);
}

using (WebResponse webResponse = webRequest.GetResponse())
{
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AdmAccessToken));
    //Get deserialized object from JSON stream
    AdmAccessToken token = (AdmAccessToken)serializer.ReadObject(webResponse.GetResponseStream());
    return token;
}

The bulk of the above code is in private AdmAccessToken HttpPost(string DatamarketAccessUri, string requestDetails) method in the sample.

The AdmAccessToken is a class defined in the sample that has properties for the returned values:

access_token
token_type
expires_in
scope

It's a very comprehensive sample, and takes a small amount of digging to get the relevant parts, but its all there as near as I can tell. The snippet I posted simply combined a couple of things that were done elsewhere in the sample for simplicity.

OTHER TIPS

A Python code to get the token:

import requests
import urllib

data = dict(
    client_id='client_id',
    client_secret='client_secret',
    scope='http://api.microsofttranslator.com',
    grant_type='client_credentials'
)
resp = requests.post(url='https://datamarket.accesscontrol.windows.net/v2/OAuth2-13', data=urllib.urlencode(data))

Note: If you send the parameters in the URL doing the POST request you will get a Bad Request (400) with the following description ACS90004: The request contains 1 tokens separated by \u0027=\u0027 instead of a single key value pair. This is why using requests one specifies data and not params.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top