Sendgrid-csharp: i always getting unauthorized response code

Created on 14 Jul 2016  路  18Comments  路  Source: sendgrid/sendgrid-csharp

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("[email protected]");
            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
question

Most helpful comment

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!

All 18 comments

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!

Hello @thinkingserious i am using the same

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

Hey @loganwasif005 I removed your API Key from your comment ...

You may want to go and create a new key and delete your previous key, because who knows who saw this key and might try to send email or make API calls with it.

@mbernier i am not using the full key in the end you see i hide the value using ------and x

It was just most of the key and I got worried that it was the whole key!!

@loganwasif005,

For the first parameter to Environment.GetEnvironmentVariable, it is not your SendGrid API Key (that's what you have now). It's the name of the variable that holds your key in the environment.

you mean i have to pass API KEY ID ? instead of code generated by Application when we create the Key ?

please let me know if more information is required @thinkingserious

btw i delete the other key

i always get following response.StatusCode = Unauthorized anyhelp

For the purpose of testing, can you do:

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

actually, I m bit confused [API key was here] = API KEY ID or the code which popup is generated when we create the new key is created

The code which the popup generated that starts with SG.

yes i am using the same code
string apiKey = Environment.GetEnvironmentVariable("NAME_OF_ENVIRONMENT_VARIABLE_THAT_HOLDS_THE_SENDGRID_API_KEY_VALUE", EnvironmentVariableTarget.User); but still i get StatusCode = Unauthorized

Please remove that code and do:

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

thanks a lot it worked :+1:

Hi..
I am also trying to send the mail using the sendgrid.. but I am also getting the "Unauthorize" everytime..

I am not sure how to set the apiKey?
Is it -->
string apiKey = "[FIRST PORTION OF THE API KEY with out SG.]----------X";
or
string apiKey = "[COMPLETE API KEY with out SG.]";

I tried both but none of them succeed..

Please advise.

Thanks

Hello @mantriv,

You need to use the entire API key, including the SG. portion.

With Best Regards,

Elmer

It worked when used my api directly without using environment variable
like this
string apiKey = "SG....."

Was this page helpful?
0 / 5 - 0 ratings