透過程式為 IAP 建立 OAuth 用戶端

本頁說明如何以程式輔助方式建立 OAuth 用戶端,以便與應用程式內購功能搭配使用,讓您能以程式輔助方式為內部應用程式端到端設定應用程式內購功能。

已知限制

使用這個 API 以程式輔助方式建立 OAuth 用戶端時,有幾項限制:

  • 您只能使用 API 修改 API 建立的 OAuth 用戶端。如果 OAuth 用戶端是使用 API 建立的,您就無法透過 Google Cloud 控制台修改。
  • 由 API 建立的 OAuth 用戶端僅限用於 IAP,因此 API 不允許更新重新導向 URI 或其他屬性。
  • 這個 API 無法對使用 Google Cloud 控制台建立的 OAuth 用戶端運作。
  • 使用 API 時,每個專案最多只能有 500 個 OAuth 用戶端。
  • 由 API 建立的 OAuth 同意畫面品牌有特定限制。詳情請參閱下文

瞭解品牌和品牌狀態

OAuth 同意畫面包含使用者的品牌資訊,稱為品牌。品牌可限制內部使用者或公開使用者。內部品牌可讓與專案相同的 Google Workspace 機構成員使用 OAuth 流程。公開品牌可讓網際網路上的任何人都能使用 OAuth 流程。

品牌可以透過手動方式或 API 程式碼建立。API 建立的品牌會自動設定不同的設定:

  • 這些參數預設為內部,如有需要,必須手動設為公開
  • 這些廣告會設為「未審查」狀態,且必須觸發品牌審查

如何手動將內部品牌設為公開:

  1. 開啟 OAuth 同意畫面
  2. 從下拉式選單中選取所需專案。
  3. 請注意,在「OAuth 同意畫面」頁面中,「使用者類型」會自動設為「內部」。如要將其設為「公開」,請按一下「編輯應用程式」,即可使用更多設定選項。
  4. 在「Application type」下方,按一下「Public」

如要針對未經審查的 API 建立品牌觸發品牌審查,請按照下列步驟操作:

  1. 開啟 OAuth 同意畫面
  2. 從下拉式選單中選取所需專案。
  3. OAuth 同意畫面頁面中輸入所有必要資訊,然後按一下「提交以供驗證」

驗證程序可能需要數週的時間才能完成。在此期間,我們會透過電子郵件傳送最新消息給您。進一步瞭解驗證程序。驗證程序進行期間,您仍可在 Google Workspace 機構中使用應用程式。進一步瞭解應用程式通過驗證前會出現哪些行為。

事前準備

在建立用戶端之前,請先確認呼叫端已獲得下列權限:

  • clientauthconfig.brands.list
  • clientauthconfig.brands.create
  • clientauthconfig.brands.get
  • clientauthconfig.clients.create
  • clientauthconfig.clients.listWithSecrets (僅適用於列出含有密鑰的 OAuth 用戶端)。
  • clientauthconfig.clients.getWithSecret
  • clientauthconfig.clients.delete
  • clientauthconfig.clients.update

這些權限包含在「編輯者」(roles/editor) 和「擁有者」(roles/owner) 基本角色中,但建議您建立包含這些權限的自訂角色,並將該角色授予呼叫端。

設定 IAP 的 OAuth

下列步驟說明如何設定同意畫面,以及為 IAP 建立及使用 oauth 用戶端。

  1. Check if you already have an existing brand by using the list command. You may only have one brand per project.

    gcloud iap oauth-brands list

    The following is an example gcloud response, if the brand exists:

    name: projects/[PROJECT_NUMBER]/brands/[BRAND_ID]
    applicationTitle: [APPLICATION_TITLE]
    supportEmail: [SUPPORT_EMAIL]
    orgInternalOnly: true
    
  2. If no brand exists, use the create command:

    gcloud iap oauth-brands create --application_title=APPLICATION_TITLE --support_email=SUPPORT_EMAIL

    The above fields are required when calling this API:

    • supportEmail: The support email displayed on the OAuth consent screen. This email address can either be a user's address or a Google Groups alias. While service accounts also have an email address, they are not actual valid email addresses, and cannot be used when creating a brand. However, a service account can be the owner of a Google Group. Either create a new Google Group or configure an existing group and set the desired service account as an owner of the group.

    • applicationTitle: The application name displayed on OAuth consent screen.

    The response contains the following fields:

    name: projects/[PROJECT_NUMBER]/brands/[BRAND_ID]
    applicationTitle: [APPLICATION_TITLE]
    supportEmail: [SUPPORT_EMAIL]
    orgInternalOnly: true
    

Creating an IAP OAuth Client

  1. Use the create command to create a client. Use the brand name from previous step.

    gcloud iap oauth-clients create projects/PROJECT_NUMBER/brands/BRAND-ID --display_name=NAME

    The response contains the following fields:

    name: projects/[PROJECT_NUMBER]/brands/[BRAND_NAME]/identityAwareProxyClients/[CLIENT_ID]
    secret: [CLIENT_SECRET]
    displayName: [NAME]
    

請使用用戶端 ID (上例中的 client_id) 和 secret 啟用 IAP。如要進一步瞭解如何使用剛建立的憑證啟用 IAP,請參閱下列主題: