【问题标题】:How to configure the APNS.Certificate in the arm template如何在arm模板中配置APNS.Certificate
【发布时间】:2017-04-12 02:03:21
【问题描述】:

我正在使用以下 azuredeploy.json 文件在 Azure 云上设置通知中心。

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "Gcm.GoogleApiKey": {
            "type": "string",
            "metadata": {
                "description": "Google Cloud Messaging API Key"
            },
            "defaultValue": "AIzaSyAyp9MernKgMS3wFNM3yNWByiP-TaGrqEg"
        },
        "APNS.Certificate": {
            "type": "string",
            "metadata": {
                "description": "A certificate (in base 64 format) provided by Apple on the iOS Provisioning Portal"
            },
            "defaultValue": ""
        },
        "APNS.certificateKey": {
            "type": "string",
            "metadata": {
                "description": "The Certificate Key provided by the iOS Provisioning Portal when registering the application"
            },
            "defaultValue": "ce469bf21dfa7b9d595d4999bfaca8a94ea47e46"
        },
        "APNS.endpoint": {
            "type": "string",
            "metadata": {
                "description": "The APNS endpoint to which our service connects. This is one of two values: gateway.sandbox.push.apple.com for the sandbox endpoint or gateway.push.apple.com, for the production endpoint. Any other value is invalid."
            },
            "allowedValues": [
                "gateway.sandbox.push.apple.com",
                "gateway.push.apple.com"
            ],
            "defaultValue": "gateway.push.apple.com"
        }
    },
    "variables": {
        "hubVersion": "[providers('Microsoft.NotificationHubs', 'namespaces').apiVersions[0]]",
        "notificationHubNamespace": "[concat('hubv2', uniqueString(resourceGroup().id))]",
        "notificationHubName": "notificationhub"
    },
    "resources": [
        {
            "name": "[variables('NotificationHubNamespace')]",
            "location": "[resourceGroup().location]",
            "type": "Microsoft.NotificationHubs/namespaces",
            "apiVersion": "[variables('hubVersion')]",
            "comments": "Notification hub namespace",
            "properties": {
                "namespaceType": "NotificationHub"
            },
            "resources": [
                {
                    "name": "[concat(variables('NotificationHubNamespace'),'/',variables('NotificationHubName'))]",
                    "location": "[resourceGroup().location]",
                    "type": "Microsoft.NotificationHubs/namespaces/notificationHubs",
                    "apiVersion": "[variables('hubVersion')]",
                    "properties": {
                        "GcmCredential": {
                            "properties": {
                                "googleApiKey": "[parameters('Gcm.GoogleApiKey')]",
                                "gcmEndpoint": "https://android.googleapis.com/gcm/send"
                            }
                        }
                    },
                    "dependsOn": [
                        "[variables('NotificationHubNamespace')]"
                    ]
                }
            ]
        }
    ],
    "outputs": {
    }
}

现在我尝试使用以下 sn-p 设置苹果推送通知服务:

"apnsCredential": {
              "properties": {
                "apnsCertificate": "[parameters('APNS.Certificate')]",
                "certificateKey": "[parameters('APNS.certificateKey')]",
                "endpoint": " gateway.sandbox.push.apple.com or gateway.push.apple.com",
              }
            }

通过上述更改,我使用 powershell 命令提示符执行了 Deploy-AzureResourceGroup.ps1,并在执行时收到错误消息“错误请求”

谁能帮我解决这个问题。

【问题讨论】:

  • 需要比“错误请求”更多的信息
  • 你有没有得到这个工作?

标签: powershell azure azure-powershell azure-notificationhub arm-template


【解决方案1】:

添加正确的 APNS.Certificate 和 APNS.certificateKey。 尝试验证您的详细信息失败,因此请求错误。 您需要一个 base 64 格式的 APNS.Certificate。

APNS.Certificate:

这是基于 64 字符串格式的 Apple 推送通知证书。

您可以使用 PowerShell 像这样转换证书(然后从输出文件“MyPushCert.txt”中复制密钥并使用它。):

$fileContentBytes = get-content ‘Apple_Certificate.p12’ -Encoding Byte

[System.Convert]::ToBase64String($fileContentBytes) | Out-File ‘MyPushCert.txt’

APNS.certificateKey:

这是您在导出证书时指定的密码。(您在创建证书时在 Apple 上创建的密码。)

【讨论】:

  • 谢谢!您的 Powershell 脚本让我摆脱了 2 天的故障排除!
  • 在较新的 powershell get-content 中不支持 -Encoding Byte:$fileContentBytes = get-content ‘Apple_Certificate.p12’ -AsByteStream
【解决方案2】:

如果不了解您的环境/设置的更多信息,就不可能确切地知道是什么原因造成的。根据this post 的说法,一个可能的问题是您的密码有多强:

在拔掉头发几个小时后,什么也没得到 除了“Bad Request”,我终于想到了使用更强的密码 比“pass@word1”。我会被诅咒的,它奏效了。不仅如此,而且 使用 Azure 资源管理器进行预配是异步的,因此您的 脚本完成的时间比以前快了很多,因为虚拟机 并行提供。

