Capturing the access token, but the request address translation error.

75 views
Skip to first unread message

chengying...@gmail.com

unread,
Feb 14, 2014, 4:46:55 AM2/14/14
to microsoft-tran...@googlegroups.com
Below is my code:
public static void bingTranslate(final String messgeContent,
final OnDataReturnListener onDataReturnListener) {

new Thread(new Runnable() {

@Override
public void run() {
try {
String result = retrieveResponse(uri);
onDataReturnListener.translateRusult(result, 0);
} catch (Exception e) {
onDataReturnListener.translateRusult("网络错误", 60);
e.printStackTrace();
}

}
}).start();

}

/**
* Forms an HTTP request, sends it using GET method and returns the result
* of the request as a String.
* @param url
*            The URL to query for a String response.
* @return The translated String.
* @throws Exception
*             on error.
*/
private static String retrieveResponse(String url) throws Exception {
long ct = System.currentTimeMillis();
System.out.println("before==" + tokenExpiration);
System.out.println("######====" + (ct - tokenExpiration));
if (clientId != null && clientSecret != null && ct > tokenExpiration) {
// String tokenJson = getToken(clientId, clientSecret);
String tokenJson = httpPostGetToken();
System.out.println("tokenJson=========" + tokenJson);
JSONObject jsonObject = new JSONObject(tokenJson);
Integer expiresIn = jsonObject.getInt("expires_in");
tokenExpiration = System.currentTimeMillis()
+ ((expiresIn * 1000) - 1);// 10秒的生命周期,失效后,会再次获得AccessToken
token = "Bearer " + jsonObject.getString("access_token");
System.out.println("token==" + token);
System.out.println("after==" + tokenExpiration);
}
String to = "en";
String from = "zh-CHS";
url = url + "?text=" + URLEncoder.encode("你好", ENCODING) + "&from="
+ from + "&to=" + to + "&appId=" + token;
System.out.println("url=========" + url);
final HttpURLConnection uc = (HttpURLConnection) new URL(url)
.openConnection();
if (referrer != null)
uc.setRequestProperty("referer", referrer);
uc.setRequestProperty("Content-Type", contentType + "; charset="
+ ENCODING);
uc.setRequestProperty("Accept-Charset", ENCODING);
// if (token != null) {
// uc.setRequestProperty("Authorization", token);
// }
uc.setRequestMethod("GET");
uc.setDoOutput(true);

try {
final int responseCode = uc.getResponseCode();
final String result = inputStreamToString(uc.getInputStream());
if (responseCode != 200) {
throw new Exception("Error from Microsoft Translator API: "
+ result);
}
return result;
} finally {
if (uc != null) {
uc.disconnect();
}
}
}

The following is abnormal situation:
java.io.FileNotFoundException:http://api.microsofttranslator.com/v2/Http.svc/Translate?text=%E4%BD%A0%E5%A5%BD&from=zh-CHS&to=en&appId=Bearer http%3a%2f%2fschemas.xmlsoap.org%2fws%2f2005%2f05%2fidentity%2fclaims%2fnameidentifier=94739d98-337b-4b99-9adc-06c8821a0ab2&http%3a%2f%2fschemas.microsoft.com%2faccesscontrolservice%2f2010%2f07%2fclaims%2fidentityprovider=https%3a%2f%2fdatamarket.accesscontrol.windows.net%2f&Audience=http%3a%2f%2fapi.microsofttranslator.com&ExpiresOn=1392371315&Issuer=https%3a%2f%2fdatamarket.accesscontrol.windows.net%2f&HMACSHA256=QRItBid32qHtoUHwyXA6MGvjRc7kDAeb9jNXpa0FUx8%3d

Request for help.
thanks
Reply all
Reply to author
Forward
0 new messages