invalid_request error when getting access token.
-
Wednesday, June 27, 2012 1:34 AM
I'm trying to get my access token for my application, using this URL:
https://datamarket.accesscontrol.windows.net/v2/OAuth2-13?grant_type=client_credentials&client_id=//CLIENT ID//&client_secret=//CLIENT SECRET//=&scope=http://api.microsofttranslator.com
Obviously I replace //CLIENT ID// and //CLIENT SECRET// with my applications information.
I tried encoding the tokens with HtmlEncode in my application but got a 400 error. So I tried the request in my browser and this is the JSON response I got:
{ "error":"invalid_request", "error_description":"ACS90007: Request method not allowed. \r\nTrace ID: 2144c829-f3fa-4ed8-80e6-40841e6a3f69\r\nTimestamp: 2012-06-27 01:11:27Z" }
I don't know what I'm doing wrong, any help?
All Replies
-
Thursday, June 28, 2012 2:58 PMOwner
Please try to URLEncode the client ID and client secret. The method call will fail, if either one contains URL-reserved characters.
Let us know if this solved the situation.
Chris Wendt
Microsoft Translator -
Thursday, June 28, 2012 3:01 PM
There's a full example on using ASP.NET to get the access token here:
http://blogs.msdn.com/b/translation/p/gettingstarted1.aspx
and
http://blogs.msdn.com/b/translation/p/gettingstarted2.aspx
See step 3.1 in the second article in particular -- you'll need to UrlEncode the Client Secret and Client ID.
-
Thursday, June 28, 2012 7:56 PM
I am using HtmlEncode to encode the tokens, does that not do the same thing as URLEncode?
This is my code for encoding the URL:
Me.request = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope={2}", _ WebUtility.HtmlEncode(clientId), _ WebUtility.HtmlEncode(clientSecret), _ WebUtility.HtmlEncode("http://api.microsofttranslator.com"))
edit: --
I guess it doesn't. But, I encoded the values manually online using a URL encoder tool and tried the Url. And got the same error as above.
- Edited by ecnepsnai Thursday, June 28, 2012 8:01 PM
-
Thursday, June 28, 2012 8:01 PMOwnerHTMLEncode and UrlEncode are different. We need UrlEncode here.
-
Thursday, June 28, 2012 8:48 PM
Well I found a sample that was pinned on this fourm and copied the code, this is my current code:
Public Function TranslateText(ByVal Text As String, ByVal FromLanguange As String, ByVal ToLanguange As String) As String Dim clientID As String = "########" Dim clientSecret As String = "#######" Dim strTranslatorAccessURI As String = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13" Dim strRequestDetails As String = String.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", HttpUtility.UrlEncode(clientID), HttpUtility.UrlEncode(clientSecret)) Dim webRequest As System.Net.WebRequest = System.Net.WebRequest.Create(strTranslatorAccessURI) webRequest.ContentType = "application/x-www-form-urlencoded" webRequest.Method = "POST" Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(strRequestDetails) webRequest.ContentLength = bytes.Length Using outputStream As System.IO.Stream = webRequest.GetRequestStream() outputStream.Write(bytes, 0, bytes.Length) End Using Dim webResponse As System.Net.WebResponse = webRequest.GetResponse() ' Make sure you add a reference to System.Runtime.Serialization here Dim AdmToken As New AdmAccessToken Dim serializer As System.Runtime.Serialization.Json.DataContractJsonSerializer = New System.Runtime.Serialization.Json.DataContractJsonSerializer(AdmToken.GetType()) Dim token As AdmAccessToken = serializer.ReadObject(webResponse.GetResponseStream()) Dim txtToTranslate As String = Text Dim headerValue As String = "Bearer " + token.access_token Dim soapClient As New TranslatorService.LanguageServiceClient() Dim translatedText As String = soapClient.Translate(headerValue, txtToTranslate, FromLanguange, ToLanguange, "text/plain", "") Return translatedText End FunctionAnd this is the error I get:
System.InvalidOperationException: Could not find default endpoint element that references contract 'TranslatorService.LanguageService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element. at MyFire.myfireactions.Twitter_Status_Translate(String Text) in C:\Users\User\Documents\Visual Studio 2010\Projects\MyFire\MyFire\Modules\myfireactions.vb:line 611 at MyFire.exceptionhandler.TranslateMessage(String TweetText) in C:\Users\User\Documents\Visual Studio 2010\Projects\MyFire\MyFire\Modules\Support Methods\exceptionhandler.vb:line 38 at MyFire.Application.li_leftclick(StackPanel sender, MouseButtonEventArgs e) in C:\Users\User\Documents\Visual Studio 2010\Projects\MyFire\MyFire\Application.xaml.vb:line 253
-
Monday, July 02, 2012 1:55 PM
On what line did you get the error?
This looks like you may not have created a web service reference to the Translator service, which the last 3 lines of the function need (Dim soapClient onwards)..
This is the service that you need to create a reference to: http://api.microsofttranslator.com/V2/Soap.svc