首页 > 解决方案 > VBA-WEB 无法使用 httpbasic 身份验证器进行授权

问题描述

我正在尝试使用 VBA-Web 通过 Excel VBA 提交 REST API 并遇到问题

https://github.com/VBA-tools/VBA-Web

我尝试修改此示例以使用 HttpBasicAuthenticator 但不成功,它显示 HTTP 错误 401。当我使用 Postman 提交其余请求时,它很好。

https://github.com/revisohq/api-samples/tree/master/rest/excel

如果我使用这个不需要身份验证的 URL。它按预期工作

https://rest.reviso.com/customers?demo=true

Private Sub Worksheet_Activate()
    Dim Client As New WebClient
    Dim Request As New WebRequest
    Client.TimeoutMs = 30000 ' 30 seconds

    Client.BaseUrl = "https://postman-echo.com/basic-auth"
    'Client.BaseUrl = "https://rest.reviso.com/customers?demo=true"
    Request.Format = WebFormat.Json

    Dim Auth As New HttpBasicAuthenticator
    Auth.Setup _
    Username:="postman", _
    Password:="password"


    Dim Response As WebResponse
    Set Response = Client.Execute(Request)

    If Response.StatusCode <> Ok Then
        Sheets("Test").Range("A1") = Response.StatusDescription
        Exit Sub
    End If

    Dim Json As Object
    Set Json = WebHelpers.ParseJson(Response.Content)

    FillData Json

End Sub

标签: excelvbarest

解决方案


推荐阅读