Java 言語ガイド

重要: このドキュメントは 2012 年以前に作成されました。このドキュメントで説明する認証オプション(OAuth 1.0、AuthSub、ClientLogin)は、2012 年 4 月 20 日をもって正式に非推奨となり、利用できなくなりました。できるだけ早く OAuth 2.0 に移行することをおすすめします。

Google Sites Data API を使用すると、クライアント アプリケーションから Google サイト内のコンテンツにアクセスし、コンテンツを公開、変更できます。 クライアント アプリケーションから、最近のアクティビティ リストのリクエスト、変更履歴の取得、添付ファイルのダウンロードを行うこともできます。

このガイドでは、Site Data API の機能に関する背景情報を説明するとともに、Java クライアント ライブラリを使用して API を操作する例も紹介します。クライアント ライブラリの設定については、Google Data Java クライアント ライブラリのスタートガイドをご覧ください。Java クライアント ライブラリが以前の Google サイト API とやり取りする際に使用する、基盤となるプロトコルについて詳しくは、プロトコル ガイドをご覧ください。

対象

このドキュメントは、Google Data Java クライアント ライブラリを使用して、Google サイトとやり取りするクライアント アプリケーションを作成するデベロッパーを対象としています。

はじめに

Google サイトは、認証に Google アカウントまたは G Suite アカウントを使用します。すでにアカウントをお持ちの場合は、準備完了です。 お持ちでない場合は、新しいアカウントを作成できます。

ライブラリのインストール

クライアント ライブラリの設定とインストールについては、Google Data Java クライアント ライブラリのスタートガイドをご覧ください。Eclipse を使用している場合、この記事では Google Data API Eclipse プラグインを使ってプロジェクトをセットアップする方法について説明しています。ご利用を開始するには、以下をご確認ください。

  1. Java 1.5 以降をインストールする
  2. クライアント ライブラリをダウンロードするgdata-src.java.zip の最新バージョン)
  3. 依存関係のリストをダウンロードする
  4. サンプル アプリケーションのダウンロード(gdata-samples.java.zip の最新バージョン)

.jar をインストールしたら、プロジェクトに以下を含める必要があります。

  1. java/lib/gdata-sites-2.0.jar - バージョン 2.0 は、以前の Google サイト API のバージョン 1.4 を対象としています。
  2. java/lib/gdata-core-1.0.jar
  3. java/lib/gdata-client-1.0.jar
  4. java/lib/gdata-spreadsheet-3.0.jar(リストページ / リストアイテムを扱う場合)

また、依存関係 JAR(gdata-media-1.0.jarmail.jargoogle-collect....jar)を必ず含めてください。

サンプル アプリケーションの実行

完全に機能するサンプル アプリケーションは、gdata-samples.java.zip ダウンロードの /java/sample/sites サブディレクトリにあります。ソースは、[ソース] タブからアクセスできる SVN リポジトリの /trunk/java/sample/sites/ でも入手できます。SitesDemo.java を使用すると、さまざまな操作を行うことができます。これは、以前の Google サイトの API の使用方法を示すものです。

サンプルを実行するには、java/sample/util/lib/sample-util.jar を含める必要があります。

自分のプロジェクトを開始する

ヒント: Eclipse プラグインの簡単なセットアップについては、Eclipse と Google Data API の使用に関する記事をご覧ください。

アプリケーションのニーズに応じて、複数のインポートが必要になります。次のインポートから始めることをおすすめします。

import com.google.gdata.client.*;
import com.google.gdata.client.sites.*;
import com.google.gdata.data.*;
import com.google.gdata.data.acl.*;
import com.google.gdata.data.media.*;
import com.google.gdata.data.sites.*;
import com.google.gdata.data.spreadsheet.*;  // If working with listpages / listitems
import com.google.gdata.util.*;

次に、以前の Google サイト API へのクライアント接続を表す SitesService オブジェクトも設定する必要があります。

SitesService client = new SitesService("yourCo-yourAppName-v1");

applicationName 引数は、company-applicationname-version の形式にする必要があります。このパラメータはロギングに使用されます。

: このガイドの残りの部分では、変数 clientSitesService を作成していることを前提としています。

以前の Google サイト API の認証

Java クライアント ライブラリは、公開フィードと非公開フィードのどちらでも使用できます。Site Data API では、サイトの権限と実行するオペレーションに応じて、非公開フィードと公開フィードにアクセスできます。たとえば、公開サイトのコンテンツ フィードを読み取ることはできても、更新は行えない場合があります。そのためには、認証済みのクライアントが必要になります。これは、ClientLogin のユーザー名とパスワード認証、AuthSub、または OAuth で行うことができます。

AuthSub、OAuth、ClientLogin について詳しくは、Google Data API の認証の概要をご覧ください。

ヒント: API は SSL(HTTPS)をサポートしています。AuthSub/OAuth を使用している場合は、SSL 経由でフィードをリクエストするために、https://sites.google.com/feeds/ のスコープを指定してください。また、G Suite ドメインの場合、管理コントロール パネルの [SSL を必須にする] 設定が API で適用されます。client.useSsl(); を呼び出すと、すべての API リクエストを強制的に HTTPS で接続できます。

ウェブ アプリケーション用の AuthSub

ウェブ アプリケーション用の AuthSub 認証は、Google アカウントでユーザーを認証する必要があるクライアント アプリケーションで使用する必要があります。オペレーターは Google サイト ユーザーのユーザー名とパスワードにアクセスする必要はありません。必要なのは AuthSub トークンのみです。

AuthSub をウェブ アプリケーションに組み込む手順を確認する

1 回限りのトークンをリクエストする

ユーザーは、初めてアプリケーションにアクセスしたときに認証を受ける必要があります。通常、デベロッパーは、ユーザーを認証してドキュメントへのアクセスをリクエストするために、ユーザーを AuthSub 承認ページに誘導するテキストとリンクを出力します。Google Data Java クライアント ライブラリには、この URL を生成する関数が用意されています。以下のコードは、AuthSubRequest ページへのリンクをセットアップしています。

import com.google.gdata.client.*;

String nextUrl = "http://www.example.com/welcome.jsp";
String scope = "https://sites.google.com/feeds/";
boolean secure = true;
boolean session = true;
String authSubUrl = AuthSubUtil.getRequestUrl(nextUrl, scope, secure, session);

G Suite でホストされているドメインでユーザーを認証する場合:

import com.google.gdata.client.*;

String hostedDomain = "example.com";
String nextUrl = "http://www.example.com/welcome.jsp";
String scope = "https://sites.google.com/feeds/";  // SSL is also supported
boolean secure = true;
boolean session = true;
String authSubUrl = AuthSubUtil.getRequestUrl(hostedDomain, nextUrl, scope, secure, session);

