I'm trying to get data from my SendGrid account via their API. I'm using classic ASP. I found some code that works except that I need to add authorization for SendGrids API as described in their documentation.
I've found several examples that seem to suggest I need to add xmlhttp.setRequestHeader after the xmlhttp.open and before the xmlhttp.send but when I uncomment the line "xmlhttp.setRequestHeader" below I get a 500 error in my browser. Can anyone tell me now to add the authorization part to the xmlhttp object?
Edit for clarity: When I comment the line "xmlhttp.setRequestHeader..." the script runs and returns the expected json result that says I need to authenticate. When I uncomment that line I get a 500 error. I have replaced my working api key (tested with a cURL command) with a generic placeholder in my code below. The real key is in place in the file on my server.
Heres the code I'm using:
<%@ Language=VBScript %>
<%
Set xmlhttp = CreateObject("Msxml2.ServerXMLHTTP.6.0")
xmlhttp.open "GET", "https://api.sendgrid.com/v3/campaigns?limit=10&offset=0 HTTP/1.1", false
'xmlhttp.setRequestHeader "Authorization", "Bearer my-correct-sendgrid-api-key"
xmlhttp.send ""
Response.AddHeader "Content-Type", "application/json;charset=UTF-8"
Response.Charset = "UTF-8"
pageReturn = xmlhttp.responseText
Set xmlhttp = Nothing
response.write pageReturn
%>
It seems the basics are working because the code above returns the expected json results, the error message that says I need to authenticate:
{
errors: [
{
field: null,
message: "authorization required"
}
]
}
SendGrids documentation uses this example:
GET https://api.sendgrid.com/v3/resource HTTP/1.1
Authorization: Bearer Your.API.Key-HERE