How to publish Google Docs in .NET?

2019-02-14 12:12发布

问题:

I'm using the google-gdata .NET library for integrating with Google Docs. It's working out pretty well, but I can't figure out how to use it to publish my documents for view-only link-based web access.

The basic idea behind this is to be able to upload a document to Google Docs, and be able to view that document in a web environment (outside of Google) without having to log in using a Google credential. As far as I can tell, the best way to do that would be publishing it via a secret link, as described in the help article linked above.

So does anyone know how to publish Google Docs programatically in .NET?

EDIT

As a point of reference, and as an illustration on the difference between changing the ACL on a document (i.e. making it public) and publishing the document per se (as the link above describes), these are the corresponding versions of the same document on Google Docs:

  • ACL public read access | http://bit.ly/HYAXBc
  • Published to the web | http://bit.ly/HN0rEn

回答1:

I just updated the .NET client library, now you can use the following code to publish a revision (it assumes entry is the DocumentEntry instance you want to publish):

RevisionQuery revisionQuery = new RevisionQuery(entry.RevisionDocument);
RevisionFeed revisions = service.Query(revisionQuery);

// TODO: choose the revision you want to publish, this sample selects the first one
RevisionEntry revision = (RevisionEntry)revisions.Entries[0];

revision.Publish = true;
revision.PublishAuto = true;

revision.Update();


回答2:

Your question has already been answered on this post which contains a Java code snippet. For .NET, you should be able to do something like that:

AclEntry aclEntry = new AclEntry();
aclEntry.Role = new AclRole("reader");
aclEntry.Scope = new AclScope(AclScope.SCOPE_DEFAULT);

// documentEntry is the document that you want to publish and service is an 
// authorized DocumentsService object.
AclEntry insertedEntry = service.Insert(
    new Uri(documentEntry.AccessControlList), aclEntry);