getRequestUrl() メソッドは、いくつかのパラメータ(AuthSubRequest ハンドラで使用されるクエリ パラメータに対応)を受け取ります。

  • 次の URL - ユーザーがアカウントにログインしてアクセスを許可した後に Google がリダイレクトする URL。上記の例では http://www.example.com/welcome.jsp
  • スコープ - 上記の例では https://sites.google.com/feeds/
  • トークンが登録モードで使用されるかどうかを示すブール値。上記の例では false
  • 後でトークンがセッション トークンと交換されるかどうかを示す 2 つ目のブール値。上記の例では true

セッション トークンへのアップグレード

Google Data API クライアント ライブラリで AuthSub を使用するをご覧ください。

セッション トークンに関する情報の取得

Google Data API クライアント ライブラリで AuthSub を使用するをご覧ください。

セッション トークンの取り消し

Google Data API クライアント ライブラリで AuthSub を使用するをご覧ください。

ウェブ用またはインストール型/モバイルアプリ用の OAuth

OAuth は AuthSub の代替手段として使用できる、ウェブ アプリケーションを対象としています。 OAuth では、すべてのデータ リクエストにデジタル署名され、ドメインを登録する必要がある点で、AuthSub のセキュアな登録モードを使用する場合と類似しています。

インストールしたアプリケーションに OAuth を組み込む手順を表示

リクエスト トークンの取得

Google Data API クライアント ライブラリでの OAuth の使用をご覧ください。

リクエスト トークンを承認する

Google Data API クライアント ライブラリでの OAuth の使用をご覧ください。

アクセス トークンへのアップグレード

Google Data API クライアント ライブラリでの OAuth の使用をご覧ください。

インストール型アプリケーションまたはモバイル アプリケーション用の ClientLogin

ClientLogin は、Google アカウントでユーザーを認証する必要があるインストール済みアプリケーションまたはモバイルアプリで使用する必要があります。アプリケーションの初回実行時に、ユーザー名とパスワードの入力を求められます。後続のリクエストでは、認証トークンが参照されます。

ClientLogin をインストールしたアプリケーションに組み込む手順を表示する

ClientLogin を使用するには、GoogleService から継承される SitesService オブジェクトの setUserCredentials() メソッドを呼び出します。クライアントが代理でリクエストを行うユーザーのメールアドレスとパスワードを指定します。例:

SitesService client = new SitesService("yourCo-yourAppName-v1");
client.setUserCredentials("example@gmail.com", "pa$$word");

ヒント: アプリケーションで初めてユーザーの認証に成功したら、後で使用できるように認証トークンをデータベースに保存します。アプリを実行するたびにユーザーにパスワードの入力を求める必要はありません。詳しくは、認証トークンの再現をご覧ください。

Java アプリケーションで ClientLogin を使用する方法について詳しくは、Google Data API クライアント ライブラリでの ClientLogin の使用をご覧ください。

トップへ戻る

サイトフィード

サイトフィードは、ユーザーが所有する、または閲覧権限のある Google サイトをリストするために使用できます。既存のサイト名を変更する場合にも使用できます。G Suite ドメインの場合は、サイト全体を作成またはコピーするためにも使用できます。

サイトの一覧表示

サイトフィードに対してクエリを実行するには、HTTP GET をサイトフィードの URL に送信します。

https://sites.google.com/feeds/site/site/

Java クライアントでは、SiteFeed クラスと SiteEntry クラスを使用してサイトフィードを操作できます。

public String getSiteFeedUrl() {
  String domain = "site";  // OR if the Site is hosted on G Suite, your domain (e.g. example.com)
  return "https://sites.google.com/feeds/site/" + domain + "/";
}

public void getSiteFeed() throws IOException, ServiceException {
  SiteFeed siteFeed = client.getFeed(new URL(getSiteFeedUrl()), SiteFeed.class);
  for (SiteEntry entry : siteFeed.getEntries()){
    System.out.println("title: " + entry.getTitle().getPlainText());
    System.out.println("site name: " + entry.getSiteName().getValue());
    System.out.println("theme: " + entry.getTheme().getValue());
    System.out.println("");
  }
}

上記のスニペットでは、サイトのタイトル、サイト名、サイトのテーマが出力されます。フィード内の追加のプロパティにアクセスするためのゲッターは他にもあります。

新しいサイトを作成する

: この機能は G Suite ドメインでのみご利用いただけます。

新しいサイトをプロビジョニングするには、新しい SiteEntry を作成し、サイトフィードでクライアントの insert() メソッドを呼び出します。

この例では、テーマ「slate」(オプションの設定)を使用して新しいサイトを作成し、サイト名(必須)と説明(省略可)を指定します。

public String getSiteFeedUrl() {
  String domain = "example.com";
  return "https://sites.google.com/feeds/site/" + domain + "/";
}

public SiteEntry createSite(String title, String summary, String theme, String tag)
    throws MalformedURLException, IOException, ServiceException {
  SiteEntry entry = new SiteEntry();
  entry.setTitle(new PlainTextConstruct(title));
  entry.setSummary(new PlainTextConstruct(summary));

  Theme tt = new Theme();
  tt.setValue(theme);
  entry.setTheme(tt);

  entry.getCategories().add(new Category(TagCategory.Scheme.TAG, tag, null));

  return client.insert(new URL(getSiteFeedUrl()), entry);
}

SiteEntry newSiteEntry = createSite("My Site Title", "summary for site", "slate", "tag");

上記のリクエストでは、G Suite ドメイン example.com に新しいサイトが作成されます。 この場合、サイトの URL は https://sites.google.com/a/example.com/my-site-title となります。

サイトが正常に作成されると、サーバーは SiteEntry オブジェクトを返します。このオブジェクトには、サーバーによって追加された要素(サイトへのリンク、サイトの ACL フィードへのリンク、サイト名、タイトル、サマリーなど)が入力されます。

サイトをコピーする

: この機能は G Suite ドメインでのみご利用いただけます。

サイトのコピーは新しいサイトの作成と似ています。違いは、複製するサイトのセルフリンクを含むリンクを新しい SiteEntry に設定する必要があります。以下は、新しいサイトを作成するで作成したサイトを複製した例です。

public SiteEntry copySite(String title, String summary, String sourceHref)
    throws MalformedURLException, IOException, ServiceException {
  SiteEntry entry = new SiteEntry();
  entry.setTitle(new PlainTextConstruct(title));
  entry.setSummary(new PlainTextConstruct(summary));
  entry.addLink(SitesLink.Rel.SOURCE, Link.Type.ATOM, sourceHref);

  return client.insert(new URL(getSiteFeedUrl()), entry);
}

String sourceHref = newSiteEntry.getLink(SitesLink.Rel.SOURCE, Link.Type.ATOM).getHref();
SiteEntry myTwin = copySite("Duplicate Site", "A copy", sourceHref);

