如何为具有 API 连接到 Gmail 的逻辑应用程序创建 ARM 模板?

How to create ARM template for logic App with API connection to Gmail?

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "logicAppName": {
      "type": "string",
      "defaultValue": "la-send-mail",
      "metadata": {
        "description": "Name of the Logic App."
      }
    },
    "logicAppLocation": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location of the Logic App."
      }
    },
    "gmail_name": {
      "type": "string",
      "defaultValue": "gmail"
    },
    "gmail_displayName": {
      "type": "string",
      "defaultValue": "roman.dovhanyk@gmail.com"
    }
  },
  "variables": {},
  "resources": [
    {
      "type": "Microsoft.Logic/workflows",
      "apiVersion": "2016-06-01",
      "name": "[parameters('logicAppName')]",
      "location": "[parameters('logicAppLocation')]",
      "dependsOn": [
        "[resourceId('Microsoft.Web/connections', parameters('gmail_name'))]"
      ],
      "properties": {
        "definition": {
          "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
          "contentVersion": "1.0.0.0",
          "parameters": {
            "$connections": {
              "defaultValue": {},
              "type": "Object"
            }
          },
          "triggers": {
            "manual": {
              "type": "Request",
              "kind": "Http",
              "inputs": {
                "schema": {
                  "properties": {
                    "body": {
                      "type": "string"
                    },
                    "bodyHTML": {
                      "type": "string"
                    },
                    "ccAddress": {
                      "type": "string"
                    },
                    "color": {
                      "type": "string"
                    },
                    "datafactoryName": {
                      "type": "string"
                    },
                    "pipelineName": {
                      "type": "string"
                    },
                    "pipelineRunId": {
                      "type": "string"
                    },
                    "time": {
                      "type": "string"
                    },
                    "title": {
                      "type": "string"
                    },
                    "toAddress": {
                      "type": "string"
                    }
                  },
                  "type": "object"
                }
              }
            }
          },
          "actions": {
            "Initialize_variable": {
              "runAfter": {},
              "type": "InitializeVariable",
              "inputs": {
                "variables": [
                  {
                    "name": "HTMLBody",
                    "type": "string",
                    "value": "<div>\n<h1 style=\"Color:@{triggerBody()?['color']};\"> Executed successfully </h1>\n<hr/>\nData Factory Name: <b>@{triggerBody()?['datafactoryName']}</b><br/>\nPipeline Name: <b>@{triggerBody()?['pipelineName']}</b><br/>\nPipeline Run Id<b>@{triggerBody()?['pipelineRunId']}</b><br/>\nTime: <b>@{triggerBody()?['time']}</b><br/>\n<hr/>\n<p>@{triggerBody()?['body']}</p>\n<div>@{triggerBody()?['bodyHTML']}</div>\n</div>"
                  }
                ]
              }
            },
            "Send_email_(V2)": {
              "runAfter": {
                "Initialize_variable": [
                  "Succeeded"
                ]
              },
              "type": "ApiConnection",
              "inputs": {
                "body": {
                  "Body": "<p>@{variables('HTMLBody')}</p>",
                  "Cc": "@triggerBody()?['ccAddress']",
                  "Subject": "@triggerBody()?['title']",
                  "To": "@triggerBody()?['toAddress']"
                },
                "host": {
                  "connection": {
                    "name": "@parameters('$connections')['gmail']['connectionId']"
                  }
                },
                "method": "post",
                "path": "/v2/Mail"
              }
            }
          },
          "outputs": {}
        },
        "parameters": {
          "$connections": {
            "value": {
              "gmail": {
                "id": "[concat('/subscriptions/',subscription().subscriptionId,'/providers/Microsoft.Web/locations/',parameters('logicAppLocation'),'/managedApis/gmail')]",
                "connectionId": "[resourceId('Microsoft.Web/connections', parameters('gmail_name'))]",
                "connectionName": "[parameters('gmail_name')]"
              }
            }
          }
        }
      }
    },
    {
      "type": "Microsoft.Web/connections",
      "apiVersion": "2016-06-01",
      "location": "[parameters('logicAppLocation')]",
      "name": "[parameters('gmail_name')]",
      "properties": {
        "api": {
          "id": "[concat('/subscriptions/',subscription().subscriptionId,'/providers/Microsoft.Web/locations/',parameters('logicAppLocation'),'/managedApis/gmail')]"
        },
        "displayName": "[parameters('gmail_displayName')]"
      }
    }
  ],
  "outputs": {}
}

这是我使用的模板,它总是给出 “部署 'la-send-mail-1_2' 失败,出现错误。显示 1 个错误,共 1 个错误。状态消息:工作流 'la-send-mail' 上的操作无法完成,因为它包含到的连接'gmail' 个无效的连接器。请重新授权连接并重试。(代码:GmailConnectorPolicyViolation)”错误

我正在 运行 从简单的 PowerShell 脚本进行部署。 谁能帮我解决这个问题

谢谢Thomas。将您的建议作为答案发布,以帮助其他社区成员。

Authorize 文档将帮助您授权 OAuth 连接。

  • 通过在 Azure 门户或 Visual Studio 中的逻辑应用设计器中打开逻辑应用,手动授权 OAuth 连接。当您授权您的连接时,可能会出现一个确认页面以允许您访问。

对于与 ARM 模板的 Oauth 连接,您需要编写脚本。但最简单的方法是手动创建这些连接然后部署 ARM。

请参阅 Logic App Connection Auth 文档以获取更多信息。

  • 此脚本将为 OAuth 逻辑应用程序连接器检索连接许可 link(并且还可以同时创建连接)。然后它将打开同意 link 并完成授权以启用连接。这可以在部署连接后使用,以确保逻辑应用程序端到端地工作。