Introduction
This is the code from our original article illustrating how to perform 3-legged OAuth authentication in order to perform the customerID operation in Google Apps Admin Audit API. The customerId is necessary for subsequent API calls. In addition, the code parses the JSON output received by API in order to isolate the customerId value.
Using the code
You need to download the google-api-java-client libraries in order to use this code.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow.Builder;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class CustomerIdSample {
private static String customerIdOperationUrl =
"https://apps-apis.google.com/a/feeds/customer/2.0/customerId?alt=json";
private static String customerIdOperationScope =
"https://apps-apis.google.com/a/feeds/policies/";
private static String clientID = "FIXME";
private static String clientSecret = "FIXME";
private static String redirectUri = "urn:ietf:wg:oauth:2.0:oob";
final class CustomerIdResponse { String version; String encoding; Entry entry; }
final class Entry { String xmlns; String xmlns$apps; Id id;
Updated updated; Link[] link; AppsProperty[] apps$property; }
final class Id { String $t; }
final class Updated { String $t; }
final class Link { String rel; String type; String href; }
final class AppsProperty { String name; String value; }
public static void main(String[] args) {
try {
try {
List<string> scopeList = Arrays
.asList(customerIdOperationScope);
Credential credential = buildCredential(clientID, clientSecret,
redirectUri, scopeList);
final HttpTransport HTTPTRANSPORT = new NetHttpTransport();
HttpRequestFactory rf = HTTPTRANSPORT
.createRequestFactory(credential);
HttpRequest request = rf.buildGetRequest(new GenericUrl(
customerIdOperationUrl));
HttpResponse response = request.execute();
BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
System.out.println(parseCustomerId(builder.toString()));
return;
} catch (IOException e) {
System.err.println(e.getMessage());
}
} catch (Throwable t) {
t.printStackTrace();
}
System.exit(1);
}
private static String parseCustomerId(String json) {
GsonBuilder builder = new GsonBuilder();
Gson gson = builder.create();
CustomerIdResponse customerIdResponse = gson.fromJson(json,
CustomerIdResponse.class);
return customerIdResponse.entry.apps$property[1].value;
}
private static Credential buildCredential(String client_id,
String client_secret, String redirect_url, Iterable<string> scopes) {
String code;
GoogleTokenResponse tokenResponse;
Credential cred = null;
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport httpTransport = new NetHttpTransport();
Builder flowBuilder = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, client_id, client_secret, scopes);
GoogleAuthorizationCodeFlow flow = flowBuilder.setAccessType("online")
.setApprovalPrompt("auto").build();
String url = flow.newAuthorizationUrl().setRedirectUri(redirectUri)
.build();
System.out.println("AUTHORIZATION URL: " + url);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out
.println("Open the URL above and paste the code (Access Token) here.");
try {
code = br.readLine();
tokenResponse = flow.newTokenRequest(code)
.setRedirectUri(redirect_url).execute();
cred = flow.createAndStoreCredential(tokenResponse, null);
} catch (IOException e) {
e.printStackTrace();
}
return cred;
}
}
History
- 06/24/2012 - Initial submission.