Power Apps 使用 Launch 或 Custom Connector 呼叫 Azure Functions API 的簡單記錄
Copyright Notice: This article is an original work licensed under the CC 4.0 BY-NC-ND license.
If you wish to repost this article, please include the original source link and this copyright notice.
Source link: https://v2know.com/article/1313
最近測試了從 Power Apps 呼叫 Azure Functions API 的方法。
最開始使用的是 Launch(),例如:
Launch("https://xxxxx.azurewebsites.net/api/Function1?code=xxxxx")
這種方式確實可以觸發 Azure Functions。
但是後來發現,Launch() 本質上只是「打開一個 URL」,並不是正式意義上的 API 呼叫方式。
如果只是打開頁面、下載檔案、開啟 PDF、跳轉到某個帳票 URL,Launch() 可以使用。
但如果想把 API 的回傳結果拿回 Power Apps 畫面裡使用,就需要使用 Custom Connector。
1. Launch 的用法
最簡單的寫法如下:
Launch("https://xxxxx.azurewebsites.net/api/Function1?code=xxxxx")
如果 Azure Function 需要參數,也可以拼接 URL:
Launch(
"https://xxxxx.azurewebsites.net/api/PrintReport?code=xxxxx&uketsukeNo="
& EncodeUrl(txtUketsukeNo.Text)
)
這種方式的優點是簡單,而且不需要建立 Custom Connector。
但是缺點也很明顯:
1. 只能打開 URL
2. 不適合接收 API 回傳值
3. Function Key 會直接暴露在 Power Apps 公式裡
4. 不適合做資料查詢、更新、錯誤處理
所以 Launch() 比較適合用在「打開帳票」「下載 PDF」「外部跳轉」這種場景。
2. 建立 Custom Connector
如果希望 Power Apps 像呼叫函式一樣呼叫 API,就需要建立 Custom Connector。
假設 Azure Functions 的 URL 是:
https://shori-jokyo-func-dev-epdugaazeke7cdgj.japaneast-01.azurewebsites.net/api/Function1?code=xxxxx
在 Custom Connector 裡不能直接整條 URL 填進去,而是要拆開。
全般設定
スキーマ:
HTTPS
ホスト:
shori-jokyo-func-dev-epdugaazeke7cdgj.japaneast-01.azurewebsites.net
ベース URL:
/api
注意:
Host 不要包含 https://
Base URL 填 /api
Function1 不要放在 Base URL 裡
也就是說,不要這樣填:
Host:
https://shori-jokyo-func-dev-epdugaazeke7cdgj.japaneast-01.azurewebsites.net
正確是:
Host:
shori-jokyo-func-dev-epdugaazeke7cdgj.japaneast-01.azurewebsites.net
3. Security 設定
Azure Functions 預設常用的是 Function Key,也就是 URL 裡的 code 參數:
/api/Function1?code=xxxxx
所以 Custom Connector 的 Security 要設定成 API Key。
認証タイプ:
API キー
パラメーター ラベル:
Function Key
パラメーター名:
code
パラメーターの場所:
クエリ
這裡不要直接貼真正的 Function Key。
真正的 key 是在最後建立 Connection 的時候輸入。
4. 定義 Action
在「定義」畫面新增 Action。
概要:
Function1 を呼び出す
説明:
Azure Functions の Function1 を呼び出します
操作 ID:
Function1
其中 操作 ID 很重要,因為之後 Power Apps 會用這個名稱呼叫 API。
例如:
ShoriJokyoFunction.Function1()
接著在 Request 裡選「サンプルからインポート」。
動詞:
GET
URL:
https://shori-jokyo-func-dev-epdugaazeke7cdgj.japaneast-01.azurewebsites.net/api/Function1
注意,這裡不要加:
?code=xxxxx
因為 code 已經在 Security 裡設定了。
5. 測試 Custom Connector
建立或更新 Connector 之後,進入「テスト」頁面。
先建立新的 Connection,這時才輸入 Azure Function Key,也就是原本 URL 裡:
code=xxxxx
後面的那串值。
測試成功時會看到類似:
状態:
200
Body:
Welcome to Azure Functions!
這代表 Power Automate / Power Apps 的 Custom Connector 已經可以成功呼叫 Azure Functions。
6. 在 Power Apps 裡使用
回到 Canvas App,新增資料來源,加入剛剛建立的 Custom Connector。
例如 Connector 名稱是:
ShoriJokyoFunction
按鈕的 OnSelect 可以寫:
Set(
varResult,
ShoriJokyoFunction.Function1()
)
如果 API 回傳的是文字,可以用 Label 顯示:
varResult
如果 API 回傳的是 JSON,例如:
{
"message": "ok"
}
那麼 Power Apps 裡可以這樣取值:
varResult.message
7. Custom Connector 的注意點:Premium
測試時發現,一旦 Power Apps 使用 Custom Connector,這個 App 會被判定為 Premium App。
也就是說,執行這個 App 的使用者需要 Power Apps Premium 相關授權。
畫面上會出現類似提示:
このコネクタでアプリを実行するには、
ユーザーごとの Power Apps プラン、アプリごとの Power Apps プラン、
または従量課金制の Power Apps プランが必要です。
意思是:
如果要執行使用這個 Connector 的 App,
需要 Power Apps Premium 授權。
所以 Custom Connector 雖然是比較正式、乾淨的 API 呼叫方式,但會涉及授權問題。
8. Launch 和 Custom Connector 的區別
簡單整理如下:
| 方法 | 用途 | 優點 | 缺點 |
|---|---|---|---|
| Launch | 打開 URL、PDF、帳票、下載連結 | 簡單,不需要 Custom Connector | 不適合接收 API 回傳值,Function Key 容易暴露 |
| Custom Connector | 正式呼叫 API,取得回傳資料 | 可以像函式一樣呼叫 API,能處理回傳值 | 需要 Premium 授權 |
9. 結論
如果只是想從 Power Apps 打開 Azure Functions 的 URL,例如帳票輸出、PDF 預覽、下載檔案,可以暫時使用:
Launch("https://xxxxx.azurewebsites.net/api/Function1?code=xxxxx")
如果想在 Power Apps 畫面中取得 API 回傳資料,則應該使用 Custom Connector:
Set(
varResult,
ShoriJokyoFunction.Function1()
)
但是需要注意,Custom Connector 會讓 App 變成 Premium App,需要相應授權。
所以可以這樣理解:
低成本測試 / 開啟帳票:
Launch
正式 API 連携 / 取得資料:
Custom Connector + Premium
目前如果只是做概念驗證,Launch() 可以作為最低成本方案。
如果之後要正式把 Azure Functions 當後端使用,還是應該考慮 Custom Connector 和 Power Apps Premium 授權。
This article was last edited at