Unable to send email through Azure Functions
Issue Summary
I wanted to use your Hello World code to send an email through Azure Functions and I am getting an "authorization grant is invalid" error. Please take a look at the code below and let me know what I should do differently.
Here is the code.
`using System; using System.Net; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using SendGrid; using SendGrid.Helpers.Mail;
public static async Task
string apiKey = System.Environment.GetEnvironmentVariable("SEND_GRID_API_KEY", EnvironmentVariableTarget.Process);
log.Info($"apikey: {apiKey}");
dynamic sg = new SendGridAPIClient(apiKey);
Email from = new Email("[email protected]");
string subject = "Hello World from the SendGrid CSharp Library!";
Email to = new Email("[email protected]");
Content content = new Content("text/plain", "Hello, Email!");
Mail mail = new Mail(from, subject, to, content);
dynamic response = await sg.client.mail.send.post(requestBody: mail.Get());
log.Info($"response.StatusCode: {response.StatusCode}");
log.Info($"response.Body.ReadAsStringAsync().Result: {response.Body.ReadAsStringAsync().Result}");
log.Info($"response.Headers.ToString(): {response.Headers.ToString()}");
return req.CreateResponse(HttpStatusCode.OK, $"All went well.");
}`
And when I do here is the response that I get
2016-10-11T20:48:31.141 Webhook was triggered! 2016-10-11T20:48:31.141 apikey: xxxxxxxxxxxxxxxxxxxxxx 2016-10-11T20:48:31.683 response.StatusCode: Unauthorized 2016-10-11T20:48:31.699 response.Body.ReadAsStringAsync().Result: {"errors":[{"message":"The provided authorization grant is invalid, expired, or revoked","field":null,"help":null}]} 2016-10-11T20:48:31.699 response.Headers.ToString(): Connection: keep-alive X-Frame-Options: DENY Date: Tue, 11 Oct 2016 20:48:30 GMT
Any other information you want to share that is relevant to the issue being reported. Especially, why do you consider this to be a bug? What do you expect to happen instead?
Technical details:
- sendgrid-csharp v8.0.5: master (latest commit: [daa42beba7964bd4ab5b40915019a6361435331])
- .NET Version: 4.6
Hello @valokafor,
Please take a look at this section: https://github.com/sendgrid/sendgrid-csharp/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key
My guess is that you are not pulling your API Key from the environment.
@thinkingserious Thanks for your response, I actually ensured that my API key is being referenced correctly, I was able log print a log statement with the API key, I have granted this API full access and I am still getting authorization denied.
Hi @valokafor,
In that case, you may have a problem with the API key itself. I suggest you reach out to our support team so that they can take a deeper look at your specific account: https://support.sendgrid.com.
This issue has been resolved.