经典asp调用api使用addheader进行授权 [英] classic asp calling an api using addheader for authorization

查看:24
本文介绍了经典asp调用api使用addheader进行授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面调用了一个在测试模式下不需要任何授权的 api.
我们现在正在迁移到需要用户名和密码的实时环境.

api 提供者已发送以下消息:

<块引用>

要访问这些服务,请通过添加以下 HTTP 标头来发送请求.授权:Basic Base64Encode(用户名:密码")

我不确定语法是否正确,想知道是否有人可以帮助我.

最初的调用(并且运行良好)是:

Dim xmlobj,用户名,密码用户名="我的用户名"密码="我的密码"设置 xmlobj = server.CreateObject("MSXML2.DOMDocument.3.0")xmlobj.async = falsexmlobj.setProperty "ServerHTTPRequest", TruexmlObj.AddHeader "Authorization", "Basic", Base64Encode(username & ":" & password)xmlobj.load(sUrl)

以上代码报错

<上一页>/api-test.asp |20|800a000d|Type_mismatch:_'Base64Encode'

任何帮助将不胜感激.

解决方案

就像我在 commentsAuthorization 标头的语法不正确.

API 示例不期望 "Base64Encode('username','password')" 这是 API 提供的示例,用于向您展示如何获取字符串 "username:password" 和 Base64 对其进行编码,这是 Authorization 标头所期望的.

但您仍然需要有 Base64Encode() 函数定义才能使代码工作.

<块引用>

Base64Encode()MyASC() 函数取自 Base64编码VBS函数(vb编码算法),源码

这样的事情应该可以工作;

<%函数 Base64Encode(inData)'RFC1521'2001 Antonin Foller,Motobit 软件,http://Motobit.cz常量 Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"调暗 cOut, sOut, I'每组3个字节For I = 1 To Len(inData) 步骤 3调暗 nGroup、pOut、sGroup'从这 3 个字节创建一个 long.nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _&H100 * MyASC(Mid(inData, I + 1, 1)) + MyASC(Mid(inData, I + 2, 1))'Oct 将 long To 8 组拆分为 3 位nGroup = Oct(nGroup)'添加前导零nGroup = String(8 - Len(nGroup), "0") &n组'转换为base64pOut = Mid(Base64, CLng("&o" & Mid(nGroup, 1, 2)) + 1, 1) + _Mid(Base64, CLng("&o" & Mid(nGroup, 3, 2)) + 1, 1) + _Mid(Base64, CLng("&o" & Mid(nGroup, 5, 2)) + 1, 1) + _Mid(Base64, CLng("&o" & Mid(nGroup, 7, 2)) + 1, 1)'将部分添加到输出字符串sOut = sOut + pOut'为 dest 中的每 76 个字符添加一个新行 (76*3/4 = 57)'如果 (I + 2) Mod 57 = 0 那么 sOut = sOut + vbCrLf下一个选择 Case Len(inData) Mod 3案例1:'8位最终sOut = 左(sOut, Len(sOut) - 2) + "=="案例2:'16位最终sOut = 左(sOut, Len(sOut) - 1) + "="结束选择Base64Encode = sOut结束功能函数 MyASC(OneChar)如果 OneChar = "" 那么 MyASC = 0 否则 MyASC = Asc(OneChar)结束功能Dim xmlobj,用户名,密码用户名="我的用户名"密码="我的密码"设置 xmlobj = server.CreateObject("MSXML2.XMLHTTP.3.0")xmlobj.Open "GET", sUrl, Falsexmlobj.setRequestHeader "授权", "基本" &Base64Encode(用户名 & ":" & 密码)xmlobj.发送暗淡的xmldoc如果 xmlobj.status = 200 那么设置 xmldoc = Server.CreateObject("MSXML2.DOMDocument.3.0")xmldoc.Load xmlobj.ResponseXML别的Response.Write "发生错误!"万一%>

I have a page that calls an api that in test mode has not required any authorization.
We are now moving to a live environment where a username and password will be required.

The api provider has sent the following message:

To access these services please send the requests by adding following HTTP header. Authorization: Basic Base64Encode("username: password")

I'm not sure of the correct syntax and wondered if someone could help me out.

The original call (and working perfectly) is:

Dim xmlobj, username, password
username="myusername"
password="mypassword"
Set xmlobj = server.CreateObject("MSXML2.DOMDocument.3.0")
xmlobj.async = false
xmlobj.setProperty "ServerHTTPRequest", True
xmlObj.AddHeader "Authorization", "Basic", Base64Encode(username & ":" & password)
xmlobj.load(sUrl)

The above code throws an error

/api-test.asp |20|800a000d|Type_mismatch:_'Base64Encode'

Any help would be greatly appreciated.

解决方案

Like I've mentioned in the comments, the syntax for the Authorization header is incorrect.

The API example doesn't expect "Base64Encode('username','password')" this is an example supplied by the API to show you how to take the string "username:password" and Base64 encode it which is what the Authorization header is expecting.

But you still need to have the Base64Encode() function definition for the code to work.

Base64Encode() and MyASC() functions are taken from Base64 encode VBS function (vb encoder algorithm), source code

Something like this should work;

<%
Function Base64Encode(inData)
  'rfc1521
  '2001 Antonin Foller, Motobit Software, http://Motobit.cz
  Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  Dim cOut, sOut, I

  'For each group of 3 bytes
  For I = 1 To Len(inData) Step 3
    Dim nGroup, pOut, sGroup

    'Create one long from this 3 bytes.
    nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _
      &H100 * MyASC(Mid(inData, I + 1, 1)) + MyASC(Mid(inData, I + 2, 1))

    'Oct splits the long To 8 groups with 3 bits
    nGroup = Oct(nGroup)

    'Add leading zeros
    nGroup = String(8 - Len(nGroup), "0") & nGroup

    'Convert To base64
    pOut = Mid(Base64, CLng("&o" & Mid(nGroup, 1, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 3, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 5, 2)) + 1, 1) + _
      Mid(Base64, CLng("&o" & Mid(nGroup, 7, 2)) + 1, 1)

    'Add the part To OutPut string
    sOut = sOut + pOut

    'Add a new line For Each 76 chars In dest (76*3/4 = 57)
    'If (I + 2) Mod 57 = 0 Then sOut = sOut + vbCrLf
  Next
  Select Case Len(inData) Mod 3
    Case 1: '8 bit final
      sOut = Left(sOut, Len(sOut) - 2) + "=="
    Case 2: '16 bit final
      sOut = Left(sOut, Len(sOut) - 1) + "="
  End Select
  Base64Encode = sOut
End Function

Function MyASC(OneChar)
  If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)
End Function

Dim xmlobj, username, password
username="myusername"
password="mypassword"
Set xmlobj = server.CreateObject("MSXML2.XMLHTTP.3.0")
xmlobj.Open "GET", sUrl, False
xmlobj.setRequestHeader "Authorization", "Basic " & Base64Encode(username & ":" & password)
xmlobj.Send

Dim xmldoc
If xmlobj.status = 200 Then
  Set xmldoc = Server.CreateObject("MSXML2.DOMDocument.3.0")
  xmldoc.Load xmlobj.ResponseXML
Else
  Response.Write "An error occurred!"
End If
%>

这篇关于经典asp调用api使用addheader进行授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