c# - Azure SendGrid 提供的授权无效、已过期或已撤销

标签 c# azure asp.net-core azure-functions sendgrid

我创建了一个内置的 azure 函数,可以使用 SendGrid 从 azure 中的可用选项发送电子邮件,这是我的代码

我使用了azure的门户选项中的开发

// The 'From' and 'To' fields are automatically populated with the values specified by the binding settings.
//
// You can also optionally configure the default From/To addresses globally via host.config, e.g.:
//
// {
//   "sendGrid": {
//      "to": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cdb8bea8bf8da5a2beb9e3aea2a0" rel="noreferrer noopener nofollow">[email protected]</a>",
//      "from": "Azure Functions <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="790a181409151c0a391f0c171a0d1016170a571a1614" rel="noreferrer noopener nofollow">[email protected]</a>>"
//   }
// }
#r "SendGrid"

using System;
using SendGrid.Helpers.Mail;
using Microsoft.Azure.WebJobs.Host;

public static SendGridMessage Run(Order order, ILogger log)
{
    log.LogInformation($"C# Queue trigger function processed order: {order.OrderId}");

    SendGridMessage message = new SendGridMessage()
    {
        Subject = $"Thanks for your order (#{order.OrderId})!",
        From = new EmailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6006120f0d200f15140c0f0f0b4e030f0d" rel="noreferrer noopener nofollow">[email protected]</a>"),
    };
    
    message.AddTo(order.CustomerEmail, order.CustomerName);
    message.AddContent("text/plain", $"{order.CustomerName}, your order ({order.OrderId}) is being processed!");
    return message;
}
public class Order
{
    public string OrderId { get; set; }
    public string CustomerName { get; set; }
    public string CustomerEmail { get; set; }
}

我的 json 如下

{
  "bindings": [
    {
      "type": "queueTrigger",
      "name": "order",
      "direction": "in",
      "queueName": "samples-orders"
    },
    {
      "type": "sendGrid",
      "name": "$return",
      "direction": "out",
      "apiKey": "FUNCTIONS_EXTENSION_VERSION"
    }
  ]
}

当我手动触发它时,我收到一个错误,如上所述,我尝试更改 api key ,但它是如何重置的

谁能帮我解决这个错误

最佳答案

如果我没记错的话,您应该将 SendGrid API key AppSetting 的名称设置为 SendGrid 绑定(bind)中的 apiKey 参数。 因此,不应将 "apiKey": "FUNCTIONS_EXTENSION_VERSION" 改为 "apiKey": "MySendGridApiKey" 或将 MySendGridApiKey 替换为您想要的任何名称喜欢给AppSetting。

然后,在您的 Azure Function AppSettings 中,创建一个具有相同名称的 AppSetting,并将值设置为您的 SendGrid API key 。

您可以阅读有关 creating your API Key here 的更多信息,并阅读有关 managing AppSettings in Azure Functions here 的更多信息.

关于c# - Azure SendGrid 提供的授权无效、已过期或已撤销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73452817/

相关文章:

c# - ASP .NET CORE 找不到包含自定义程序集的文件或程序集

c# - 使用事务内存数据库进行单元测试时,如何抑制 InMemoryEventId.TransactionIgnoredWarning?

c# - ASP.NET Core API 仅返回列表的第一个结果

c# - 组合框项目源 = ObservableCollection & 我需要在顶部有一个 '-None-' 虚拟条目

c# - Xamarin UI 测试 "1 is not a supported code page."

rest - 通过 API 从 Azure Application Insights 获取信息

reactjs - React App 部署到 Azure Web 的流程?

c# - 使用 EF/Code First/Repository Pattern/Table Per Type (TPT) 时有良好的编程原则吗?

c# - 如何使方法的返回类型通用?

powershell - azure函数powershell从存储队列读取并写入存储表