帖子推荐通过Troubleshooting common Azure deployment errors with Azure Resource Manager

【讨论】:

    【解决方案3】:

    我不确定您是否应该为您的模板动态设置apiVersion。它们因您部署的内容而异。

    Best Practices:

    避免对资源类型的 API 版本使用参数或变量。资源属性和值可能因版本号而异。当 API 版本设置为参数或变量时,代码编辑器中的 IntelliSense 无法确定正确的架构。而是在模板中硬编码 API 版本。

    通知中心的正确apiVersion 似乎是2015-04-01https://github.com/Azure/azure-resource-manager-schemas/blob/master/schemas/2015-04-01/Microsoft.NotificationHubs.json

    【讨论】:

      【解决方案4】:

      我不得不使用 PowerShell 来解决这个问题。 部分想法来自这里:https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-deploy-and-manage-powershell

      下面是脚本,它在本地测试并且正在运行。 Microsoft.Azure.NotificationHubs - 使用版本 1.0.9。 在使用 ARM 模板创建通知中心后,我们将其作为 PowerShell 任务/步骤之一用于 VSTS 发布。

      Login-AzureRmAccount
      Select-AzureRmSubscription -SubscriptionName my-subscription-here
      
      Write-Host "Begin process..."
      
      try
      {
          # Make sure to reference the latest version of Microsoft.Azure.NotificationHubs.dll
          Write-Host "Adding the [Microsoft.Azure.NotificationHubs.dll] assembly to the script...";
          $scriptPath = Split-Path (Get-Variable MyInvocation -Scope 0).Value.MyCommand.Path;
          $packagesFolder = $scriptPath + "\packs";
          Write-Host $packagesFolder;
          $assembly = Get-ChildItem $packagesFolder -Include "Microsoft.Azure.NotificationHubs.dll" -Recurse;
          write-Host $assembly.FullName;
          Add-Type -Path $assembly.FullName;
      
          Write-Host "The [Microsoft.Azure.NotificationHubs.dll] assembly has been successfully added to the script.";
      
          # Create requered variables
          $HubNamespace = "hub-namespace";
          $HubName = "hub-name";
          $ResourceGroup = "resource-group";
      
          $GcmApiKey = "api key here";
          # Possible values: gateway.push.apple.com, gateway.sandbox.push.apple.com
          $ApnsEndpoint = "gateway.push.apple.com";
          # A certificate (in base 64 format) provided by Apple on the iOS Provisioning Portal
          $ApnsCertificate = "base 64 certificate here";
          $ApnsCertificateKey = "certificate key/password here";
      
          $GcmCredential = New-Object -TypeName Microsoft.Azure.NotificationHubs.GcmCredential -ArgumentList $GcmApiKey;
          $ApnsCredential = New-Object -TypeName Microsoft.Azure.NotificationHubs.ApnsCredential;
          $ApnsCredential.Endpoint = $ApnsEndpoint;
          $ApnsCredential.ApnsCertificate = $ApnsCertificate;
          $ApnsCredential.CertificateKey = $ApnsCertificateKey;
      
          # Query the namespace
          $FoundNamespaces = Get-AzureRmNotificationHubsNamespace -Namespace $HubNamespace -ResourceGroup $ResourceGroup
      
          # Check if the namespace already exists
          if ($FoundNamespaces -and $FoundNamespaces.Length -eq 1)
          {
              $CurrentNamespace = $FoundNamespaces[0];
              Write-Host "The namespace [$HubNamespace] in the [$($CurrentNamespace.Location)] region was found.";
      
              $HubListKeys = Get-AzureRmNotificationHubListKeys -Namespace $HubNamespace -ResourceGroup $ResourceGroup -NotificationHub $HubName -AuthorizationRule DefaultFullSharedAccessSignature;
              # Check to see if the Notification Hub exists
              if ($HubListKeys)
              {
                  # Create the NamespaceManager object used to update the notification hub
                  Write-Host "Creating a NamespaceManager object for the [$HubNamespace] namespace...";
                  $NamespaceManager = [Microsoft.Azure.NotificationHubs.NamespaceManager]::CreateFromConnectionString($HubListKeys.PrimaryConnectionString);
                  Write-Host "NamespaceManager object for the [$HubNamespace] namespace has been successfully created.";
      
                  # Update notification hub with new details
                  Write-Host "The [$Path] notification hub already exists in the [$HubNamespace] namespace."  ;
                  $NHDescription = $NamespaceManager.GetNotificationHub($HubName);
                  $NHDescription.GcmCredential = $GcmCredential;
                  $NHDescription.ApnsCredential = $ApnsCredential;
                  $NHDescription = $NamespaceManager.UpdateNotificationHub($NHDescription);
                  Write-Host "The [$HubName] notification hub was updated";
              }
              else
              {
                  Write-Host "The [$HubName] notification hub does not exist."
              }
          }
          else
          {
              Write-Host "The [$HubNamespace] namespace does not exist."
          }
      }
      catch [System.Exception]
      {
          Write-Error($_.Exception.Message)
      }
      

      希望对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-08
        • 1970-01-01
        相关资源
        最近更新 更多