Using the Google Docs Java API with a Google Apps account, can I impersonate a user and upload a file?
When I run the program below, it clearly enters the domain and impersonates the user, because he extracts the details of one of the files and prints the name of the file. However, when it tries to load the file, a ServiceForbiddenException is thrown.
If this is not possible in the Java API, does anyone know if my program can write an HTTP request to upload a file using the protocol API?
public class AuthExample { private static DocsService docService = new DocsService("Auth Example"); public static void main(String[] args) throws Exception { String adminUser = args[0]; String adminPassword = args[1]; String authToken = args[2]; String impersonatedUser = args[3]; loginToDomain(adminUser, adminPassword, authToken); URL url = new URL( "https://docs.google.com/feeds/" + impersonatedUser + "/private/full" ); DocumentListFeed feed = docService.getFeed(url, DocumentListFeed.class); DocumentListEntry entry = feed.getEntries().get(0); String title = entry.getTitle().getPlainText(); System.out.println( title ); String type = entry.getType(); if ( type.equals("document") ) { String encodedAdminUser = URLEncoder.encode(adminUser); String resourceId = entry.getResourceId(); String resourceIdNoPrefix = resourceId.substring( resourceId.indexOf(':')+1 ); String downloadUrl = "https://docs.google.com/feeds/download/documents/Export" + "?xoauth_requestor=" + encodedAdminUser + "&docId=" + resourceIdNoPrefix + "&exportFormat=doc"; downloadFile( downloadUrl, title + ".doc" ); } } private static void loginToDomain(String adminUser, String adminPassword, String authToken) throws OAuthException, AuthenticationException { String domain = adminUser.substring( adminUser.indexOf('@')+1 ); GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters(); oauthParameters.setOAuthConsumerKey(domain); oauthParameters.setOAuthConsumerSecret(authToken); oauthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH); oauthParameters.setScope("https://docs.google.com/feeds/ http://spreadsheets.google.com/feeds/ http://docs.googleusercontent.com/"); docService.useSsl(); docService.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer()); docService.setUserCredentials(adminUser, adminPassword); }
}
source share