您正在查看 Apigee Edge 說明文件。
前往 Apigee X 說明文件。 info
版本:2.0.0
透過 Google 驗證,存取指定的 Google API。
使用這項擴充功能取得 Google Cloud 服務的權杖 (OAuth 或 JWT),然後在後續呼叫 Google API 時使用權杖,例如使用 ServiceCallout 政策。
舉例來說,您可以在 API 代理中使用此擴充功能取得權杖,然後透過 PopulateCache 政策快取權杖,再透過 ServiceCallout 政策傳遞權杖,以便從 API 代理流程中存取 Google Cloud 服務。
必要條件
本內容提供設定和使用此擴充功能的參考資訊。使用 ExtensionCallout 政策從 API proxy 使用擴充功能前,您必須:
請確認要使用的擴充功能帳戶 (由您用於憑證的服務帳戶所代表的帳戶) 具有擴充功能要驗證的 Google Cloud 服務存取權。
關於使用 Google Cloud 進行驗證
這個擴充功能會代表特定 Google Cloud 專案中定義的會員,向 Google Cloud 要求驗證。設定這個擴充功能時,請使用該專案成員的服務帳戶 JSON 檔案。
因此,這個擴充功能只會存取該成員有權存取的資源。換句話說,這個擴充功能能否成功驗證,取決於在 Google Cloud 控制台中授予的權限與擴充功能在執行階段要求的存取權 (透過範圍或目標對象) 是否相符。
一般來說,您需要執行下列步驟,才能透過這個擴充功能驗證 API 存取權:
請確認這項擴充功能所代表的服務帳戶成員,具備您要存取的 Google 資源存取權。您可以使用 Google Cloud 控制台中的 Cloud Identity and Access Management (Cloud IAM) 頁面,為這項擴充功能所代表的專案成員授予角色。
設定這個擴充功能時,請使用該成員的服務帳戶金鑰 JSON。
設定ExtensionCallout 政策以使用這個擴充功能時,請只針對專案成員有存取權的資源要求驗證。
範例
以下範例說明如何使用ExtensionCallout 政策向 Google Cloud 進行驗證。
取得存取憑證
在以下範例中,擴充功能的 getOauth2AccessToken
動作會擷取權杖,用於傳送至 Cloud Translation API 的要求。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ConnectorCallout async="false" continueOnError="true" enabled="true" name="Get-Access-Token">
<DisplayName>Get Access Token</DisplayName>
<Connector>google-auth</Connector>
<Action>getOauth2AccessToken</Action>
<Input><![CDATA[{
"scope" : [
"https://www.googleapis.com/auth/cloud-translation"
]
}]]></Input>
<Output>google.credentials</Output>
</ConnectorCallout>
回應值如下所示:
{
"access_token":"ya29.c.ElpSB...BMgkALBJ0kou-8",
"token_type":"Bearer",
"expiresInSec": 3600
}
以下的 AssignMessage 政策會從上述 ExtensionCallout 政策中擷取回應值,並將該值複製到回應酬載中。這對偵錯作業來說相當實用。在實際操作中,您可能不想將符記傳回給用戶端。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Retrieve-Auth-Token">
<DisplayName>Retrieve Auth Token</DisplayName>
<AssignTo type="response" createNew="false"/>
<Set>
<Payload contentType="application/json">{google.credentials.access_token}</Payload>
</Set>
</AssignMessage>
快取存取權杖
為避免不必要的呼叫來擷取符記,建議您將收到的符記快取。對於需要權杖的後續呼叫,從 Apigee Edge 快取中擷取權杖的速度會比取得新權杖快。快取憑證到期時,請擷取新的憑證,並使用該憑證重新整理快取。
以下範例 API Proxy 的程式碼說明如何設定及使用快取憑證,以 ServiceCallout 政策呼叫 Google Translation API。此處的每個程式碼範例都適用於流程中的不同政策。
系統會依照下列流程 XML 所述的順序執行下列政策:
<Request>
<!-- Attempt to get a token from the cache. -->
<Step>
<Name>Get-Cached-Auth-Token</Name>
</Step>
<!-- Only execute the following ExtensionCallout policy if the call to the
cache couldn't retrieve a cached token. -->
<Step>
<Name>Google-Auth-Callout</Name>
<Condition>lookupcache.Get-Cached-Auth-Token.cachehit is false</Condition>
</Step>
<!-- Only execute the following PopulateCache policy if the call to the
cache couldn't retrieve a cached token. -->
<Step>
<Name>Cache-Auth-Token</Name>
<Condition>lookupcache.Get-Cached-Auth-Token.cachehit is false</Condition>
</Step>
<!-- Use the ServiceCallout policy to call the translate API. -->
<Step>
<Name>Translate-Text</Name>
</Step>
</Request>
以下的 LookupCache 政策會嘗試從快取中取得權杖。如果權杖已取得並快取,這項政策會取得權杖,供 API 代理程式使用。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <LookupCache async="false" continueOnError="false" enabled="true" name="Get-Cached-Auth-Token"> <DisplayName>Get Cached Auth Token</DisplayName> <!-- Give cache key and scope to specify the entry for the cached token. --> <CacheKey> <Prefix/> <KeyFragment>gcp_translate_token_</KeyFragment> </CacheKey> <Scope>Exclusive</Scope> <!-- Assign the retrieved token (if any) to a variable, where it can be retrieved by policies. --> <AssignTo>cloud.translation.auth.token</AssignTo> </LookupCache>
如果快取查詢未擷取快取的權杖,則下列 ExtensionCallout 政策會擷取新的 OAuth 權杖,並指定 Google Cloud Translation API 做為權杖的範圍。如果設定
Google-Auth-Callout
擴充功能時使用的服務帳戶憑證代表有權存取 API 的專案成員,Google Cloud 就會傳回有效的權杖。<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ConnectorCallout async="false" continueOnError="true" enabled="true" name="Google-Auth-Callout"> <DisplayName>Google-Auth-Callout</DisplayName> <Connector>example-auth-extension</Connector> <Action>getOauth2AccessToken</Action> <Input><![CDATA[{ "scope" : ["https://www.googleapis.com/auth/cloud-translation"] }]]></Input> <Output parsed="false">cloud.translation.auth.token</Output> </ConnectorCallout>
ExtensionCallout 政策擷取新權杖後,PopulateCache 政策會將權杖快取,供 API Proxy 中的政策稍後使用。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <PopulateCache async="false" continueOnError="false" enabled="true" name="Cache-Auth-Token"> <DisplayName>Cache Auth Token</DisplayName> <Properties/> <!-- Set cache key information to specify a unique key for this entry. --> <CacheKey> <Prefix/> <KeyFragment>gcp_translate_token_</KeyFragment> </CacheKey> <Scope>Exclusive</Scope> <ExpirySettings> <TimeoutInSec>5</TimeoutInSec> </ExpirySettings> <!-- Get the token to cache from the variable where the ExtensionCallout put it. --> <Source>cloud.translation.auth.token</Source> </PopulateCache>
動作
getOauth2AccessToken
取得 OAuth 2.0 存取權杖。當 Google API 需要 OAuth 權杖時,您可以使用這個動作,在 API Proxy 和 Google API 之間支援雙足式 OAuth。
在雙方 OAuth 中,這個擴充功能動作會使用服務帳戶 JSON (您在設定這個擴充功能時會新增該 JSON) 向 Google 進行驗證,藉此擷取 OAuth 權杖。這項動作擷取 OAuth 權杖後,API 代理程式就能使用權杖呼叫 Google API,有效地代表 Google 服務帳戶呼叫 API。
系統會根據 Google API 適用的 OAuth 2.0 範圍中列出的範圍,篩選 Google Cloud API 的存取權。
如要進一步瞭解 OAuth 2.0 的伺服器對伺服器互動行為,請參閱「針對伺服器對伺服器應用程式使用 OAuth 2.0」一文。
語法
<Action>getOauth2AccessToken</Action>
<Input><![CDATA[{
"scope" : [
"scope1",
"scope2"
]
}]]></Input>
範例
在以下範例中,擴充功能的 getOauth2AccessToken
動作會擷取權杖,用於傳送至 Cloud Translation API 的要求。
<Action>getOauth2AccessToken</Action>
<Input><![CDATA[{
"scope" : [
"https://www.googleapis.com/auth/cloud-translation"
]
}]]></Input>
要求參數
參數 | 說明 | 類型 | 預設 | 必填 |
---|---|---|---|---|
範圍 | OAuth 2.0 範圍陣列。如要進一步瞭解範圍,請參閱「Google API 適用的 OAuth 2.0 範圍」。 | 陣列 | ["https://www.googleapis.com/auth/cloud-platform"] ,可授予服務帳戶存取權,存取所有 API。 |
不會。 |
回應
物件,其中包含存取權存證、類型和到期日,格式如下:
{
"accessToken": "ewogICJ0eXB...C5jb20iCn0K",
"token_type": "Bearer",
"expiresInSec": 3600
}
回應屬性
參數 | 說明 | 預設 | 必填 |
---|---|---|---|
accessToken | OAuth 2.0 存取權杖。 | 無 | 是 |
tokenType | 權杖類型。 | 承載系統 | 是 |
expiresInSec | 符記到期前的秒數。 | 3600 | 是 |
getJWTAccessToken
取得 JSON Web Token (JWT) 存取權杖。如果您要呼叫的 API 在 Google API GitHub 存放區中發布了服務定義,您可以使用這個權杖來驗證 Google API。
對於某些 Google API,您可以直接使用已簽署的 JWT 做為不記名憑證,而非 OAuth 2.0 存取權杖,來發出經過授權的 API 呼叫。這樣一來,您就不必在發出 API 呼叫前,先向 Google 授權伺服器提出網路要求。
如要進一步瞭解如何使用 JWT 存取權權杖進行驗證,請參閱「針對伺服器對伺服器應用程式使用 OAuth 2.0」。
語法
<Action>getJWTAccessToken</Action>
<Input><![CDATA[{
"audience" : "audience"
}]]></Input>
範例:Cloud 函式網址
在以下範例中,擴充功能的 getOauth2AccessToken
動作會擷取權杖,用於傳送至 Cloud Translation API 的要求。
<Action>getJWTAccessToken</Action>
<Input><![CDATA[{
"audience" : "https://YOUR_REGION-YOUR_PROJECT_ID.cloudfunctions.net/FUNCTION_NAME"
}]]></Input>
範例:受 Cloud IAP 保護的用戶端 ID
在以下範例中,擴充功能的 getOauth2AccessToken
動作會擷取權杖,用於傳送至 Cloud Translation API 的要求。
<Action>getJWTAccessToken</Action>
<Input><![CDATA[{
"audience" : "Cloud-IAP-secured-client-ID"
}]]></Input>
要求參數
參數 | 說明 | 預設 | 必填 |
---|---|---|---|
audience | 權杖的預期收件者。這可能包括受 Cloud IAP 保護的用戶端 ID、Cloud Functions 網址等。 | 無 | 是 |
回應
{
"accessToken": "token",
"tokenType": "Bearer",
"expiresInSec": 3600
}
回應屬性
參數 | 說明 | 預設 | 必填 |
---|---|---|---|
accessToken | 存取權杖。 | 無 | 是 |
tokenType | 權杖類型。 | 承載系統 | 是 |
expiresInSec | 過期時間 (以秒為單位)。 | 3600 | 是 |
設定參考資料
如要設定及部署此擴充功能,以便在 API 代理程式中使用,請使用下列說明。如要瞭解如何使用 Apigee 控制台設定擴充功能,請參閱「新增及設定擴充功能」。
常見的擴充功能屬性
每個擴充功能都有下列屬性。
屬性 | 說明 | 預設 | 必要 |
---|---|---|---|
name |
您要為這項擴充功能設定的名稱。 | 無 | 相容 |
packageName |
Apigee Edge 指定的擴充功能套件名稱。 | 無 | 相容 |
version |
擴充功能的擴充功能套件版本號碼。 | 無 | 相容 |
configuration |
您要新增的擴充功能專屬的設定值。請參閱「這個擴充功能套件的屬性」一文 | 無 | 相容 |
這個擴充功能套件的屬性
請為下列專屬於此擴充功能的設定屬性指定值。
屬性 | 說明 | 預設 | 必填 |
---|---|---|---|
憑證 | 輸入 Apigee Edge 主控台後,這就是服務帳戶金鑰 JSON 檔案的完整內容。透過管理 API 傳送時,這會是從整個服務帳戶金鑰 JSON 檔案產生的 Base64 編碼值。 | 無 | 是 |