背景
使用安全私人圖片功能,即可在票證上定義不需要透過公開網址存取的圖片 (一般圖片必須透過公開網址存取,才能在票證上正確顯示)。您可以使用 Wallet API 上傳圖片,並取得可用於參照該圖片的 ID,該圖片會顯示在 Google 錢包票證物件上。
系統支援下列票證類型:
- 活動票券
- 登機證
- 大眾運輸票證
- 優惠卡
- 禮物卡
- 會員憑證
- 一般票證
功能範例
|
| 使用安全私密圖片的會員卡 |
應用實例
安全私人圖片可供使用不必公開存取的圖片,因此可用於個人資料相片等用途。這類用途包括但不限於:
- 年費會員證
- 商家名片
- 大眾運輸票證
注意:安全私人圖片不得用於代表政府核發的身分證件,且須遵守可接受的使用政策中定義的其他規範。
使用錢包功能時的注意事項
- 無法與一般私人票證搭配使用
- 只能新增至票證物件 (而非類別)
- 只能搭配單一物件使用
- 只能搭配 ImageModuleData 使用 (不能搭配其他圖片,例如標誌和寬版標題標誌)
開發中的功能
- 網路支援
- 主頁橫幅上的私人圖片
整合步驟
- 使用 Google 錢包 API 上傳私人圖片,即可收到
privateImageId。 - 將圖片新增至票證物件 (例如
ImageModuleData)。請勿在圖片上設定sourceUri欄位,而是要使用上一個步驟取得的值設定privateImageId欄位。
流程圖
|
| 安全私密圖片序列 |
程式碼範例
上傳圖片
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();
更新票證,在正面顯示圖片
您可以在任何 imageModulesData 欄位中使用安全的私人映像檔。 以下範例說明如何使用 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))));
}例外狀況處理
如果 Wallet FeatureAPI 使用方式有誤,可能會發生下列錯誤:
| 訊息 | 原因 |
|---|---|
| 圖片不得同時有 source_uri 和 private_image_id | 核發者嘗試在單一圖片上設定 source_uri 和 private_image_id,但系統不允許這麼做 |
| 找不到發行者「%s」的 ID 為「%s」的私人圖片 | 在物件上設定不存在的私人圖片 ID |
| 無法將 ID 為「%s」的私人圖片新增至發行者「%s」的物件「%s」,因為該圖片已用於物件「%s」。私密圖片只能用於一個物件。 | 嘗試在多個物件上使用相同的私人圖片。如要將同一個私人圖片用於多個物件,必須重新上傳圖片,並取得新的私人圖片 ID,才能用於第二個物件 |