注意事項:

  • コピーできるのは、認証済みのユーザーが所有するサイトとサイト テンプレートのみです。
  • サイト テンプレートをコピーすることもできます。Google サイトの設定ページで [このサイトをテンプレートとして公開する] 設定がオンになっている場合、サイトはテンプレートです。
  • ソースサイトで所有者として登録されている間は、別のドメインからサイトをコピーできます。

サイトのメタデータの更新

サイト名の変更、サイトのテーマ、カテゴリタグ、概要の変更を行うには、まず対象のサイトを含む SiteEntry を取得し、1 つ以上のプロパティを変更してから、SiteEntryupdate() メソッドを呼び出す必要があります。この例では、前のサイトのテーマを変更し、サイトの名前を変更します。

myTwin.setTitle(new PlainTextConstruct("better-title"));

Theme theme = myTwin.getTheme();
theme.setValue('iceberg');
myTwin.setTheme(theme);

myTwin.getCategories().add(new Category(TagCategory.Scheme.TAG, "newTag", null));

SiteEntry updatedSiteEntry = myTwin.update();

System.out.println(updatedSiteEntry.getTitle().getPlainText();

ウェブアドレスのマッピング

ウェブアドレスのマッピングを使用すると、Google サイトのユーザーは自分のドメインを Google サイトにマッピングできます。たとえば、http://sites.google.com/a/domain.com/mysite の代わりに http://www.mydomainsite.com を使用できます。サイトがホストされている場所に応じて、サイトのウェブアドレスのマッピングを手動で変更できます。詳しくは、ヘルプセンターの記事をご覧ください。

サイトのウェブアドレスのマッピングの取得

サイトのウェブアドレスのマッピングを返すには、with-mappings=true パラメータを使用してサイトエントリまたはフィードを取得します。

SiteQuery query = new SiteQuery(new URL("https://sites.google.com/feeds/site/siteName"));
query.setWithMappings(true);

SiteFeed feed = service.getFeed(query, SiteFeed.class);
for (SiteEntry entry : feed.getEntries()) {
  System.out.println("Mappings for '" + entry.getSiteName().getValue() + "':");
  for (Link link : entry.getWebAddressMappingLinks()) {
    System.out.println("  " + link.getHref());
  }
}

既存のマッピングは、rel='webAddressMapping' が指定された link として表示されます。たとえば、上記の例では、サイト http://sites.google.com/site/myOtherTestSite を指す 3 つの webAddressMapping があります。

ウェブアドレスのマッピングの変更

: すべての GET/POST/PUT オペレーションで、ウェブアドレス マッピングを操作するときは with-mappings=true パラメータを指定する必要があります。パラメータが存在しない場合、webAddressMapping はサイトエントリで返されません(GET)。また、エントリからのマッピングの更新または削除(PUT)の際にも考慮されません。

マッピングを追加、更新、削除するには、新しいサイトの作成時またはサイトのメタデータの更新時に、そのようなリンクを指定、変更、削除します。サイトフィードの URI に with-mappings=true パラメータを含める必要があります。 注: アドレスのマッピングを更新するには、サイト管理者か、ドメイン管理者(G Suite でホストされているサイトの場合)の権限が必要です。

たとえば、次のリクエストでは、http://www.mysitemapping.com マッピングを http://www.my-new-sitemapping.com に更新し、リンクをエントリから外すことで http://www.mysitemapping2.com を削除します。

SiteEntry entry = client.getEntry(new URL("https://sites.google.com/feeds/site/site/siteName?with-mappings=true"), SiteEntry.class);

// Modify mappings (remove all mappings, add some of them again, add modified mappings)
entry.removeLinks(SitesLink.Rel.WEBADDRESSMAPPING, Link.Type.HTML);
entry.addLink(SitesLink.Rel.WEBADDRESSMAPPING, Link.Type.HTML, "http://www.my-new-sitemapping.com");

// Update the entry with the mappings.
entry.update();

ウェブアドレスのマッピングは、サイトの作成時またはコピー時にも指定できます。

トップへ戻る

アクティビティ フィード

サイトの最近のアクティビティ(変更)を取得するには、アクティビティ フィードを取得します。アクティビティ フィードの各エントリには、サイトに加えられた変更に関する情報が含まれています。

アクティビティ フィードをクエリするには、HTTP GET をアクティビティ フィードの URL に送信します。

https://sites.google.com/feeds/activity/site/siteName

Java クライアントで、ActivityFeed クラスを使用して ActivityEntry オブジェクトを返します。

public String buildActivityFeedUrl() {
  String domain = "site";  // OR if the Site is hosted on G Suite, your domain (e.g. example.com)
  String siteName = "mySite";
  return "https://sites.google.com/feeds/activity/" + domain + "/" + siteName + "/";
}

public void getActivityFeed() throws IOException, ServiceException {
  ActivityFeed activityFeed = client.getFeed(new URL(buildActivityFeedUrl()), ActivityFeed.class);
  for (BaseActivityEntry<?> entry : activityFeed.getEntries()){
    System.out.println(entry.getSummary().getPlainText());
    System.out.println(" revisions link: " + entry.getRevisionLink().getHref());
  }
}

: このフィードにアクセスするには、サイトの共同編集者またはオーナーである必要があります。 クライアントは、AuthSub、OAuth、ClientLogin の各トークンを使用して認証を行う必要があります。Google サイトのサービスの認証をご覧ください。

トップへ戻る

リビジョン フィード

コンテンツ エントリの変更履歴を取得するには、HTTP GET をエントリのリビジョン リンクに送信します。

https://sites.google.com/feeds/revision/site/siteName/CONTENT_ENTRY_ID

この例では、コンテンツ フィードに対してクエリを実行し、最初のコンテンツ エントリのリビジョン フィードを取得します。

ContentFeed contentFeed = client.getFeed(new URL(buildContentFeedUrl()), ContentFeed.class);
URL revisionFeedUrl = new URL(contentFeed.getEntries().get(0).getRevisionLink().getHref()); // use first entry

public void getRevisionFeed(String revisionFeedUrl) throws IOException, ServiceException {
  RevisionFeed revisionFeed = client.getFeed(revisionFeedUrl, RevisionFeed.class);
  for (BaseContentEntry<?> entry : revisionFeed.getEntries()){
    System.out.println(entry.getTitle().getPlainText());
    System.out.println(" updated: " + entry.getUpdated().toUiString() + " by " +
        entry.getAuthors().get(0).getEmail());
    System.out.println(" revision #: " + entry.getRevision().getValue());
  }
}

注: このフィードにアクセスするには、サイトの共同編集者またはオーナーである必要があります。 クライアントは、AuthSub、OAuth、ClientLogin の各トークンを使用して認証を行う必要があります。Google サイトのサービスの認証をご覧ください。

トップへ戻る

コンテンツ フィード

コンテンツ フィードの取得

コンテンツ フィードには、サイトの最新コンテンツが表示されます。HTTP GET をコンテンツ フィードの URL に送信することでアクセスできます。

https://sites.google.com/feeds/content/site/siteName
フィード パラメータ説明
sitesite」、または G Suite でホストされているドメイン(例: example.com)です。
siteNameサイトのウェブスペース名。サイトの URL に含まれています(例: mySite)。

コンテンツ フィードの取得例:

public String buildContentFeedUrl() {
  String domain = "site";  // OR if the Site is hosted on G Suite, your domain (e.g. example.com)
  String siteName = "mySite";
  return "https://sites.google.com/feeds/content/" + domain + "/" + siteName + "/";
}

ContentFeed contentFeed = client.getFeed(new URL(buildContentFeedUrl()), ContentFeed.class);

結果として得られる contentFeed は、サーバーからのレスポンスを含む ContentFeed オブジェクトです。contentFeed の各エントリは、ユーザーのサイト内の異なるページまたはアイテムを表します。ContentFeed には、BaseContentEntry から継承されるさまざまなタイプのオブジェクト(ListItemEntryListPageEntryAttachmentEntryWebAttachmentEntryFileCabinetPageEntryAnnouncementsPageEntryAnnouncementEntryWebPageEntryCommentEntry)が含まれます。

ContentFeed のさまざまなエントリタイプを一覧表示する例を次に示します。各タイプのエントリには異なるプロパティが含まれていますが、すべてがここに出力されているわけではありません。

public String getContentBlob(BaseContentEntry<?> entry) {
 return ((XhtmlTextConstruct) entry.getTextContent().getContent()).getXhtml().getBlob();
}

// Extracts an entry's numeric ID.
private String getEntryId(String selfLink) {
  return selfLink.substring(selfLink.lastIndexOf("/") + 1);
}

public void printContentEntries(ContentFeed contentFeed) {
  System.out.println("Listing all WebPageEntry:");
  for (WebPageEntry entry : contentFeed.getEntries(WebPageEntry.class)) {
    System.out.println(" title: " + entry.getTitle().getPlainText());
    System.out.println(" id: " + getEntryId(entry));
    if (entry.getParentLink() != null) {
      System.out.println(" parent id: " + getEntryId(entry.getParentLink().getHref()));
    }
    System.out.println(" author: " + entry.getAuthors().get(0).getEmail());
    System.out.println(" content: " + getContentBlob(entry));
  }

  System.out.println("Listing all ListPageEntry:");
  for (ListPageEntry entry : contentFeed.getEntries(ListPageEntry.class)) {
    System.out.println(" title: " + entry.getTitle().getPlainText());
    System.out.println(" id: " + getEntryId(entry));
    for (Column col : entry.getData().getColumns()) {
      System.out.print(" [" + col.getIndex() + "] " + col.getName() + "\t");
    }
  }

  for (ListItemEntry entry : contentFeed.getEntries(ListItemEntry.class)) {
    for (Field field : entry.getFields()) {
      System.out.print(" [" + field.getIndex() + "] " + field.getValue() + "\t");
    }
    System.out.println("\n");
  }

  System.out.println("Listing all FileCabinetPageEntry:");
  for (FileCabinetPageEntry entry : contentFeed.getEntries(FileCabinetPageEntry.class)) {
    System.out.println(" title: " + entry.getTitle().getPlainText());
    System.out.println(" id: " + getEntryId(entry));
    System.out.println(" content: " + getContentBlob(entry));
  }

  System.out.println("Listing all CommentEntry:");
  for (CommentEntry entry : contentFeed.getEntries(CommentEntry.class)) {
    System.out.println(" in-reply-to: " + entry.getInReplyTo().toString());
    System.out.println(" content: " + getContentBlob(entry));
  }

  System.out.println("Listing all AnnouncementsPageEntry:");
  for (AnnouncementsPageEntry entry : contentFeed.getEntries(AnnouncementsPageEntry.class)) {
    System.out.println(" title: " + entry.getTitle().getPlainText());
    System.out.println(" id: " + getEntryId(entry));
    System.out.println(" content: " + getContentBlob(entry));
  }

  System.out.println("Listing all AnnouncementEntry:");
  for (AnnouncementEntry entry : contentFeed.getEntries(AnnouncementEntry.class)) {
    System.out.println(" title: " + entry.getTitle().getPlainText());
    System.out.println(" id: " + getEntryId(entry));
    if (entry.getParentLink() != null) {
      System.out.println(" parent id: " + getEntryId(entry.getParentLink().getHref()));
    }
    System.out.println(" draft?: " + entry.isDraft());
    System.out.println(" content: " + getContentBlob(entry));
  }

  System.out.println("Listing all AttachmentEntry:");
  for (AttachmentEntry entry : contentFeed.getEntries(AttachmentEntry.class)) {
    System.out.println(" title: " + entry.getTitle().getPlainText());
    System.out.println(" id: " + getEntryId(entry));
    if (entry.getParentLink() != null) {
      System.out.println(" parent id: " + getEntryId(entry.getParentLink().getHref()));
    }
    if (entry.getSummary() != null) {
      System.out.println(" description: " + entry.getSummary().getPlainText());
    }
    System.out.println(" revision: " + entry.getRevision().getValue());
    MediaContent content = (MediaContent) entry.getContent();
    System.out.println(" src: " + content.getUri());
    System.out.println(" content type: " + content.getMimeType().getMediaType());
  }

  System.out.println("Listing all WebAttachmentEntry:");
  for (WebAttachmentEntry entry : contentFeed.getEntries(WebAttachmentEntry.class)) {
    System.out.println(" title: " + entry.getTitle().getPlainText());
    System.out.println(" id: " + getEntryId(entry));
    if (entry.getParentLink() != null) {
      System.out.println(" parent id: " + getEntryId(entry.getParentLink().getHref()));
    }
    if (entry.getSummary() != null) {
      System.out.println(" description: " + entry.getSummary().getPlainText());
    }
    System.out.println(" src: " + ((MediaContent) entry.getContent()).getUri());
  }
}

注: このフィードは、サイトの共有権限に応じて、認証が不要な場合もあります。 サイトが非公開の場合、クライアントは AuthSub、OAuth、または ClientLogin トークンを使用して認証する必要があります。Google サイトのサービスの認証をご覧ください。

コンテンツ フィードクエリの例

標準の Google Data API クエリ パラメータと、以前の Google サイト API に固有のパラメータを使用して、コンテンツ フィードを検索できます。サポートされているパラメータの詳細と一覧については、リファレンス ガイドをご覧ください。

: このセクションの例では、コンテンツ フィードを取得するで説明した buildContentFeedUrl() メソッドを使用しています。

特定のエントリの種類を取得する

特定のタイプのエントリのみを取得するには、kind パラメータを使用します。この例では、attachment エントリのみを返します。

ContentQuery query = new ContentQuery(new URL(buildContentFeedUrl()));
query.setKind("webpage");
ContentFeed contentFeed = client.getFeed(query, ContentFeed.class);
for (AttachmentEntry entry : contentFeed.getEntries(AttachmentEntry.class)) {
  System.out.println(entry.getTitle().getPlainText());
}

複数のエントリタイプを返すには、各 kind を「,」で区切ります。この例では、filecabinet エントリと listpage エントリを返します。

URL url = new URL(buildContentFeedUrl() + "?kind=filecabinet,listpage");
ContentFeed contentFeed = client.getFeed(url, ContentFeed.class);
for (FileCabinetPageEntry entry : contentFeed.getEntries(FileCabinetPageEntry.class)) {
  System.out.println(" title: " + entry.getTitle().getPlainText());
}
for (ListPageEntry entry : contentFeed.getEntries(ListPageEntry.class)) {
  System.out.println(" title: " + entry.getTitle().getPlainText());
}

パスによるページの取得

Google サイト内のページの相対パスがわかっている場合は、path パラメータを使用してその特定のページを取得できます。 この例では、http://sites.google.com/site/siteName/path/to/the/page にあるページが返されます。

ContentQuery query = new ContentQuery(new URL(buildContentFeedUrl()));
query.setPath("/path/to/the/page");
ContentFeed contentFeed = client.getFeed(query, ContentFeed.class);
for (BaseContentEntry<?> entry : contentFeed.getEntries()) {
  System.out.println(" title: " + entry.getTitle().getPlainText());
}

親ページ内のすべてのエントリを取得する

ページのコンテンツ エントリ ID(次の例の「1234567890」など)がわかっている場合は、parent パラメータを使用してその子エントリ(存在する場合)をすべてフェッチできます。

ContentQuery query = new ContentQuery(new URL(buildContentFeedUrl()));
query.setParent("1234567890");
ContentFeed contentFeed = client.getFeed(query, ContentFeed.class);

その他のパラメータについては、リファレンス ガイドをご覧ください。

トップへ戻る



コンテンツの作成

注: サイトのコンテンツを作成する前に、クライアントでサイトを設定していることを確認してください。
client.site = "siteName";

新しいコンテンツ(ウェブページ、リストページ、ファイルキャビネット ページ、お知らせページなど)を作成するには、HTTP POST をコンテンツ フィードに送信します。

https://sites.google.com/feeds/content/site/siteName

サポートされるノードタイプの一覧については、リファレンス ガイドkind パラメータをご覧ください。

新しいアイテムやページの作成

次の例では、サイトのトップレベルに新しい webpage を作成し、ページ本文に XHTML をいくつか組み込んで、見出しのタイトルを「New WebPage Title」に設定しています。

private void setContentBlob(BaseContentEntry<?> entry, String pageContent) {
  XmlBlob xml = new XmlBlob();
  xml.setBlob(pageContent);
  entry.setContent(new XhtmlTextConstruct(xml));
}

public WebPageEntry createWebPage(String title, String content)
    throws MalformedURLException, IOException, ServiceException {
  WebPageEntry entry = new WebPageEntry();
  entry.setTitle(new PlainTextConstruct(title));

  setContentBlob(entry, content); // Entry's HTML content

  return client.insert(new URL(buildContentFeedUrl()), entry);
}

WebPageEntry createdEntry = createWebPage("New Webpage Title", "<b>HTML content</b>");
System.out.println("Created! View at " + createdEntry.getHtmlLink().getHref());

リクエストが成功した場合、createdEntry にはサーバー上に作成されたエントリのコピーが含まれます。

カスタム URL パスでのアイテム/ページの作成

デフォルトでは、上記の例は URL http://sites.google.com/site/siteName/new-webpage-title の下に作成され、ページ見出しは「新しいウェブページのタイトル」に設定されます。つまり、<atom:title> は URL に対して new-webpage-title に正規化されます。ページの URL パスをカスタマイズするには、<sites:pageName> 要素を設定します。

この例では、「File Storage」という見出しの新しい filecabinet ページを作成していますが、<sites:pageName> 要素を指定することで、http://sites.google.com/site/siteName/file-storage ではなく http://sites.google.com/site/siteName/files という URL にページを作成しています。

public FileCabinetPageEntry createFileCabinetPage(String title, String content, String customPageName)
    throws MalformedURLException, IOException, ServiceException {
  FileCabinetPageEntry entry = new FileCabinetPageEntry();
  entry.setTitle(new PlainTextConstruct(title));

  setContentBlob(entry, content); // Entry's HTML content

  entry.setPageName(new PageName(customPageName)); // Upload to a custom page path

  return client.insert(new URL(buildContentFeedUrl()), entry);
}

FileCabinetPageEntry createdEntry = createFileCabinetPage("File Storage", "<b>HTML content</b>", "files");
System.out.println("Created! View at " + createdEntry.getHtmlLink().getHref());

サーバーは、ページの URL パスに次の優先順位ルールを適用します。

  1. <sites:pageName>(存在する場合)。a-z, A-Z, 0-9, -, _ を満たす必要があります。
  2. <atom:title>。pageName が存在しない場合は null にできません。正規化では、空白文字は削除されて「-」になり、a-z, A-Z, 0-9, -, _ に一致しない文字は削除されます。

サブページを作成する

親ページの下にサブページ(子)を作成するには、エントリに親リンクを設定する必要があります。親ノードのセルフリンクに対するリンクの href 属性。

public AnnouncementEntry postAnnouncement(String title, String content, AnnouncementsPageEntry parentPage)
    throws MalformedURLException, IOException, ServiceException {
  AnnouncementEntry entry = new AnnouncementEntry();
  entry.setTitle(new PlainTextConstruct(title));

  setContentBlob(entry, content); // Entry's HTML content

  // Set the entry's parent link to create the announcement under that page.
  entry.addLink(SitesLink.Rel.PARENT, Link.Type.ATOM, parentPage.getSelfLink().getHref());

  return client.insert(new URL(buildContentFeedUrl()), entry);
}

ContentFeed contentFeed = client.getFeed(new URL(buildContentFeedUrl() + "?kind=announcementspage"), ContentFeed.class);

AnnouncementEntry createdEntry = postAnnouncement("Party!!", "My place, this weekend", contentFeed.getEntries().get(0));
System.out.println("New post by " + createdEntry.getAuthors().get(0).getName());

上記の例では、ユーザーのコンテンツ フィードにある最初のお知らせページに新しい announcement を作成します。通知のタイトルは「Party!!」に、コンテンツは「My place, this now」に設定されています。

ページ テンプレート

ページ テンプレートの作成

ページ テンプレートを作成するプロセスは、新しいアイテム/ページの作成サブページの作成と同じです。違いは、キーワードおよびラベルをそれぞれ「http://schemas.google.com/g/2005#template」と「template」に設定した category を追加する点です。

この例では、新しい webpage テンプレートを作成します。

// The template webpage entry.
WebPageEntry entry = new WebPageEntry();

// Set title and content.
entry.setTitle(new PlainTextConstruct("Page template title"));
XmlBlob xml = new XmlBlob();
xml.setBlob("Content for page template");
entry.setContent(new XhtmlTextConstruct(xml));

// Set the template category
Category TEMPLATE_CATEGORY = new Category(TemplateCategory.Scheme.LABELS,
    TemplateCategory.Term.TEMPLATE, TemplateCategory.Label.TEMPLATE);
entry.getCategories().add(TEMPLATE_CATEGORY);

// Insert the template webpage entry.
WebPageEntry createdEntry = client.insert(new URL("https://sites.google.com/feeds/content/site/siteName"), entry);

テンプレートからページを作成する

ページ テンプレートの作成と同様に、ページ テンプレートのセルフリンクを指す rel='http://schemas.google.com/sites/2008#template' を指定して <link> を含めることで、テンプレートから新しいページをインスタンス化できます。

この例では、新しい filecabinet テンプレートを作成し、そのテンプレートから新しい filecabinet ページをインスタンス化しています。

URL feedUrl = new URL("https://sites.google.com/feeds/content/site/siteName");

// 1. Create file cabinet page template
FileCabinetPageEntry inputTemplateEntry = new FileCabinetPageEntry();
inputTemplateEntry.setTitle(new PlainTextConstruct("File cabinet page template title"));
XmlBlob xml = new XmlBlob();
xml.setBlob("Content for page template");
inputTemplateEntry.setContent(new XhtmlTextConstruct(xml));

// Set the template category
Category TEMPLATE_CATEGORY = new Category(TemplateCategory.Scheme.LABELS,
    TemplateCategory.Term.TEMPLATE, TemplateCategory.Label.TEMPLATE);
inputTemplateEntry.getCategories().add(TEMPLATE_CATEGORY);

// 2. Create file cabinet page template instance
FileCabinetPageEntry templateEntry = client.insert(feedUrl, inputTemplateEntry);

// Specify link to the page template
FileCabinetPageEntry templateInstanceEntry = new FileCabinetPageEntry();
templateInstanceEntry.setTitle(new PlainTextConstruct("File cabinet template instance"));
templateInstanceEntry.addLink(new Link(SitesLink.Rel.TEMPLATE, Link.Type.ATOM, templateEntry.getSelfLink().getHref()));

FileCabinetPageEntry createdFileCabinetFromTemplate =  client.insert(feedUrl, templateInstanceEntry);

: <category> を定義するテンプレートがあるにもかかわらず、エントリにはテンプレートを含める必要があります。また、<content> 要素を追加すると、サーバーはその要素を拒否します。

ファイルのアップロード

Google サイトと同様に、API ではファイル キャビネット ページや親ページに添付ファイルをアップロードできます。

親に添付ファイルをアップロードするには、コンテンツ フィードの URL に HTTP POST リクエストを送信します。

https://sites.google.com/feeds/content/site/siteName

すべての種類の添付ファイルを親ページにアップロードする必要があります。したがって、アップロードしようとしている AttachmentEntry または WebAttachmentEntry オブジェクトに親リンクを設定します。詳しくは、サブページを作成するをご覧ください。

添付ファイルをアップロード中

この例では、ユーザーのコンテンツ フィードで最初に見つかった FileCabinetPageEntry に PDF ファイルをアップロードします。アタッチメントは、「Getting Started」というタイトルと(省略可能な)「HR より良い」というタイトルで作成されます。

MimetypesFileTypeMap mediaTypes = new MimetypesFileTypeMap();
mediaTypes.addMimeTypes("application/msword doc");
mediaTypes.addMimeTypes("application/vnd.ms-excel xls");
mediaTypes.addMimeTypes("application/pdf pdf");
mediaTypes.addMimeTypes("text/richtext rtx");
// ... See a more complete list of mime types in the SitesHelper.java

public AttachmentEntry uploadAttachment(File file, BasePageEntry<?> parentPage,
    String title, String description) throws IOException, ServiceException {
  AttachmentEntry newAttachment = new AttachmentEntry();
  newAttachment.setMediaSource(new MediaFileSource(file, mediaTypes.getContentType(file)));
  newAttachment.setTitle(new PlainTextConstruct(title));
  newAttachment.setSummary(new PlainTextConstruct(description));
  newAttachment.addLink(SitesLink.Rel.PARENT, Link.Type.ATOM, parentPage.getSelfLink().getHref());

  return client.insert(new URL(buildContentFeedUrl()), newAttachment);
}

ContentFeed contentFeed = client.getFeed(new URL(buildContentFeedUrl() + "?kind=filecabinet"), ContentFeed.class);
FileCabinetPageEntry parentPage = contentFeed.getEntries(FileCabinetPageEntry.class).get(0);

AttachmentEntry attachment = uploadAttachment(
    new File("/path/to/your/file.pdf"), parentPage, "Getting Started", "HR packet");
System.out.println("Uploaded!");

アップロードが成功すると、作成された添付ファイルのエントリのコピーが attachment に格納されます。

フォルダへの添付ファイルのアップロード

添付ファイルを FileCabinetPageEntry の既存のフォルダにアップロードするには、カテゴリを指定して「term」属性にフォルダ名を設定します。 たとえば、uploadAttachment() に次の行を追加します。

newAttachment.getCategories().add(new Category("http://schemas.google.com/sites/2008#folder", "FolderName"));

ウェブの添付ファイル

ウェブ添付ファイルは、特別な種類の添付ファイルです。基本的には、ファイル キャビネット リストに追加できるウェブ上の他のファイルへのリンクです。この機能は、Google サイト管理画面の [URL でファイルを追加] アップロード方法に類似しています。

: ウェブ添付ファイルはファイル キャビネットの中にのみ作成できます。他のタイプのページにはアップロードできません。

この例では、ユーザーのコンテンツ フィードで最初に見つかった FileCabinetPageEntry の下に WebAttachmentEntry を作成します。タイトルと説明(オプション)は、それぞれ「GoogleLogo」と「nice colors」に設定されています。

public WebAttachmentEntry uploadWebAttachment(String contentUrl, FileCabinetPageEntry filecabinet,
    String title, String description) throws MalformedURLException, IOException, ServiceException {
  MediaContent content = new MediaContent();
  content.setUri(contentUrl);

  WebAttachmentEntry webAttachment = new WebAttachmentEntry();
  webAttachment.setTitle(new PlainTextConstruct(title));
  webAttachment.setSummary(new PlainTextConstruct(description));
  webAttachment.setContent(content);
  webAttachment.addLink(SitesLink.Rel.PARENT, Link.Type.ATOM,
      filecabinet.getSelfLink().getHref());

  return client.insert(new URL(buildContentFeedUrl()), webAttachment);
}

ContentFeed contentFeed = client.getFeed(new URL(buildContentFeedUrl() + "?kind=filecabinet"), ContentFeed.class);
FileCabinetPageEntry parentPage = contentFeed.getEntries(FileCabinetPageEntry.class).get(0);

WebAttachmentEntry webAttachment =
    uploadWebAttachment("http://www.google.com/images/logo.gif", parentPage, "Google's Logo", "nice colors");
System.out.println("Web attachment created!");

POST は、ユーザーのファイル キャビネットに「http://www.google.com/images/logo.gif」の画像を参照するリンクを作成します。

トップへ戻る



コンテンツの更新

ページのメタデータや HTML コンテンツの更新

BaseContentEntry タイプのメタデータ(タイトル、pageName など)とページ コンテンツは、エントリの update() メソッドを使用して編集できます。これにより、HTTP PUT リクエストがエントリの edit リンクに送信されます。

以下に、ListPageEntry を次のように変更した例を示します。

  • タイトルが「更新版タイトル」に変更されました。
  • ページの HTML コンテンツが「<p>更新済みの HTML コンテンツ</p>」に更新されている
  • リストの最初の列見出しが「オーナー」に変更されます
ContentFeed contentFeed = client.getFeed(
    new URL(buildContentFeedUrl() + "?kind=listpage"), ContentFeed.class);
ListPageEntry listPage = contentFeed.getEntries(ListPageEntry.class).get(0); // Update first list page found

// Update title
listPage.setTitle(new PlainTextConstruct("Updated Title"));

// Update HTML content
XmlBlob xml = new XmlBlob();
xml.setBlob("<p>Updated HTML Content</p>");
listPage.setContent(new XhtmlTextConstruct(xml));

// Change first column's heading
listPage.getData().getColumns().get(0).setName("Owner");

// listPage.setPageName(new PageName("new-page-path"));  // You can also change the page's URL path

ListPageEntry updatedEntry = listPage.update();

System.out.println("ListPage updated!");

添付ファイルの内容を更新しています

AttachmentEntry の場合、エントリの MediaSource を設定してからエントリの updateMedia(boolean) メソッドを使用してコンテンツを更新することもできます。

次の例では、既存の添付ファイルの内容を更新します。

public AttachmentEntry updateFile(AttachmentEntry entry, File newFile)
    throws IOException, ServiceException {
  // See Uploading Attachments for the definition of mediaTypes.
  entry.setMediaSource(new MediaFileSource(newFile, mediaTypes.getContentType(newFile)));
  return entry.updateMedia(false);
}

この例では、HTTP PUT リクエストをエントリの edit-media リンクに送信しています。返される AttachmentEntry には、更新されたコンテンツが含まれます。

添付ファイルのメタデータとコンテンツの更新

updateMedia() メソッドを使用すると、1 回の呼び出しで添付ファイルのメタデータとそのコンテンツを更新できます。ファイルの内容のみ、メタデータ、またはその両方を更新できる場合。

この例では、添付ファイルのタイトルを「New Title」に変更し、添付ファイルの説明を更新し、ファイルの内容を新しい .zip ファイルに置き換えます。 リクエストに新しいファイル コンテンツが含まれているため、AttachmentEntryupdateMedia() が使用されます。

public AttachmentEntry updateAttachment(AttachmentEntry entry, File newFile, String newTitle, String newDescription)
    throws IOException, ServiceException  {
  // See Uploading Attachments for the definition of mediaTypes.
  entry.setMediaSource(new MediaFileSource(newFile, mediaTypes.getContentType(newFile)));
  entry.setTitle(new PlainTextConstruct(newTitle));
  entry.setSummary(new PlainTextConstruct(newDescription));

  return entry.updateMedia(true);
}

ContentFeed contentFeed = client.getFeed(
    new URL(buildContentFeedUrl() + "?kind=attachment&max-results=1"), ContentFeed.class);
AttachmentEntry attachment = contentFeed.getEntries(AttachmentEntry.class).get(0); // Update first attachment found

AttachmentEntry updatedAttachment = updateAttachment(attachment, new File("/path/to/file.zip"), "New Title", "better stuff");

トップへ戻る



コンテンツの削除

Google サイトからページまたはアイテムを削除するには、まずコンテンツ エントリを取得してから、エントリの delete() を呼び出します。

entry.delete();

エントリの edit リンクと ETag 値を渡すことで、サービスクラスの delete() メソッドを使用することもできます。

client.delete(entry.getEditLink().getHref(), "*"); // Note: using "*" may overwrite another client's changes.

エントリが正常に削除されると、サーバーは HTTP 200 OK を返します。

トップへ戻る



添付ファイルをダウンロードしています

AttachmentEntry をダウンロードするには、HTTP GET リクエストをエントリの content src リンクに送信します。

次の例では、ユーザーのコンテンツ フィードで見つかった最初の AttachmentEntry をディレクトリ「/path/to/save/file/」にダウンロードします。

private void downloadFile(String downloadUrl, String fullFilePath) throws IOException, ServiceException {
  System.out.println("Downloading file from: " + downloadUrl);

  MediaContent mc = new MediaContent();
  mc.setUri(downloadUrl);
  MediaSource ms = service.getMedia(mc);

  InputStream inStream = null;
  FileOutputStream outStream = null;

  try {
    inStream = ms.getInputStream();
    outStream = new FileOutputStream(fullFilePath);

    int c;
    while ((c = inStream.read()) != -1) {
      outStream.write(c);
    }
  } finally {
    if (inStream != null) {
      inStream.close();
    }
    if (outStream != null) {
      outStream.flush();
      outStream.close();
    }
  }
}

public void downloadAttachment(AttachmentEntry entry, String directory) throws IOException, ServiceException {
  String url = ((OutOfLineContent) entry.getContent()).getUri();
  downloadFile(url, directory + entry.getTitle().getPlainText()); // Use entry's title for the save filename
}

ContentFeed contentFeed = client.getFeed(
    new URL(buildContentFeedUrl() + "?kind=attachment&max-results=1"), ContentFeed.class);

downloadAttachment(contentFeed.getEntries(AttachmentEntry.class).get(0), "/path/to/save/file/");
System.out.println("Downloaded.");

トップへ戻る

ACL フィード

共有権限(ACL)の概要

ACL フィード内の各 ACL エントリは、特定のエンティティのアクセスロールを表します。アクセスロールとは、ユーザー、ユーザー グループ、ドメイン、デフォルトのアクセス(公開サイト)のいずれかです。エントリは、明示的なアクセス権を持つエンティティに対してのみ表示されます。Google サイトの UI の共有画面の [アクセス権を持つユーザー] パネルに、メールアドレスごとに 1 つのエントリが表示されます。したがって、サイトへの暗黙的なアクセス権がある場合でも、ドメイン管理者は表示されません。

ロール

ロール要素は、エンティティが持つことができるアクセスレベルを表します。gAcl:role 要素で使用できる値は次の 4 つです。

  • Reader - 閲覧者(読み取り専用権限と同等)。
  • writer - 共同編集者(読み取り/書き込みアクセスと同等)。
  • owner - 通常はサイト管理者(読み取り/書き込みアクセス権と同等)。

スコープ

スコープ要素は、このアクセスレベルを持つエンティティを表します。gAcl:scope 要素には次の 4 つのタイプがあります。

  • user - メールアドレスの値(例: 「user@gmail.com」)。
  • group - Google グループのメールアドレス(「group@domain.com」など)。
  • domain - G Suite ドメイン名(「domain.com」など)。
  • default - 「default」タイプのスコープは 1 つだけあり、値を持ちません(例: <gAcl:scope type="default">)。このスコープは、公開サイトでのユーザーのデフォルト アクセスを制御します。

: ドメインは、gAcl:role 値を「オーナー」アクセス権に設定することはできません。アクセスできるのはリーダーまたはライターのみです。

ACL フィードの取得

AclFeed クラスと AclEntry クラスは、サイトの共有権限の管理に使用でき、サービスクラスの getFeed() メソッドを使用して取得できます。

次の例では、特定のサイトの ACL フィードを取得し、各 AclEntry の権限を出力します。

public String getAclFeedUrl(String siteName) {
  String domain = "site";  // OR if the Site is hosted on G Suite, your domain (e.g. example.com)
  return "https://sites.google.com/feeds/acl/site/" + domain + "/" + siteName + "/";
}

public void getAclFeed(String siteName) throws IOException, ServiceException {
  AclFeed aclFeed = client.getFeed(new URL(getAclFeedUrl(siteName)), AclFeed.class);
  for (AclEntry entry : aclFeed.getEntries()) {
    System.out.println(entry.getScope().getValue() + " (" + entry.getScope().getType() + ") : " +
                       entry.getRole().getValue());
  }
}

getAclFeed('my-site-name');

SiteFeed のエントリを処理する場合、各 SiteEntry にその ACL フィードへのリンクが含まれます。たとえば、次のスニペットは SiteEntry の acl フィードを取得します。

String aclLink = siteEntry.getLink(SitesAclFeedLink.Rel.ACCESS_CONTROL_LIST, Link.Type.ATOM).getHref();
AclFeed aclFeed = client.getFeed(new URL(aclLink), AclFeed.class);

サイトの共有

: 一部の共有 ACL は、その権限を許可するようにドメインが構成されている場合にのみ使用できます(たとえば、G Suite ドメインのドメイン外部との共有が有効になっている場合)。

API を使用して Google サイトを共有するには、クライアントが新しい AclEntry を作成し、それをサーバーに対して POST で送信する必要があります。

「user@example.com」をサイトの reader として追加する例を次に示します。

AclRole role = new AclRole("reader");
AclScope scope = new AclScope(AclScope.Type.USER, "user@example.com");
AclEntry aclEntry = addAclRole(role, scope, entry);

public AclEntry addAclRole(AclRole role, AclScope scope, SiteEntry siteEntry)
    throws IOException, MalformedURLException, ServiceException  {
  AclEntry aclEntry = new AclEntry();
  aclEntry.setRole(role);
  aclEntry.setScope(scope);

  Link aclLink = siteEntry.getLink(SitesAclFeedLink.Rel.ACCESS_CONTROL_LIST, Link.Type.ATOM);
  return client.insert(new URL(aclLink.getHref()), aclEntry);
}

AclScopeAclRoles の有効な値については、ACL フィードの概要セクションをご覧ください。

グループレベルとドメインレベルでの共有

1 人のユーザーとサイトを共有する場合と同様に、Google グループまたは G Suite ドメイン間でサイトを共有できます。

グループのメールアドレスと共有:

AclScope scope = new AclScope(AclScope.Type.GROUP, "group_name@example.com");

ドメイン全体で共有:

AclScope scope = new AclScope(AclScope.Type.DOMAIN, "example.com");

ドメインレベルでの共有は、G Suite ドメインで、サイトがホストされているドメインでのみサポートされます。 たとえば、http://sites.google.com/a/domain1.com/siteA は、ドメイン全体を domain2.com と共有できず、domain2.com とのみ共有できます。G Suite ドメイン(例: http://sites.google.com/site/siteB)にホストされていないサイトはドメインを招待できません。

共有権限を変更しています

サイトの既存の共有権限を取得するには、まず対象の AclEntry を取得し、必要に応じて権限を変更してから、AclEntryupdate() メソッドを呼び出してサーバー上の ACL を変更します。

この例では、サイトを共有するセクションの以前の aclEntry の例を変更して、「user@example.com」を writer(共同編集者)に更新します。

aclEntry.setRole(new AclRole("writer"));
AclEntry updatedAclEntry = aclEntry.update();

// Could also use the client's update method
// client.update(new URL(aclEntry.getEditLink().getHref()), aclEntry);

ETag の詳細については、Google Data API リファレンス ガイドをご覧ください。

共有権限を削除しています

共有権限を削除するには、まず AclEntry を取得してから、delete() メソッドを呼び出します。

aclEntry.delete();

// Could also use the client's delete method
// client.delete(new URL(aclEntry.getEditLink().getHref()), aclEntry);

ETag の詳細については、Google Data API リファレンス ガイドをご覧ください。

トップへ戻る

特別なトピック

フィードまたはエントリを再度取得する

以前に取得したフィードやエントリを取得する場合は、前回取得してから変更されている場合にのみリストまたはエントリを送信するようにサーバーに伝えることで、効率を向上できます。

このような条件付き取得を行うには、getFeed() メソッドと getEntry() メソッドの両方で、If-Modified-Since ヘッダーの ETag 値または DateTime オブジェクトを受け取る追加の引数を指定します。エントリの Etag には entry.getEtag() からアクセスできます。

次の例では、コンテンツのウェブページ エントリを条件付きで取得します。

String feedUrl = "https://sites.google.com/feeds/content/site/siteName/123456789";
WebPageEntry entry = client.getEntry(new URL(feedUrl), WebPageEntry.class, "\"GVQHSARDQyp7ImBq\"");

サーバーはこのリクエストを受信すると、リクエストされたアイテムが指定された ETag と同じ ETag を持つかどうかを確認します。ETag が一致していれば、アイテムは変更されておらず、サーバーは HTTP 304 NotModifiedException 例外がスローされます。

ETag が一致しない場合は、前回リクエストした後にアイテムが変更されているため、サーバーからそのアイテムが返されます。

ETag の詳細については、Google Data API リファレンス ガイドをご覧ください。

トップへ戻る