sendgrid-csharp: i always getting unauthorized response code

Issue Summary

i have the valid key but i am always getting unauthorized in response code

please check the code below

St``` ring apiKey = Environment.GetEnvironmentVariable(“[API KEY WAS HERE]----------X”, EnvironmentVariableTarget.User); dynamic sg = new SendGridAPIClient(apiKey, “https://api.sendgrid.com”);


            Email from = new Email("no-reply@runapp.com");
            String subject = Emailsubject;
            Email to = new Email(toEmail);
            Content content = new Content("text/html",body);
            Mail mail = new Mail(from, subject, to, content);

            String ret = mail.Get();

            string requestBody = ret;
            dynamic response = sg.client.mail.send.post(requestBody: requestBody);
            Console.WriteLine(response.StatusCode);
            Console.WriteLine(response.Body.ReadAsStringAsync().Result);
            Console.WriteLine(response.Headers.ToString());


#### Steps to Reproduce


#### Technical details:

* sendgrid-csharp Version: master (latest commit: [commit number])
* .NET Version: 4.5.2

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 20 (8 by maintainers)

Most upvoted comments

It worked when used my api directly without using environment variable like this string apiKey = “SG…”

Hello @loganwasif005,

If you want to use the SendGrid API Key directly in your code (not recommended), then please do the following:

string apiKey = "[API KEY WAS HERE]----------X";

The recommended method is to create an environment variable and reference it as follows:

string apiKey = Environment.GetEnvironmentVariable("NAME_OF_ENVIRONMENT_VARIABLE_THAT_HOLDS_THE_SENDGRID_API_KEY_VALUE", EnvironmentVariableTarget.User);

Thanks!

thanks a lot it worked 👍

Please remove that code and do:

string apiKey = "SG.----------X";

Hello @thinkingserious i am using the same

ring apiKey = Environment.GetEnvironmentVariable([YOUR API KEY WAS HERE]----------X", EnvironmentVariableTarget.User);