ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
# [](#vba-web)VBA-Web VBA-Web(以前称为Excel-REST)使得在Windows和Mac上使用VBA轻松处理复杂的Web服务和API。 它包括对身份验证的支持,自动转换和解析JSON,使用cookie和标头等等。 [![Donate](https://camo.githubusercontent.com/525c57da54fd58534920c5c706345a734a39ce7f/68747470733a2f2f74696d68616c6c2e6769746875622e696f2f6173736574732f646f6e6174652d70617472656f6e4032782e706e67)](https://www.patreon.com/timhall) ## [](#getting-started)入门 * 下载 [最新版 (v4.1.6)](https://github.com/VBA-tools/VBA-Web/releases) * 安装或更新现有文件使用 `VBA-Web - Installer.xlsm` * 要在Excel中从头开始,`VBA-Web - Blank.xlsm`已经完成了所有设置并准备就绪 更多信息请查看 [Wiki](https://github.com/VBA-tools/VBA-Web/wiki) ## [](#upgrading)升级 要从Excel-REST升级到VBA-Web,参照[升级指南](https://github.com/VBA-tools/VBA-Web/wiki/Upgrading-from-v3-to-v4) 注意:在解析Mac的解析器问题时,已暂时从VBA-Web中删除XML支持。 Windows上仍然可以支持XML,参照[这些说明](https://github.com/VBA-tools/VBA-Web/wiki/XML-Support-in-4.0)使用自定义格式化程序。 ## [](#notes)说明 * 内置身份验证支持,支持HTTP Basic,OAuth 1.0,OAuth 2.0,Windows,Digest,Google等。 有关更多信息,请参阅 [授权](Authentication.md) * 对于代理环境,`Client.EnabledAutoProxy = True`将自动加载代理设置 * 支持自定义请求和响应格式。 请参阅[RegisterConverter](RegisterConverter.md) ## [](#examples)范例 以下示例演示如何使用Google Maps API获取两个位置之间的路线。 ### [](#getjson-example)GetJSON Example ~~~vbnet Function GetDirections(Origin As String, Destination As String) As String '// 创建WebClient以执行请求 '// 并设置一个基本URL,将所有请求附加到 Dim MapsClient As New WebClient MapsClient.BaseUrl = "https://maps.googleapis.com/maps/api/" '// 使用GetJSON帮助程序执行简单请求并使用响应 Dim Resource As String Dim Response As WebResponse Resource = "directions/json?" & _ "origin=" & Origin & _ "&destination=" & Destination & _ "&sensor=false" Set Response = MapsClient.GetJSON(Resource) '// => GET https://maps.../api/directions/json?origin=...&destination=...&sensor=false ProcessDirections Response End Function Public Sub ProcessDirections(Response As WebResponse) If Response.StatusCode = WebStatusCode.Ok Then Dim Route As Dictionary Set Route = Response.Data("routes")(1)("legs")(1) Debug.Print "It will take " & Route("duration")("text") & _ " to travel " & Route("distance")("text") & _ " from " & Route("start_address") & _ " to " & Route("end_address") Else Debug.Print "Error: " & Response.Content End If End Sub ~~~ VBA-Web中有3个主要组件: 1. `WebRequest` 用于定义复杂请求 2. `WebClient` 用于执行请求 3. `WebResponse` 用于处理回应 在上面的例子中,请求相当简单,因此我们可以跳过创建一个`WebRequest`,而是使用`Client.GetJSON`帮助器来从特定的URL获取json。 在处理响应时,我们可以查看`StatusCode`以确保请求成功,然后使用`Data`参数中的解析json从响应中提取复杂信息。 ### [](#webrequest-example)WebRequest范例 如果您希望对请求有更多控制权,则以下示例使用“WebRequest”来定义复杂请求。 ~~~vbnet Function GetDirections(Origin As String, Destination As String) As String Dim MapsClient As New WebClient MapsClient.BaseUrl = "https://maps.googleapis.com/maps/api/" '// 创建WebRequest以获取路线 Dim DirectionsRequest As New WebRequest DirectionsRequest.Resource = "directions/{format}" DirectionsRequest.Method = WebMethod.HttpGet '// 设置请求格式 '// -> 设置content-type和accept 标头并解析响应 DirectionsRequest.Format = WebFormat.Json '// 替换{format} 段 DirectionsRequest.AddUrlSegment "format", "json" '// 添加querystring到request DirectionsRequest.AddQuerystringParam "origin", Origin DirectionsRequest.AddQuerystringParam "destination", Destination DirectionsRequest.AddQuerystringParam "sensor", "false" '// => GET https://maps.../api/directions/json?origin=...&destination=...&sensor=false '// 执行请求并使用响应 Dim Response As WebResponse Set Response = MapsClient.Execute(DirectionsRequest) ProcessDirections Response End Function Public Sub ProcessDirections(Response As WebResponse) '// ... 与前面的示例相同 End Sub ~~~ 上面的例子演示了`WebRequest`提供的一些强大功能。 一些功能包括: * Url segments (用值替换资源中的{segment}) * Method (GET, POST, PUT, PATCH, DELETE) * content-type、accept headers、转换/解析请求和响应的格式(json,xml,url编码,纯文本) * QuerystringParams * Body * Cookies * Headers 更多内容, 查阅文档的[`WebRequest`](WebRequest.md)部分 ### [](#authentication-example)Authentication 范例 以下示例演示如何使用带有VBA-Web的身份验证器来查询Twitter, `TwitterAuthenticator`(在`authenticators /`[文件夹](https://github.com/VBA-tools/VBA-Web/tree/master/authenticators)中找到)使用Twitter的OAuth 1.0a身份验证及其详细信息 创建可以在[Wiki](https://github.com/VBA-tools/VBA-Web/wiki/Implementing-your-own-IAuthenticator)中[实现自己的IWebAuthenticator](%E5%AE%9E%E7%8E%B0%E8%87%AA%E5%B7%B1%E7%9A%84IWebAuthenticator.md)找到。 ~~~vbnet Function QueryTwitter(Query As String) As WebResponse Dim TwitterClient As New WebClient TwitterClient.BaseUrl = "https://api.twitter.com/1.1/" ' Setup authenticator Dim TwitterAuth As New TwitterAuthenticator TwitterAuth.Setup _ ConsumerKey:="Your consumer key", _ ConsumerSecret:="Your consumer secret" Set TwitterClient.Authenticator = TwitterAuth ' Setup query request Dim Request As New WebRequest Request.Resource = "search/tweets.json" Request.Format = WebFormat.Json Request.Method = WebMethod.HttpGet Request.AddQuerystringParam "q", Query Request.AddQuerystringParam "lang", "en" Request.AddQuerystringParam "count", 20 '// => GET https://api.twitter.com/1.1/search/tweets.json?q=...&lang=en&count=20 '// 授权承载令牌...(自动接收并添加到Twitter身份验证器) Set QueryTwitter = TwitterClient.Execute(Request) End Function ~~~ 更多信息, 查阅 [Wiki](https://github.com/VBA-tools/VBA-Web/wiki), [文档](https://vba-tools.github.com/VBA-Web/docs/), 和[范例](Examples.md) ### [](#release-notes)Release Notes View the [changelog](https://github.com/VBA-tools/VBA-Web/blob/master/CHANGELOG.md) for release notes ### [](#about)About * Author: Tim Hall * License: MIT * 机翻搬运工:gnefnuy