Home > other >  Azure SendGrid The provided authorization grant is invalid, expired, or revoked
Azure SendGrid The provided authorization grant is invalid, expired, or revoked

Time:08-24

I have created a built in azure function to send email using SendGrid from the available options in azure, here is my code

I used develop in portal option from 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": "[email protected]",
//      "from": "Azure Functions <[email protected]>"
//   }
// }
#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("[email protected]"),
    };
    
    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; }
}

I have my json as follows

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

When I manually trigger it I am getting an error as mentioned I tried changing the api key but some how it is resetting

Can some one help me how to resolve the error

CodePudding user response:

If I recall correctly, you should set the name of your SendGrid API key AppSetting to the apiKey parameter in the SendGrid binding. So instead of "apiKey": "FUNCTIONS_EXTENSION_VERSION" it should be "apiKey": "MySendGridApiKey" or replace MySendGridApiKey with whatever name you'd like to give the AppSetting.

Then, in your Azure Function AppSettings, create an AppSetting with the same name and set the value to your SendGrid API key.

You can read more about creating your API Key here, and read more about managing AppSettings in Azure Functions here.

  • Related