经典ASP xmlhttp 获取如何为sendgrid 添加身份验证api
Classic ASP xmlhttp get how to add authentication for sendgrid api
我正在尝试通过他们的 API 从我的 SendGrid 帐户获取数据。我正在使用经典 ASP。我找到 some code that works except that I need to add authorization for SendGrids API as described in their documentation.
我发现几个例子似乎表明我需要在 xmlhttp.open 之后和 xmlhttp.send 之前添加 xmlhttp.setRequestHeader 但是当我取消注释 "xmlhttp.setRequestHeader" 行时下面我的浏览器出现 500 错误。现在有人可以告诉我将授权部分添加到 xmlhttp 对象吗?
为清楚起见编辑: 当我注释行 "xmlhttp.setRequestHeader..." 时,脚本运行并且 returns 预期的 json 结果表明我需要进行验证。当我取消注释该行时,出现 500 错误。我在下面的代码中用通用占位符替换了我的工作 api 键(使用 cURL 命令测试)。真正的密钥在我服务器上的文件中。
这是我使用的代码:
<%@ 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
%>
似乎基础工作正常,因为上面的代码 returns 预期的 json 结果,错误消息说我需要验证:
{
errors: [
{
field: null,
message: "authorization required"
}
]
}
SendGrids 文档使用此示例:
GET https://api.sendgrid.com/v3/resource HTTP/1.1
Authorization: Bearer Your.API.Key-HERE
在评论some discussion之后,问题似乎出在这一行
xmlhttp.setRequestHeader "Authorization", "Bearer my-correct-sendgrid-api-key"
这是在 IServerXMLHTTPRequest
实例上设置 HTTP 授权 header 的正确方法。
需要将 Bearer my-correct-sendgrid-api-key
替换为您实际的 API 密钥,否则您可能会收到错误响应。
根据 documentation;
From How To Use The Web API v3 - Authentication
API Keys can be generated in your account - visit https://app.sendgrid.com/settings/api_keys. To use keys, you must set a plain text header named “Authorization” with the contents of the header being “Bearer XXX” where XXX is your API Secret Key.
如果您无法访问错误日志,您可以使用 On Error Resume Next
捕获错误并查看错误描述。
<%@ Language=VBScript %>
<%
Set xmlhttp = CreateObject("Msxml2.ServerXMLHTTP.6.0")
xmlhttp.open "GET", "https://api.sendgrid.com/v3/campaigns?limit=10&offset=0", false
On Error Resume Next
xmlhttp.setRequestHeader "Authorization", "Bearer xx.xxxxx.xxxx"
If Err.Number<>0 Then
Response.Write "Error:" & Err.Description & "<br>"
End If
On Error GoTo 0
xmlhttp.send ""
Response.AddHeader "Content-Type", "application/json;charset=UTF-8"
Response.Charset = "UTF-8"
pageReturn = xmlhttp.responseText
Set xmlhttp = Nothing
response.write pageReturn
%>
谢谢大家!感谢 Kul-Tigin 指出我的错误,该错误错误解释了 SendGrids 示例中 url 部分的注释 "HTTP/1.1"。
当我改变时:
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.open "GET", "https://api.sendgrid.com/v3/campaigns?limit=10&offset=0", false
xmlhttp.setRequestHeader "Authorization", "Bearer my-correct-sendgrid-api-key"
它开始工作,没有错误。这是工作代码:
<!-- language: lang-js -->
<%@ Language=VBScript %>
<%
Set xmlhttp = CreateObject("Msxml2.ServerXMLHTTP.6.0")
xmlhttp.open "GET", "https://api.sendgrid.com/v3/campaigns?limit=10&offset=0", 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
%>
SearchAndResQ 我将阅读您提供的关于错误捕获的 link。谢谢!
这适用于从经典 asp 访问远程 api。
反向正在创建一个 api en classic asp 并且客户端需要向我们发送一个令牌 auth
在这种情况下,你得到这个值:Request.ServerVariables("HTTP_Authorization")
我需要数周时间才能理解这一点,但我知道如何理解并希望与所有 asp 开发人员分享。 asp永远
我正在尝试通过他们的 API 从我的 SendGrid 帐户获取数据。我正在使用经典 ASP。我找到 some code that works except that I need to add authorization for SendGrids API as described in their documentation.
我发现几个例子似乎表明我需要在 xmlhttp.open 之后和 xmlhttp.send 之前添加 xmlhttp.setRequestHeader 但是当我取消注释 "xmlhttp.setRequestHeader" 行时下面我的浏览器出现 500 错误。现在有人可以告诉我将授权部分添加到 xmlhttp 对象吗?
为清楚起见编辑: 当我注释行 "xmlhttp.setRequestHeader..." 时,脚本运行并且 returns 预期的 json 结果表明我需要进行验证。当我取消注释该行时,出现 500 错误。我在下面的代码中用通用占位符替换了我的工作 api 键(使用 cURL 命令测试)。真正的密钥在我服务器上的文件中。
这是我使用的代码:
<%@ 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
%>
似乎基础工作正常,因为上面的代码 returns 预期的 json 结果,错误消息说我需要验证:
{
errors: [
{
field: null,
message: "authorization required"
}
]
}
SendGrids 文档使用此示例:
GET https://api.sendgrid.com/v3/resource HTTP/1.1
Authorization: Bearer Your.API.Key-HERE
在评论some discussion之后,问题似乎出在这一行
xmlhttp.setRequestHeader "Authorization", "Bearer my-correct-sendgrid-api-key"
这是在 IServerXMLHTTPRequest
实例上设置 HTTP 授权 header 的正确方法。
需要将 Bearer my-correct-sendgrid-api-key
替换为您实际的 API 密钥,否则您可能会收到错误响应。
根据 documentation;
From How To Use The Web API v3 - Authentication
API Keys can be generated in your account - visit https://app.sendgrid.com/settings/api_keys. To use keys, you must set a plain text header named “Authorization” with the contents of the header being “Bearer XXX” where XXX is your API Secret Key.
如果您无法访问错误日志,您可以使用 On Error Resume Next
捕获错误并查看错误描述。
<%@ Language=VBScript %>
<%
Set xmlhttp = CreateObject("Msxml2.ServerXMLHTTP.6.0")
xmlhttp.open "GET", "https://api.sendgrid.com/v3/campaigns?limit=10&offset=0", false
On Error Resume Next
xmlhttp.setRequestHeader "Authorization", "Bearer xx.xxxxx.xxxx"
If Err.Number<>0 Then
Response.Write "Error:" & Err.Description & "<br>"
End If
On Error GoTo 0
xmlhttp.send ""
Response.AddHeader "Content-Type", "application/json;charset=UTF-8"
Response.Charset = "UTF-8"
pageReturn = xmlhttp.responseText
Set xmlhttp = Nothing
response.write pageReturn
%>
谢谢大家!感谢 Kul-Tigin 指出我的错误,该错误错误解释了 SendGrids 示例中 url 部分的注释 "HTTP/1.1"。
当我改变时:
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.open "GET", "https://api.sendgrid.com/v3/campaigns?limit=10&offset=0", false
xmlhttp.setRequestHeader "Authorization", "Bearer my-correct-sendgrid-api-key"
它开始工作,没有错误。这是工作代码:
<!-- language: lang-js -->
<%@ Language=VBScript %>
<%
Set xmlhttp = CreateObject("Msxml2.ServerXMLHTTP.6.0")
xmlhttp.open "GET", "https://api.sendgrid.com/v3/campaigns?limit=10&offset=0", 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
%>
SearchAndResQ 我将阅读您提供的关于错误捕获的 link。谢谢!
这适用于从经典 asp 访问远程 api。
反向正在创建一个 api en classic asp 并且客户端需要向我们发送一个令牌 auth
在这种情况下,你得到这个值:Request.ServerVariables("HTTP_Authorization")
我需要数周时间才能理解这一点,但我知道如何理解并希望与所有 asp 开发人员分享。 asp永远