Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sample code for retrieving an Oauth token for use with Bing translation APIs.
import java.util.List;
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 = "";
private static final String clientId = "-->YOUR_CLIENT_ID_HERE<--";
private static final String clientSecret = "-->YOUR_CLIENT_SECRET_HERE<--";
private static final String oauthScope = "";
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());
// 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()));
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 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