带有X-Auth-Token的VBA API POST

时间:2017-05-27 15:35:46

标签: excel vba excel-vba curl

我有以下curl代码,我试图转换为VBA for excel

   Endpoint : http://api.datadoctorit.com/email-validate
   Request Type : POST
   curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen( $data ),
            'X-Auth-Token: XXXXXXX'
        ) );

以下是我提出的代码:

Dim strURL As String
Dim hReq As WinHttpRequest: Set hReq = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim strResp As String
Dim Token As String
Token = mytoken
strURL = "http://api.datadoctorit.com/email-validate"
hReq.Open "POST", strURL, False
hReq.SetRequestHeader "Content-Type", "application/json"
hReq.Send "X-Auth-Token" & ":" & token
strResp = hReq.ResponseText
MsgBox strResp

我一直收到这个错误:

  

"   {""错误"":""您需要先登录或注册才能继续。""}"

1 个答案:

答案 0 :(得分:1)

使用Base 64授权(从基本授权API调用的用户名和密码生成)可以添加身份验证

只需添加以下内容:

var obj = session.Get<MyClass>("myKey") ?? new MyClass();

例如

hReq.setRequestHeader "Authorization", "Basic " & authCode

(一个真正的Auth代码长约50个字符)