Skip to content

Instantly share code, notes, and snippets.

@phyous
Created March 4, 2013 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phyous/5079099 to your computer and use it in GitHub Desktop.
Save phyous/5079099 to your computer and use it in GitHub Desktop.
Sample code for retrieving an Oauth token for use with Bing translation APIs. http://msdn.microsoft.com/en-us/library/ff512421.aspx
import java.io.InputStreamReader;
import java.net.URI;
import java.util.List;
import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.io.CharStreams;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.simple.JSONObject;
/*
Thread safe singleton wrapper for Bing oauth token
*/
public class BingOauth {
private static final int oauthTimeout = 10000;
private static final String oauthEndpoint = "/v2/OAuth2-13/";
private static final String oauthServerName = "datamarket.accesscontrol.windows.net";
private static final String clientId = "-->YOUR_CLIENT_ID_HERE<--";
private static final String clientSecret = "-->YOUR_CLIENT_SECRET_HERE<--";
private static final String oauthScope = "http://api.microsofttranslator.com";
private static final String oauthGrantType = "client_credentials";
private static String oauthToken = null;
/**
* Thread safe lazy initializer for oauth token
*/
public static String getOauthToken() {
if (oauthToken == null) {
synchronized (BingOauth.class) {
if (oauthToken == null) {
oauthToken = processOauthToken();
}
}
}
return oauthToken;
}
private static String processOauthToken() {
String token = "";
try {
// Create request
final HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, oauthTimeout);
HttpClient client = new DefaultHttpClient(httpParams);
HttpPost post = new HttpPost(getOauthUri());
post.setEntity(getOauthBody());
// Parse response
HttpResponse response = client.execute(post);
InputStreamReader reader = new InputStreamReader(response.getEntity().getContent(), Charsets.UTF_8);
String responseBody = CharStreams.toString(reader);
token = (new JSONObject(responseBody)).getString("access_token");
} catch (Exception e) {
System.out.println(String.format("Error processing oauth token:\n%s", e.toString()));
e.printStackTrace();
}
return token;
}
private static URI getOauthUri() throws Exception {
return new URI("https", oauthServerName, oauthEndpoint, null);
}
private static UrlEncodedFormEntity getOauthBody() throws Exception {
List<NameValuePair> nameValuePairs = Lists.newArrayListWithCapacity(4);
nameValuePairs.add(new BasicNameValuePair("client_id", clientId));
nameValuePairs.add(new BasicNameValuePair("client_secret", clientSecret));
nameValuePairs.add(new BasicNameValuePair("scope", oauthScope));
nameValuePairs.add(new BasicNameValuePair("grant_type", oauthGrantType));
return new UrlEncodedFormEntity(nameValuePairs);
}
}
@JHrique
Copy link

JHrique commented Sep 28, 2014

How do you manage this " token = (new JSONObject(responseBody)).getString("access_token");"?

When I try that I get the error "constructor JSONObject.JSONObject(Map) is not applicable"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment