服務呼叫政策

可擴充政策

本頁面適用於 Apigee 和 Apigee Hybrid。

查看 Apigee Edge 說明文件。

ServiceCallout 政策可讓您從 API Proxy 流程呼叫其他服務。您可以對外部服務 (例如外部 RESTful 服務端點) 或內部服務 (例如相同組織和環境中的 API Proxy) 發出呼叫。

這項政策屬於可擴充政策,使用這項政策可能會產生費用或影響使用率,具體取決於您的 Apigee 授權。如要瞭解政策類型和使用影響,請參閱「政策類型」。

  • 在外部用途中,您會對 Proxy 外部的第三方 API 進行呼叫。系統會剖析第三方 API 的回應,並插入 API 的回應訊息,為應用程式使用者提供更豐富、整合的資料。您也可以在要求流程中使用 ServiceCallout 政策提出要求,然後將回應中的資訊傳遞至 API Proxy 的 TargetEndpoint。
  • 在另一個用途中,您呼叫的 Proxy 與呼叫來源位於相同的組織和環境。舉例來說,如果某個 Proxy 提供一或多個其他 Proxy 會使用的離散低階功能,您可能會發現這項功能很有用。舉例來說,如果某個 Proxy 透過後端資料儲存庫公開建立/讀取/更新/刪除作業,則該 Proxy 可能是其他多個 Proxy 的目標 Proxy,這些 Proxy 會向用戶端公開資料。

這項政策支援透過 HTTP 和 HTTPS 提出的要求。

範例

對內部 Proxy 進行本機呼叫

<LocalTargetConnection>
    <APIProxy>data-manager</APIProxy>
    <ProxyEndpoint>default</ProxyEndpoint>
</LocalTargetConnection>

這個範例會建立對本機 API Proxy (即位於相同組織和環境中的 Proxy) 的呼叫,名為 data-manager,並指定其 Proxy 端點,名稱為 default。

網址做為變數

<HTTPTargetConnection>
    <URL>http://example.com/{request.myResourcePath}</URL>
</HTTPTargetConnection>

這個範例在網址中使用變數,動態填入目標網址。網址的通訊協定部分 (http://) 無法由變數指定。此外,您必須為網址的網域部分和網址的其餘部分使用不同的變數。

Google 地理編碼/定義要求

<ServiceCallout name="ServiceCallout-GeocodingRequest1">
    <DisplayName>Inline request message</DisplayName>
    <Request variable="authenticationRequest">
      <Set>
        <QueryParams>
          <QueryParam name="address">{request.queryparam.postalcode}</QueryParam>
          <QueryParam name="region">{request.queryparam.country}</QueryParam>
          <QueryParam name="sensor">false</QueryParam>
        </QueryParams>
      </Set>
    </Request>
    <Response>GeocodingResponse</Response>
    <Timeout>30000</Timeout>
    <HTTPTargetConnection>
      <URL>https://maps.googleapis.com/maps/api/geocode/json</URL>
    </HTTPTargetConnection>
</ServiceCallout>

您不一定要使用 AssignMessage 政策等政策來建立要求物件,也可以直接在 ServiceCallout 政策中定義。在本例中,ServiceCallout 政策會設定傳遞至外部服務的三個查詢參數值。您可以在 ServiceCallout 政策中建立完整的要求訊息,指定酬載、編碼類型 (例如 application/xml)、標頭、表單參數等。

以下是另一個範例,說明在要求到達 ServiceCallout 政策之前,如何形成要求。

<ServiceCallout name="ServiceCallout-GeocodingRequest2">
    <Request clearPayload="false" variable="GeocodingRequest"/>
    <Response>GeocodingResponse</Response>
    <Timeout>30000</Timeout>
    <HTTPTargetConnection>
      <URL>https://maps.googleapis.com/maps/api/geocode/json</URL>
    </HTTPTargetConnection>
</ServiceCallout>

要求訊息的內容是從名為 GeocodingRequest 的變數中擷取 (例如,可由 AssignMessage 政策填入)。回應訊息會指派給名為 GeocodingResponse 的變數,可由 ExtractVariables 政策或以 JavaScript 或 Java 編寫的自訂程式碼剖析。政策會等待 30 秒,等 Google Geocoding API 回應,逾時後就會停止。

呼叫目標伺服器

<ServiceCallout async="false" continueOnError="false" enabled="true" name="service-callout">
    <DisplayName>service-callout</DisplayName>
    <Properties/>
    <Request clearPayload="true" variable="myRequest">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    </Request>
    <Response>myResponse</Response>
    <HTTPTargetConnection>
        <LoadBalancer>
            <Algorithm>RoundRobin</Algorithm>
            <Server name="httpbin"/>
            <Server name="yahoo"/>
        </LoadBalancer>
        <Path>/get</Path>
    </HTTPTargetConnection>
</ServiceCallout>

這項政策會使用 LoadBalancer 屬性呼叫目標伺服器,並在這些伺服器之間進行負載平衡。在本範例中,負載會分配給兩個目標伺服器,分別名為 httpbin 和 yahoo。如要瞭解如何為 Proxy 設定目標伺服器及負載平衡,請參閱「跨後端伺服器進行負載平衡」。


關於 ServiceCallout 政策

在許多情況下,您可以在 API Proxy 中使用 ServiceCallout 政策。舉例來說,您可以設定 API Proxy 來呼叫外部服務,以提供地理位置資料、顧客評論、合作夥伴零售目錄中的商品等。

呼叫通常會與另外兩項政策搭配使用:AssignMessage 和 ExtractVariables。

  • 要求:AssignMessage 會填入傳送至遠端服務的要求訊息。
  • 回應:ExtractVariables 會剖析回應並擷取特定內容。

ServiceCallout 政策通常由下列部分組成:

  1. AssignMessage 政策:建立要求訊息、填入 HTTP 標頭、查詢參數、設定 HTTP 動詞等。
  2. ServiceCallout 政策:參照 AssignMessage 政策建立的訊息、定義外部呼叫的目標網址,以及定義目標服務傳回的回應物件名稱。

    如要提升效能,您也可以快取 ServiceCallout 回應,詳情請參閱「如何將 ServiceCallout 政策的結果儲存在快取中?以及如何從快取中擷取結果?」一節。

  3. ExtractVariables 政策:通常會定義 JSONPath 或 XPath 運算式,用於剖析 ServiceCallout 產生的訊息。接著,政策會設定變數,其中包含從 ServiceCallout 回應剖析的值。

自訂錯誤處理

在 PostClientFlow 中,ServiceCallout 錯誤無法變更用戶端回應、觸發 FaultRules 或停止後續步驟,即使 continueOnError="false" 也是如此,因為回應已傳送。如果加入 <Response> 元素,Apigee 仍會在發生錯誤時將 servicecallout.{policy-name}.failed 設為 true。如果省略 <Response>,Apigee 會以非同步方式傳送呼叫,不會等待回應、忽略錯誤,也不會設定這個變數。

元素參考資料

您可以在這項政策中設定下列元素和屬性:

<ServiceCallout async="false" continueOnError="false" enabled="true" name="Service-Callout-1">
    <DisplayName>Custom label used in UI</DisplayName>
    <Request clearPayload="true" variable="myRequest">
        <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
        <Remove>
            <StatusCode/>
            <Path/>
            <Version/>
            <Verb/>
         </Remove>
         <Copy>
            <StatusCode/>
            <Path/>
            <Version/>
            <Verb/>
        </Copy>
        <Add>
            <Headers/>
            <QueryParams/>
            <FormParams/>
        </Add>
        <Set>
            <Headers/>
            <QueryParams/>
            <FormParams/>
            <Payload/>
            <StatusCode/>
            <Path/>
            <Version/>
            <Verb/>
        </Set>
    </Request>
    <Response>calloutResponse</Response>
    <Timeout>30000</Timeout>
    <HTTPTargetConnection>
        <URL>http://example.com</URL>
        <LoadBalancer/>
        <SSLInfo/>
        <Properties/>
        <Authentication>
          <HeaderName ref="{variable}">STRING</HeaderName>
          <GoogleAccessToken>
            <Scopes>
              <Scope>https://www.googleapis.com/auth/cloud-platform</Scope>
            </Scopes>
            <LifetimeInSeconds ref="{variable}">3600</LifetimeInSeconds>
          </GoogleAccessToken>
        </Authentication>
        <Authentication>
          <HeaderName ref="{variable}">STRING</HeaderName>
          <GoogleIDToken>
            <Audience ref="{variable}" useTargetUrl="BOOLEAN">{hostname}</Audience>
            <IncludeEmail ref="{variable}">true</IncludeEmail>
          </GoogleIDToken>
        </Authentication>
    </HTTPTargetConnection>
    <LocalTargetConnection>
        <APIProxy/>
        <ProxyEndpoint/>
        <Path/>
    </LocalTargetConnection>
</ServiceCallout>

<ServiceCallout> 屬性

<ServiceCallout async="false" continueOnError="false" enabled="true" name="Service-Callout-1">

下表說明所有政策父項元素的共同屬性:

屬性 說明 預設 存在必要性
name

政策的內部名稱。name 屬性的值可以包含英文字母、數字、空格、連字號、底線和句號。這個值不得超過 255 個半形字元。

您可以選擇使用 <DisplayName> 元素,在管理 UI 代理程式編輯器中為政策加上不同、自然語言的名稱。

不適用 必填
continueOnError

將其設為 false,即可在政策失敗時傳回錯誤。這是大多數政策的預期行為。

將其設為 true,即使政策失敗,流程執行作業仍會繼續進行。另請參閱:

false 選用
enabled

設為 true 即可強制執行政策。

設為 false 即可關閉政策。即使政策仍附加至流程中,也不會強制執行。

是 選用
async

此屬性已淘汰。

false 已淘汰

<DisplayName> 元素

除了 name 屬性之外,您也可以在管理 UI 代理程式編輯器中使用不同的自然語言名稱標示政策。

<DisplayName>Policy Display Name</DisplayName>
預設

不適用

如果省略這個元素,系統會使用政策的 name 屬性值。

存在必要性 選用
類型 字串

<Request> 元素

指定變數,其中包含從 API Proxy 傳送至其他服務的要求訊息。變數可由流程中的前一項政策建立,也可以在 ServiceCallout 政策中內嵌建立。

<Request clearPayload="true" variable="myRequest">
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    <Remove>
        <StatusCode/>
        <Path/>
        <Version/>
        <Verb/>
    </Remove>
    <Copy>
        <StatusCode/>
        <Path/>
        <Version/>
        <Verb/>
    </Copy>
    <Add>
        <Headers/>
        <QueryParams/>
        <FormParams/>
    </Add>
    <Set>
        <Headers/>
        <QueryParams/>
        <FormParams/>
        <Payload/>
        <StatusCode/>
        <Path/>
        <Version/>
        <Verb/>
    </Set>
</Request>

<Remove>、<Copy>、<Add> 和 <Set> 標記的語法與 AssignMessage 政策相同。

如果無法解析要求訊息,或要求訊息類型無效,政策會傳回錯誤。

在最簡單的範例中,您會傳遞一個變數,其中包含先前在 API Proxy 流程中填入的要求訊息:

<Request clearPayload="true" variable="myRequest"/>

您也可以在 ServiceCallout 政策中,填入傳送至外部服務的要求訊息:

<Request>
  <Set>
    <Headers>
      <Header name="Accept">application/json</Header>
    </Headers>
    <Verb>POST</Verb>
    <Payload contentType="application/json">{"message":"my test message"}</Payload>
  </Set>
  <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
</Request>
預設 如果省略 Request 元素或其任何屬性,Apigee 會指派下列預設值:

<Request clearPayload="true" variable="servicecallout.request"/>

來看看這些預設值的意義。首先,clearPayload=true 表示每次執行 ServiceCallout 政策時,都會建立新的要求物件。也就是說,要求和要求 URI 路徑絕不會重複使用。第二,預設變數名稱 servicecallout.request 是保留名稱,如果您未提供名稱,系統會將該名稱指派給要求。

如果您使用資料遮蓋功能,請務必瞭解這個預設名稱。如果省略變數名稱,則必須在遮蓋設定中加入 servicecallout.request。舉例來說,如要遮蓋授權標頭,使其不會出現在偵錯工作階段中,請在遮蓋設定中新增下列內容,擷取預設名稱:

servicecallout.request.header.Authorization。

在家狀態 選用。
類型 不適用

屬性

屬性 說明 預設 狀態
variable

要包含要求訊息的變數名稱。

servicecallout.request 選用
clearPayload

如果設為 true,系統會在要求傳送至 HTTP 目標後,清除含有要求訊息的變數,釋出要求訊息使用的記憶體。

只有在執行 ServiceCallout 後需要要求訊息時,才將 clearPayload 選項設為 false。

true 選用

<Request>/<IgnoreUnresolvedVariables> 元素

如果設為 true,政策會忽略要求中任何未解析的變數錯誤。

<Request clearPayload="true" variable="myRequest">
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
</Request>
預設 false
在家狀態 選用
類型 布林值

<Response> 元素

如果 API Proxy 邏輯需要遠端呼叫的回應才能進一步處理,請加入這個元素。

如果這個元素存在,則會指定變數名稱,其中包含從外部服務收到的回應訊息。只有在政策成功讀取整個回應時,系統才會將目標的回應指派給變數。在 PostClientFlow 之外,如果遠端呼叫因任何原因失敗,政策會傳回錯誤 (如要瞭解 PostClientFlow 行為,請參閱「自訂錯誤處理」)。

如果省略這個元素,API Proxy 就不會等待回應,API Proxy 流程會繼續執行後續的流程步驟。此外,顯而易見的是,如果沒有 Response 元素,後續步驟就無法處理目標的回應,且 Proxy 流程也無法偵測遠端呼叫是否失敗。使用 ServiceCallout 時,省略 Response 元素的常見用途:將訊息記錄到外部系統。

 <Response>calloutResponse</Response>
預設 不適用
在家狀態 選用
類型 字串

<Timeout> 元素

ServiceCallout 政策等待目標回應的時間 (以毫秒為單位)。您無法在執行階段動態設定這個值。如果 ServiceCallout 逾時,系統會傳回 HTTP 500、政策失敗,且 API Proxy 會進入錯誤狀態,如「處理錯誤」所述。

<Timeout>30000</Timeout>
預設 55000 毫秒 (55 秒),Apigee 的預設 HTTP 逾時設定
在家狀態 選用
類型 整數

<HTTPTargetConnection> 元素

提供傳輸詳細資料,例如網址、TLS/SSL 和 HTTP 屬性。請參閱 <TargetEndpoint> 設定參考資料。

<HTTPTargetConnection>
    <URL>http://example.com</URL>
    <LoadBalancer/>
    <SSLInfo/>
    <Properties/>
</HTTPTargetConnection>
預設 不適用
在家狀態 必填
類型 不適用

<HTTPTargetConnection>/<Authentication> 元素

產生 Google OAuth 2.0 或 Google 核發的 OpenID Connect 權杖,以便對 Google 服務和在特定 Google Cloud 產品 (例如 Cloud Functions 和 Cloud Run) 上執行的自訂服務發出經過驗證的呼叫。如要使用這個元素,請按照「使用 Google 驗證」一文所述的步驟進行設定和部署。設定正確的話,這項政策會為您建立驗證權杖,並將其新增至服務要求。

子元素 GoogleAccessToken 和 GoogleIDToken 可讓您設定政策,產生 Google OAuth 或 OpenID Connect 權杖。您必須根據要呼叫的服務類型,選擇其中一個子元素。

ServiceCallout 政策僅支援呼叫 HTTP 服務。

預設 不適用
必填與否 選填。
類型 複雜類型
父項元素 <HTTPTargetConnection>
子元素 <HeaderName>
<GoogleAccessToken>
<GoogleIDToken>

Authentication 元素使用下列語法:

語法

<ServiceCallout>
...
  <HTTPTargetConnection>
    <Authentication>
      <HeaderName ref="FLOW_VARIABLE">STRING</HeaderName>
      <GoogleAccessToken>
        <Scopes>
          <Scope>SCOPE</Scope>
          ...
        </Scopes>
        <!-- NOTE: The default value for LifetimeInSeconds is 3600. Change the default only
        if you want to limit the risk of leaked access tokens or improve performance. -->
        <LifetimeInSeconds ref="{variable}">INTEGER</LifetimeInSeconds>
      </GoogleAccessToken>

    --OR--
      <HeaderName ref="FLOW_VARIABLE">STRING</HeaderName>
      <GoogleIDToken>
        <Audience ref="{variable}" useTargetUrl="BOOLEAN">STRING</Audience>
        <IncludeEmail ref="{variable}">BOOLEAN</IncludeEmail>
      </GoogleIDToken>
    </Authentication>
  </HTTPTargetConnection>
</ServiceCallout>

使用 GoogleAccessToken

以下範例顯示 GoogleAccessToken 元素:

<Authentication>
  <GoogleAccessToken>
    <Scopes>
      <Scope>https://www.googleapis.com/auth/cloud-platform</Scope>
    </Scopes>
  </GoogleAccessToken>
</Authentication>

使用 GoogleIDToken

以下範例顯示 GoogleIDToken 元素:

<Authentication>
  <GoogleIDToken>
      <Audience>https://httpserver0-bar.run.app</Audience>
      <IncludeEmail>false</IncludeEmail>
  </GoogleIDToken>
</Authentication>

使用 HeaderName

以下範例顯示 HeaderName 元素:

  <Authentication>
    <HeaderName>X-Serverless-Authorization</HeaderName>
    <GoogleAccessToken>
      <Scopes>
        <Scope>"https://www.googleapis.com/auth/cloud-platform"</Scope>
      </Scopes>
    </GoogleAccessToken>
  </Authentication>

使用 LifetimeInSeconds

以下範例顯示 HeaderName 元素:

  <Authentication>
    <GoogleAccessToken>
      <Scopes>
        <Scope>"https://www.googleapis.com/auth/cloud-platform"</Scope>
      </Scopes>
      <LifetimeInSeconds ref="variable">3600</LifetimeInSeconds>
    </GoogleAccessToken>
  </Authentication>

屬性

無。

HeaderName 子元素

根據預設,如果存在驗證設定,Apigee 會產生不記名權杖,並將其插入傳送至目標系統的訊息中的 Authorization 標頭。HeaderName 元素可讓您指定其他標頭的名稱,用來存放不記名權杖。如果目標是使用 X-Serverless-Authorization 標頭的 Cloud Run 服務,這項功能就特別實用。Authorization 標頭 (如有) 會保持不變,並在要求中傳送。

預設 不適用
必填與否 否
類型 字串
父項元素 <Authentication>
子元素 無

HeaderName 元素使用下列語法:

語法

<ServiceCallout>
...
  <Authentication>
    <HeaderName ref="FLOW_VARIABLE">STRING</HeaderName>
    <GoogleAccessToken>
      ...
    </GoogleAccessToken>
  </Authentication>
  ...
</ServiceCallout>

使用靜態字串

在這個範例中,產生的不記名權杖預設會新增至傳送至目標系統的 X-Serverless-Authorization 標頭。Authorization 標頭 (如有) 則會保持不變,並在要求中傳送。

<Authentication>
  <HeaderName>X-Serverless-Authorization</HeaderName>
  <GoogleAccessToken>
    <Scopes>
      <Scope>https://www.googleapis.com/auth/cloud-platform</Scope>
    </Scopes>
  </GoogleAccessToken>
</Authentication>

使用變數參照

在這個範例中,產生的不記名權杖預設會新增至傳送至目標系統的 X-Serverless-Authorization 標頭。如果 my-variable 有值,系統會使用該值,而非預設字串。Authorization 標頭 (如有) 則會保持不變,並在要求中傳送。

<Authentication>
  <HeaderName ref='my-variable'>X-Serverless-Authorization</HeaderName>
  <GoogleAccessToken>
    <Scopes>
      <Scope>https://www.googleapis.com/auth/cloud-platform</Scope>
    </Scopes>
  </GoogleAccessToken>
</Authentication>

GoogleAccessToken 子元素

產生 Google OAuth 2.0 權杖,向 Google 服務發出經過驗證的呼叫。Google OAuth 權杖可用於呼叫多種 Google 服務,例如 Cloud Logging 和 Secret Manager。

預設 不適用
必填與否 必須提供 GoogleAccessToken 或 GoogleIDToken 子項元素。
類型 字串
父項元素 <Authentication>
子元素 <Scopes>
<LifetimeInSeconds>

GoogleAccessToken 元素使用下列語法:

語法

<ServiceCallout>
...
  <Authentication>
    <GoogleAccessToken>
      <Scopes>
        <Scope>SCOPE_1</Scope>
        ...
      </Scopes>
      <!-- NOTE: The default value for LifetimeInSeconds is 3600. We do not recommend changing
      the default unless you want to limit the risk of leaked access tokens or improve performance. -->
      <LifetimeInSeconds ref="FLOW_VARIABLE">INTEGER</LifetimeInSeconds>
    </GoogleAccessToken>
  </Authentication>
  ...
</ServiceCallout>

範例 1

以下範例顯示 GoogleAccessToken 元素:

<Authentication>
  <GoogleAccessToken>
    <Scopes>
      <Scope>https://www.googleapis.com/auth/cloud-platform</Scope>
    </Scopes>
  </GoogleAccessToken>
</Authentication>

範例 2

以下範例說明如何使用 ServiceCallout 政策連線至 Secret Manager,擷取密鑰。

<ServiceCallout name="ServiceCallout-sm">
  <Response>SecretManagerResponse</Response>
  <Timeout>30000</Timeout>
  <HTTPTargetConnection>
    <Authentication>
      <GoogleAccessToken>
        <Scopes>
          <Scope>https://www.googleapis.com/auth/cloud-platform</Scope>
        </Scopes>
      </GoogleAccessToken>
    </Authentication>
    <URL>
      https://secretmanager.googleapis.com/v1/projects/project-id/secrets/secret-id
    </URL>
  </HTTPTargetConnection>
</ServiceCallout>

範例 3

以下範例說明如何透過 ServiceCallout 政策對 Cloud Run 進行呼叫。

<ServiceCallout name="ServiceCallout-CloudRun">
  <Response>CloudRunResponse</Response>
  <Timeout>30000</Timeout>
  <HTTPTargetConnection>
    <Authentication>
      <GoogleIDToken>
        <Audience>https://cloudrun-hostname.a.run.app/test</Audience>
      </GoogleIDToken>
    </Authentication>
    <URL>https://cloudrun-hostname.a.run.app/test</URL>
  </HTTPTargetConnection>
</ServiceCallout>

Scopes 子元素

識別要納入 OAuth 2.0 存取權杖的範圍。詳情請參閱「Google API 適用的 OAuth 2.0 範圍」。您可以在這個元素下方新增一或多個 <Scope> 子元素。

預設 不適用
必填與否 必填
類型 字串
父項元素 <GoogleAccessToken>
子元素 <Scope>

Scope 子元素

指定有效的 Google API 範圍。詳情請參閱「Google API 適用的 OAuth 2.0 範圍」。

預設 不適用
必填與否 至少須輸入一個值。
類型 字串
父項元素 <Scopes>
子元素 無。

LifetimeInSeconds 子元素

指定存取權杖的生命週期長度 (以秒為單位)。

預設 3600
必填與否 選用
類型 整數
父項元素 <GoogleAccessToken>
子元素 無。

GoogleIDToken 子元素

產生 Google 核發的 OpenID Connect 權杖,向 Google 服務發出經過驗證的呼叫。

預設 不適用
必填與否 必須提供 GoogleAccessToken 或 GoogleIDToken 子元素。
類型 字串
父項元素 <Authentication>
子元素 <Audience>
<IncludeEmail>

GoogleIDToken 元素使用下列語法:

語法

<ServiceCallout>
...
  <Authentication>
    <GoogleIDToken>
        <Audience ref="{variable}" useTargetUrl="BOOLEAN">STRING</Audience>
        <IncludeEmail ref="{variable}">BOOLEAN</IncludeEmail>
    </GoogleIDToken>
  </Authentication>
</ServiceCallout>

範例 1

以下範例顯示 GoogleIDToken 元素:

<Authentication>
  <GoogleIDToken>
      <Audience>https://httpserver0-bar.run.app</Audience>
      <IncludeEmail>true</IncludeEmail>
  </GoogleIDToken>
</Authentication>

目標對象子元素

產生的驗證權杖目標對象,例如權杖授予存取權的 API 或帳戶。

如果 Audience 的值為空白,或 ref 變數未解析為有效值,且 useTargetUrl 為 true,則目標網址 (不含任何查詢參數) 會做為目標對象。根據預設,useTargetUrl 為 false。

<Audience>explicit-audience-value-here</Audience>

or:

<Audience ref='variable-name-here'/>

or:

<Audience ref='variable-name-here' useTargetUrl='true'/>

or:

<Audience useTargetUrl='true'/>
預設 不適用
必填與否 必填
類型 字串
父項元素 <GoogleIDToken>
子元素 無。

IncludeEmail 子元素

如果設為 true,產生的驗證權杖會包含服務帳戶 email 和 email_verified 宣告。

預設 false
必填與否 選用
類型 布林值
父項元素 <GoogleIDToken>
子元素 無。

<HTTPTargetConnection>/<URL> 元素

所呼叫服務的網址:

<HTTPTargetConnection>
    <URL>http://example.com</URL>
</HTTPTargetConnection>

您可以使用變數動態提供部分網址。不過,網址的通訊協定部分 (下方的 http://) 無法由變數指定。在下一個範例中,您將使用變數指定查詢參數的值:

<URL>http://example.com/forecastrss?w=${request.header.woeid}</URL>

或者,使用變數設定網址路徑的一部分:

<URL>http://example.com/{request.resourcePath}?w=${request.header.woeid}</URL>

如要使用變數指定網址的網域和通訊埠,請使用一個變數指定網域和通訊埠,並使用第二個變數指定網址的其他部分:

<URL>http://{request.dom_port}/{request.resourcePath}</URL>
預設 不適用
在家狀態 必填
類型 字串

<HTTPTargetConnection>/<SSLInfo> 元素

後端服務的 TLS/SSL 設定。如需 TLS/SSL 設定的說明,請參閱「TLS 設定選項」和「API Proxy 設定參考資料」中的「TLS/SSL TargetEndpoint 設定」。

<HTTPTargetConnection>
    <URL>https://example.com</URL>
    <SSLInfo>
        <Enabled>true</Enabled>
        <ClientAuthEnabled>true</ClientAuthEnabled>
        <KeyStore>ref://mykeystoreref</KeyStore>  ## Use of a reference is recommended
        <KeyAlias>myKey</KeyAlias>
        <TrustStore>myTruststore</TrustStore>
        <Ciphers/>
        <Protocols/>
    </SSLInfo>
</HTTPTargetConnection>
預設 不適用
在家狀態 選用
類型 不適用

<HTTPTargetConnection>/<Properties> 元素

後端服務的 HTTP 傳輸屬性。詳情請參閱「端點屬性參考資料」。

<HTTPTargetConnection>
    <URL>http://example.com</URL>
    <Properties>
        <Property name="allow.http10">true</Property>
        <Property name="request.retain.headers">
          User-Agent,Referer,Accept-Language
        </Property>
    </Properties>
</HTTPTargetConnection>
預設 不適用
在家狀態 選用
類型 不適用

<HTTPTargetConnection>/<LoadBalancer> 元素

呼叫一或多個目標伺服器,並對這些伺服器進行負載平衡。請參閱「範例」部分中的呼叫目標伺服器範例。另請參閱「後端伺服器之間的負載平衡」。另請參閱「目標端點/伺服器呼叫」,瞭解如何透過 ServiceCallout 政策和使用路由規則呼叫目標伺服器。

<HTTPTargetConnection> <LoadBalancer> <Algorithm>RoundRobin</Algorithm> <Server name="httpbin"/> <Server name="yahoo"/> </LoadBalancer> <Path>/get</Path> </HTTPTargetConnection>
預設 不適用
在家狀態 選用
類型 不適用

<LocalTargetConnection> 元素

指定本機 Proxy (即相同組織和環境中的 Proxy) 做為服務呼叫的目標。

如要進一步指定目標,請使用 <APIProxy> 和 <ProxyEndpoint> 元素,或 <Path> 元素。

<LocalTargetConnection>
   <APIProxy/>
   <ProxyEndpoint/>
   <Path/>
</LocalTargetConnection>
預設 不適用
在家狀態 必填
類型 不適用

<LocalTargetConnection>/<APIProxy> 元素

本機呼叫目標的 API Proxy 名稱。Proxy 必須與發出呼叫的 Proxy 位於相同的組織和環境。

<LocalTargetConnection>
   <APIProxy>data-manager</APIProxy>
   <ProxyEndpoint>default</ProxyEndpoint>
</LocalTargetConnection>

除了 <APIProxy> 元素外,請一併加入 <ProxyEndpoint> 元素,指定呼叫應指定的 Proxy 端點名稱。

<LocalTargetConnection>
   <APIProxy/>
   <ProxyEndpoint/>
</LocalTargetConnection>
預設 不適用
在家狀態 必填
類型 字串

<LocalTargetConnection>/<ProxyEndpoint> 元素

應做為呼叫目標的 Proxy 端點名稱。這是 <APIProxy> 元素指定的 API Proxy 中的 Proxy 端點。

<LocalTargetConnection>
   <APIProxy>data-manager</APIProxy>
   <ProxyEndpoint>default</ProxyEndpoint>
</LocalTargetConnection>
預設 不適用
在家狀態 選用
類型 不適用

<LocalTargetConnection>/<Path> 元素

目標端點的路徑。端點必須參照與發出呼叫的 Proxy 位於相同組織和環境的 Proxy。

如果您不知道或無法依賴 Proxy 名稱,請使用這個屬性,而非 <APIProxy>/<ProxyEndpoint> 配對。這條路徑可能是可靠的目標。

<LocalTargetConnection>
   <Path>/data-manager</Path>
</LocalTargetConnection>
預設 不適用
在家狀態 選用
類型 不適用

結構定義

流程變數

流程變數可讓政策和流程在執行階段,根據 HTTP 標頭、訊息內容或流程情境,動態調整行為。ServiceCallout 政策執行後,即可使用下列預先定義的流程變數。如要進一步瞭解流程變數,請參閱「流程變數參考資料」。

ServiceCallout 有自己的要求和回應,您可以透過變數存取這些資料。由於主要訊息使用 request.* 和 response.* 變數前置字元,因此請使用 myrequest.* 和 calloutResponse.* 前置字元 (ServiceCallout 設定中的預設值),取得 ServiceCallout 專屬的訊息資料。下表的第一個範例說明如何在 ServiceCallout 中取得 HTTP 標頭。

變數 說明

以下範例說明如何取得 ServiceCallout 要求和回應標頭,方法與從主要要求和回應取得標頭類似。

calloutResponse.header.HeaderName

myRequest.header.HeaderName

其中 calloutResponse 是服務呼叫中 Response 的變數名稱,myRequest 則是 Request 的變數名稱。例如:

calloutResponse.header.Content-Length

傳回 ServiceCallout 回應的 Content-Length 標頭。

範圍:從 ServiceCallout 往後
類型:字串
權限:讀取/寫入

ServiceCallout 要求或回應中的訊息標頭。舉例來說,如果 API Proxy 目標是 http://example.com,而 ServiceCallout 目標是 http://mocktarget.apigee.net,這些變數就是呼叫 http://mocktarget.apigee.net 的標頭。

servicecallout.requesturi

範圍:從 ServiceCallout 要求轉送
類型:字串
權限:讀取/寫入

ServiceCallout 政策的 TargetEndpoint URI。URI 是不含通訊協定和網域規格的 TargetEndpoint 網址。

servicecallout.{policy-name}.target.url

範圍:從 ServiceCallout 要求轉送
類型:字串
權限:讀取/寫入

ServiceCallout 的目標網址。

calloutResponse.content

其中 calloutResponse 是 ServiceCallout 設定中的<Response>變數名稱。

範圍:從 ServiceCallout 回應轉送
類型:字串
權限:讀取/寫入

ServiceCallout 的回應主體。

servicecallout.{policy-name}.expectedcn

範圍:從 ServiceCallout 要求轉送
類型:字串
權限:讀取/寫入

ServiceCallout 政策中參照的 TargetEndpoint 預期通用名稱。只有在 TargetEndpoint 參照 TLS/SSL 端點時,這個屬性才有意義。

servicecallout.{policy-name}.failed

範圍:從 ServiceCallout 回應開始
類型:布林值
權限:讀取/寫入

布林值,表示政策是否成功 (false) 或失敗 (true)。

錯誤

本節說明這項政策觸發錯誤時,Apigee 傳回的錯誤代碼和錯誤訊息,以及 Apigee 設定的錯誤變數。如果您要開發錯誤處理規則,就必須瞭解這項資訊。如需更多資訊,請參閱「關於政策錯誤的相關資訊」和「處理錯誤」。

執行階段錯誤

政策執行時可能會發生這些錯誤。

錯誤代碼 HTTP 狀態 原因 修正
steps.servicecallout.ExecutionFailed 500

發生這個錯誤的原因可能如下:

  • 要求政策處理格式錯誤或無效的輸入內容。
  • 後端目標服務傳回錯誤狀態 (預設為 4xx 或 5xx)。
steps.servicecallout.RequestVariableNotMessageType 500 政策中指定的 Request 變數不是 Message 類型。舉例來說,如果是字串或其他非訊息類型,您就會看到這項錯誤。
steps.servicecallout.RequestVariableNotRequestMessageType 500 政策中指定的 Request 變數不是 RequestMessage 類型。舉例來說,如果是回應類型,您會看到這則錯誤訊息。
googletoken.EmptyIDTokenAudience 500

<GoogleIDToken> 已啟用,但 useTargetUrl 設為 false,且在發生錯誤時,沒有直接或透過參照的方式將值提供給 <Audience>。

messaging.adaptors.http.filter.GoogleTokenGenerationFailure 500 如果 API Proxy 是使用 <Authentication> 元素進行設定,就可能發生此錯誤。可能的原因包括:
  • 透過 Proxy 部署的服務帳戶:
    • 不存在於專案中
    • 已停用
    • (僅限 Apigee hybrid) 未將 roles/iam.serviceAccountTokenCreator 角色授予 apigee-runtime 服務帳戶。
  • apigee-runtime 服務帳戶的來源專案已停用 IAMCredentials API。
  • 使用 <GoogleAccessToken> 元素,且提供一或多個無效範圍。例如,檢查是否有錯字或空白範圍。
  • 僅限 Apigee hybrid:請檢查執行階段容器的記錄,並搜尋 GoogleTokenGenerationFailure,找出可能有助於偵錯的問題的詳細錯誤訊息。

    部署錯誤

    部署含有這項政策的 Proxy 時,可能會發生這些錯誤。

    錯誤名稱 原因 修正
    URLMissing <HTTPTargetConnection> 內部缺少或空白 <URL> 元素。
    ConnectionInfoMissing 如果政策沒有 <HTTPTargetConnection> 或 <LocalTargetConnection> 元素,就會發生這項錯誤。
    InvalidTimeoutValue 如果 <Timeout> 值為負數或零,就會發生這個錯誤。
    FAILED_PRECONDITION 如果使用 <Authentication> 標記設定 Proxy 時缺少服務帳戶,就會發生這個錯誤。

    例如:

    Deployment of \"organizations/foo/apis/apiproxy/revisions/1\" requires a service
              account identity, but one was not provided with the request.
    PERMISSION_DENIED 如果 Proxy 是使用 <Authentication> 標記設定,而服務帳戶有權限問題,就會發生這個錯誤。可能的原因:
    • 服務帳戶不存在。
    • 服務帳戶並未在 Apigee 機構所屬的 Google Cloud 專案中建立。
    • 部署者確實具備服務帳戶的 iam.serviceAccounts.actAs 權限。詳情請參閱「關於服務帳戶權限」。

    錯誤變數

    這些變數會在發生執行階段錯誤時設定。詳情請參閱「關於政策錯誤的相關資訊」。

    變數 地點 範例
    fault.name="fault_name" fault_name 是錯誤名稱,如上方「執行階段錯誤」表格所列。錯誤名稱是錯誤代碼的最後一個部分。 fault.name = "RequestVariableNotMessageType"
    servicecallout.policy_name.failed policy_name 是擲回錯誤的政策的使用者指定名稱。 servicecallout.SC-GetUserData.failed = true

    錯誤回應範例

    {
       "fault":{
          "detail":{
             "errorcode":"steps.servicecallout.RequestVariableNotMessageType"
          },
          "faultstring":"ServiceCallout[ServiceCalloutGetMockResponse]:
                request variable data_str value is not of type Message"
       }
    }

    錯誤規則範例

    <FaultRule name="RequestVariableNotMessageType">
        <Step>
            <Name>AM-RequestVariableNotMessageType</Name>
        </Step>
        <Condition>(fault.name = "RequestVariableNotMessageType")</Condition>
    </FaultRule>

    相關主題