Minimal Google Docs Java Client
This code demonstrates how to fetch docs via google docs java api. This example is usable with the gdata-java-client
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import com.google.gdata.client.GoogleService;
import com.google.gdata.client.GoogleAuthTokenFactory.UserToken;
import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.media.MediaSource;
public class MinimalGoogleDocsClient {
private static final String APPLICATION_NAME = "JavaGDataClientSampleAppV3.0";
public static final String SPREADSHEETS_SERVICE_NAME = "wise";
public DocsService service;
public GoogleService spreadsheetsService;
public void getSpreadSheet(String id) throws Exception {
UserToken docsToken = (UserToken) service.getAuthTokenFactory()
.getAuthToken();
UserToken spreadsheetsToken = (UserToken) spreadsheetsService
.getAuthTokenFactory().getAuthToken();
service.setUserToken(spreadsheetsToken.getValue());
URL url = new URL(
"http://spreadsheets.google.com/feeds/download/spreadsheets/Export?gid=0&exportFormat=csv&key="
+ id);
readUrl(url);
service.setUserToken(docsToken.getValue());
}
public void getDocument(String id) throws Exception {
URL url = new URL(
"http://docs.google.com/feeds/download/documents/Export?docID=document:"
+ id + "&exportFormat=txt");
readUrl(url);
}
public void readUrl(URL url) throws Exception {
MediaContent mc = new MediaContent();
mc.setUri(url.toString());
MediaSource ms = service.getMedia(mc);
BufferedReader in = new BufferedReader(new InputStreamReader(ms
.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
}
public void go() throws Exception {
String user = "YOURUSERNAME";
String pass = "YOURPASS";
service = new DocsService(APPLICATION_NAME);
spreadsheetsService = new GoogleService(SPREADSHEETS_SERVICE_NAME,
APPLICATION_NAME);
service.setUserCredentials(user, pass);
spreadsheetsService.setUserCredentials(user, pass);
getSpreadSheet("0ApGHrBnaasfKdDJSaUc0TSEzcmZXb0Z5MU9JN3ZuUXc");
getDocument("0AZGHrBnasfsasKZGQ1OXdqbTafsasfWYydHE5OWRk");
}
public static void main(String[] args) throws Exception {
new MinimalGoogleDocsClient().go();
}
}