Hey everyone! I’m working on an Android app and I’ve got the auth token for Google Docs using AccountManager. Now I’m stuck trying to get all the user’s documents. I’ve looked at different libraries like Google-Api-Java-Client, but I’m confused about how to use them. The Picasa sample app didn’t help much either.
Does anyone have experience with this? I’d really appreciate some guidance on how to retrieve the list of Google Docs files. Maybe there’s a simple way I’m missing? Or if you’ve used any specific library successfully, I’d love to hear about it.
Here’s a basic code snippet of what I’ve tried so far:
String authToken = AccountManager.get(context).getAuthToken(account, "writely", null, activity, null, null).getResult().getString(AccountManager.KEY_AUTHTOKEN);
// Now what? How do I use this token to get the document list?
Any tips or code examples would be super helpful. Thanks in advance!
As someone who’s worked extensively with Google APIs, I can tell you that the Google Drive API is indeed the way to go for fetching Google Docs files. However, I’ve found that using the REST API directly can sometimes be simpler and more flexible than using the Java client library.
Here’s a quick snippet that’s worked well for me:
String url = "https://www.googleapis.com/drive/v3/files?q=mimeType='application/vnd.google-apps.document'&fields=files(id,name)";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", "Bearer " + authToken);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// Parse JSON response and process files
This approach gives you more control over the HTTP requests and can be easier to debug. Just remember to handle exceptions and implement proper threading for network operations on Android.
I’ve tackled this issue before, and I found that using the Google Drive API is the way to go for fetching Google Docs files. You’ll need to add the Google Drive API client library to your project.
Here’s a basic approach:
Set up Google Drive API in your Google Cloud Console.
Add the necessary dependencies to your gradle file.
Initialize the Drive service using your auth token.
Use the Files.list() method to retrieve the documents.
The key is to filter the query for Google Docs file type. Something like:
String pageToken = null;
do {
FileList result = driveService.files().list()
.setQ("mimeType='application/vnd.google-apps.document'")
.setSpaces("drive")
.setFields("nextPageToken, files(id, name)")
.setPageToken(pageToken)
.execute();
for (File file : result.getFiles()) {
// Process each file
}
pageToken = result.getNextPageToken();
} while (pageToken != null);
This should get you started. Remember to handle exceptions and implement proper error checking.