Guest User

Untitled

a guest
Sep 12th, 2018
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. Google Docs: Cannot export/download user's document using administrative access/impersonation (forbidden 403) in C#
  2. using System.IO;
  3. using Google.GData.Documents;
  4. using Google.GData.Client;
  5. using Google.GData.Extensions;
  6. using Google.Documents;
  7.  
  8.  
  9. void impersonateAndGetAllUsersDocs()
  10. {
  11. string applicationName = "myapp";
  12. string username = "admin@domain.com";
  13. string password = "adminPassword";
  14. string accountToImpersonate = "someOtherUser@domain.com";
  15.  
  16. DocRequest = new DocumentsRequest(new RequestSettings(applicationName, username, password));
  17.  
  18. DocumentsListQuery docQuery = new DocumentsListQuery();
  19. docQuery.Uri = new Uri(docQuery.Uri.ToString().Replace("/default/", "/" + accountToImpersonate + "/"));
  20.  
  21. AtomFeed docFeed = DocRequest.Service.Query(query);
  22. //process every document in the feed
  23. foreach (AtomEntry docEntry in docFeed.Entries)
  24. {
  25. //this line works for all docs (irrespective of who the author is)
  26. string title = docEntry.Title.Text;
  27.  
  28. Document doc = new Document()
  29. {
  30. AtomEntry = docEntry;
  31. };
  32.  
  33. //the next line throws an exception with HTTP 401 or 403 (permission) if the author is not the impersonated account
  34. Stream queryStream = DocRequest.Download(doc, Document.DownloadType.html)
  35.  
  36. //do something with the stream, such as write to local disk
  37.  
  38. //all done, close the stream
  39. if (queryStream != null)
  40. queryStream.Close();
  41. }
  42. }
Add Comment
Please, Sign In to add comment