我正在努力提高我对 VBA 的了解,学习 GET、POST 和其他东西,因为我已经看过很多示例,并且无法理解我做错了什么。可能是Oauth部分。
主要问题是我只是一个 Excel 人。我不是网络开发人员,所以我的知识几乎为零,而且可能我错过了很多基本的东西。
我希望这个问题不要太宽泛。
背景:我正在尝试从推文中获取 JSON 对象的 ResponseText。 信息公开而且您无需登录即可查看我想要获取的信息,并且您不需要 Twitter 帐户。
为了测试,我使用这条推文:https://twitter.com/StackOverflow/status/1273391252357201922
我想要什么:使用开发人员工具检查代码(我使用的是 Firefox),我看到了这个:
此 GET 请求返回此 ResponseText:
所以我想把那个 ResponseText 放到 VBA 中。
我的代码:在 SO 中检查不同的代码,我已经建立了这个:
Sub test()
Dim MiHttp As Object
Dim MiUrl As String
Set MiHttp = CreateObject("MSXML2.XMLHTTP")
MiUrl = "https://api.twitter.com/2/timeline/conversation/1273391252357201922.json?include_profile_interstitial_type=1&include_blocking=1&include_blocked_by=1&include_followed_by=1&include_want_retweets=1&include_mute_edge=1&include_can_dm=1&include_can_media_tag=1&skip_status=1&cards_platform=Web-12&include_cards=1&include_ext_alt_text=true&include_reply_count=1&tweet_mode=extended&include_entities=true&include_user_entities=true&include_ext_media_color=true&include_ext_media_availability=true&send_error_codes=true&simple_quoted_tweet=true&count=20&ext=mediaStats%2ChighlightedLabel&include_quote_count=true"
With MiHttp
.Open "GET", MiUrl
.Send
DoEvents
Debug.Print .responseText
End With
MiHttp.abort
Set MiHttp = Nothing
End Sub
它运行,没有编码错误,但我明白了:{"errors":[{"code":200,"message":"Forbidden."}]}
所以我尝试添加带有授权的RequestHeaders:在
.Send
之前添加这行代码:.setRequestHeader "authorization", "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA"
然后我在调试器中得到这个:{"errors":[{"message":"Rate limit exceeded","code":88}]}
所以检查了 Twitter 库中的开发人员寻找有关 Bearer 的东西和 token 的信息,我必须承认我不知所措。现在我迷路了。我认为这会很容易,因为每个人都可以从任何推文手动获取公共(public)信息,无需使用任何应用程序或登录 Twitter,但看起来我错了,我有点迷路了。
最后一个问题:我想知道是否可以通过任何方式获取 Bearer token ,然后将其应用到我的代码中,以获取 JSON responseText(处理 JSON 并了解它们将是一个完全不同的问题,超出了这里的范围)。
我想用 VBA 来实现这一点,没有其他应用程序或语言,因为我不知道。
其实我对全文都不感兴趣,只对红线包围的部分感兴趣。
寻求一些帮助,指导,光明。
在此先感谢,我希望这个问题不会太宽泛。
谢谢!
更新:测试了@ChristosLytras 的答案。我收到此错误:
2020 年 7 月更新:现在工作网址是:
https://api.twitter.com/2/timeline/conversation/1273391252357201922.json?include_profile_interstitial_type=1&include_blocking=1&include_blocked_by=1&include_followed_by=1&include_want_retweets=1&include_mute_edge=1&include_can_dm=1&include_can_media_tag=1&skip_status=1&cards_platform=Web-12&include_cards=1&include_ext_alt_text=true&include_reply_count=1&tweet_mode=extended&include_entities=true&include_user_entities=true&include_ext_media_color=true&include_ext_media_availability=true&send_error_codes=true&simple_quoted_tweet=true&count=20&ext=mediaStats%2ChighlightedLabel&include_quote_count=true
最佳答案
您必须在请求 header 中传递有效获取的访客 token 以及授权 Bearer
你会得到回应。 twitter 公共(public) API 持有者永远不会改变。
为了为每个请求获取新的有效访客 token ,您可以创建 HEAD
使用 WinHttp.WinHttpRequest.5.1
请求而不是 MSXML2.XMLHTTP
并阅读 gt
cookie 使用正则表达式,如 gt=(\d+);
.这将在每次调用时获取 cookie header 。您不能使用 MSXML2.XMLHTTP
因为它使用缓存并且每次请求 HEAD
时都不会获得新的访客 token .
使用带有 VBA 7.1 的 Excel 2013 测试的工作代码:
Dim MiHttp As Object
Dim GuestTokenRE As Object
Dim MiUrl As String
Set MiHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
Set GuestTokenRE = CreateObject("VBScript.RegExp")
MiUrl = "https://api.twitter.com/2/timeline/conversation/1273391252357201922.json?include_profile_interstitial_type=1&include_blocking=1&include_blocked_by=1&include_followed_by=1&include_want_retweets=1&include_mute_edge=1&include_can_dm=1&include_can_media_tag=1&skip_status=1&cards_platform=Web-12&include_cards=1&include_ext_alt_text=true&include_reply_count=1&tweet_mode=extended&include_entities=true&include_user_entities=true&include_ext_media_color=true&include_ext_media_availability=true&send_error_codes=true&simple_quoted_tweet=true&count=20&ext=mediaStats%2ChighlightedLabel&include_quote_count=true"
With MiHttp
' Make a HEAD request with no cache to get the Guest Token cookie
.Open "HEAD", "https://twitter.com", False
.setRequestHeader "User-Agent", "Firefox"
.setRequestHeader "Pragma", "no-cache"
.setRequestHeader "Cache-Control", "no-cache"
.Send
DoEvents
' Use a regular expression to extract guest token from response headers
GuestTokenRE.Pattern = "Set-Cookie: gt=(\d+);"
GuestTokenRE.IgnoreCase = True
Dim matches as Object
Set matches = GuestTokenRE.Execute(.getAllResponseHeaders())
If matches.Count = 1 Then
Dim guestToken As String
guestToken = matches.Item(0).Submatches.Item(0)
' Print the Guest Token for validation
Debug.Print "Got Guest Token", guestToken
' Now we have a valid guest token, make the request
.Open "GET", MiUrl, False
' Authorization Bearer is always the same
.setRequestHeader "authorization", "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA"
.setRequestHeader "x-guest-token", guestToken
.Send
DoEvents
Debug.Print "Got response", .responseText
Else
Debug.Print "Could not fetch Guest Token"
End If
End With
MiHttp.abort
Set MiHttp = Nothing
Set GuestTokenRE = Nothing
关于80072efe
错误你必须得到
WinHttp.WinHttpRequest.5.1
去工作。 80072efe
错误表示连接异常终止,您可以阅读更多信息here .我没有这样的问题,所以这些错误不是来自端点。运行代码的屏幕截图
关于excel - 尝试从 Twitter 中的 GET 请求中获取 ResponseText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62468126/