非公開イメージを保護する

背景

安全な非公開画像機能を使用すると、公開 URL からアクセスする必要のないパスの画像を定義できます(パスで画像を適切にレンダリングするための通常の画像の要件)。Wallet API を使用して画像をアップロードし、Wallet パス オブジェクトでその画像を参照するために使用できる識別子を受け取ります。

次のパスタイプがサポートされています。

  • イベント チケット
  • 搭乗券
  • 交通機関のパス
  • クーポンカード
  • ギフトカード
  • ポイントカード
  • 汎用パス

機能の例

安全なプライベート イメージの例
安全な非公開イメージを使用したメンバーシップ カード

ユースケース

安全な非公開イメージを使用すると、一般公開する必要のないイメージを使用できます。たとえば、プロフィール写真などに使用できます。これにより、次のようなユースケースが可能になります。

  • 年間メンバーシップ パス
  • ビジネスカード
  • 交通機関のパス

注: 安全な限定公開イメージは、利用規定で定義されている追加のガイドラインとともに、政府機関発行の身分証明書としてパスを表すために使用されるものではありません。

ウォレット機能を使用する際の考慮事項

  • 汎用プライベート パスでは使用できません
  • パス オブジェクト(クラスではない)にのみ追加できます
  • 単一の Object でのみ使用できます
  • ImageModuleData でのみ使用できます(他の画像、たとえばロゴやワイド ヘッダーロゴでは使用できません)。

開発中の機能

  • ウェブサポート
  • ヒーロー画像に非公開画像を使用する

統合の手順

  1. Google Wallet API を使用して非公開画像をアップロードし、privateImageId を受け取ります。
  2. パス オブジェクトに画像を追加します(例: ImageModuleData)。画像の sourceUri フィールドを設定する代わりに、前のステップで取得した値を使用して privateImageId フィールドを設定します。

シーケンス図

Secure Image のシーケンス図
安全なプライベート画像シーケンス

コード例

画像をアップロードする

String issuerId = "12345";
String keyFilePath = "/path/to/key.json";
GoogleCredential credential =
    GoogleCredential.fromStream(new FileInputStream(keyFilePath))
        .createScoped(Arrays.asList("https://www.googleapis.com/auth/wallet_object.issuer"));
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

// Prepare request to upload image
String contentType = "image/jpeg";  // MIME type of image
String imageFilePath = "/path/to/image.jpg";
HttpContent content =
    new ByteArrayContent(
        contentType, ByteStreams.toByteArray(new FileInputStream(imageFilePath)));

String url =
    String.format(
        "https://walletobjects.googleapis.com/upload/walletobjects/v1/privateContent/%s/uploadPrivateImage",
        issuerId);

// Make request to upload image
HttpResponse response =
    httpTransport
        .createRequestFactory(credential)
        .buildPostRequest(new GenericUrl(url), content)
        .execute();

// Get privateImageId from response
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(response.parseAsString(), JsonObject.class);
String privateImageId = jsonObject.get("privateImageId").getAsString();

パス オブジェクトで非公開画像を使用する

// Build GenericObject with privateImageId in ImageModuleData (also adding an optional TextModuleData)
Image image = new Image().setPrivateImageId(privateImageId);
ImageModuleData imageModuleData = new ImageModuleData().setId("imageId").setMainImage(image);
TextModuleData textModuleData =
    new TextModuleData().setId("textId").setHeader("Card holder").setBody("John Doe");
GenericObject genericObject =
    new GenericObject()
        .setId(issuerId + ".objectId")
        // class must be inserted before inserting object
        .setClassId(issuerId + ".classId")
        .setCardTitle("Business name")
        .setHeader("My membership card")
        .setImageModulesData(Arrays.asList(imageModuleData))
        .setTextModulesData(Arrays.asList(textModuleData));

// Insert GenericObject (or can use in JWT without inserting ahead of time)
Walletobjects service =
    new Walletobjects.Builder(httpTransport, GsonFactory.getDefaultInstance(), credential)
        .setApplicationName("My Application")
        .build();

service.genericobject().insert(genericObject).execute();

パスを更新して画像を前面に表示する

安全な非公開イメージは、任意の imageModulesDatafield で使用できます。次に、imageModulesData フィールドを使用して、テンプレートのオーバーライドでカードの前面に画像を配置する例を示します。テンプレートのオーバーライドを使用してクラスを挿入する例を次に示します。

CardTemplateOverride cardTemplateOverride =
    new CardTemplateOverride()
        .setCardRowTemplateInfos(
            Arrays.asList(
                new CardRowTemplateInfo()
                    .setTwoItems(
                        new CardRowTwoItems()
                            // The ids chosen here must be set on the object's TextModuleData and ImageModuleData
                            .setStartItem(
                                createTemplateItem("object.textModulesData['textId']"))
                            .setEndItem(
                                createTemplateItem("object.imageModulesData['imageId']")))));
GenericClass genericClass =
    new GenericClass()
        .setId(issuerId + ".classId")
        .setClassTemplateInfo(
            new ClassTemplateInfo().setCardTemplateOverride(cardTemplateOverride));

service.genericclass().insert(genericClass);


...


private static TemplateItem createTemplateItem(String fieldPath) {
  return new TemplateItem()
      .setFirstValue(
          new FieldSelector()
              .setFields(Arrays.asList(new FieldReference().setFieldPath(fieldPath))));
}

例外処理

ウォレット FeatureAPI の誤った使用により、次のようなエラーが発生する可能性があります。

メッセージ 理由
イメージに source_uri と private_image_id の両方を指定することはできません 発行者が 1 つのイメージに source_uri と private_image_id を設定しようとしましたが、これは許可されていません
発行者 %s の ID %s の非公開イメージが見つかりませんでした オブジェクトに存在しない非公開イメージ ID を設定する
ID %s の限定公開イメージを発行者 %s のオブジェクト %s に追加できませんでした。オブジェクト %s で使用されているためです。プライベート イメージは 1 つのオブジェクトでのみ使用できます。 複数のオブジェクトで同じ非公開イメージを使用しようとしています。同じ非公開イメージを複数のオブジェクトで使用するには、再アップロードする必要があります。再アップロードすると、新しい非公開イメージ ID が取得され、2 番目のオブジェクトで使用できるようになります。