vb.net 必须使用适当的属性或方法 - TwitchAPI 修改“接受”标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29514443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:59:20  来源:igfitidea点击:

The 'Accept' header must be modified using the appropriate property or method - TwitchAPI

vb.netwinformsexceptionhttpwebrequest

提问by ElementalTree

My problem, as you can see in the title, is I get an exception when adding the header. What I'm trying to do is send an authorization request to the public TwitchAPI. Here's the request that I'm trying to translate:

正如您在标题中看到的,我的问题是在添加标题时出现异常。我想要做的是向公共TwitchAPI发送授权请求。这是我要翻译的请求:

curl -H 'Accept: application/vnd.twitchtv.v3+json' 
-H 'Authorization: OAuth <access_token>' \ 
-X GET https://api.twitch.tv/kraken/channel

It's when I add the Accept header where this exception pops up in my face (title). I'm not sure if I've translated this correctly but this is the code I have right now:

当我添加 Accept 标头时,这个异常就会出现在我的脸上(标题)。我不确定我是否正确翻译了它,但这是我现在拥有的代码:

Dim wr = CType(WebRequest.Create("https://api.twitch.tv/kraken/channel"), HttpWebRequest)
wr.Method = "GET"
wr.Headers.Add("Authorization: OAuth <oauth_token>")
wr.Headers.Add("Accept: application/vnd.twitchtv.v3+json")
Return CType(wr.GetResponse(), HttpWebResponse)

where oauth_token is my access token, anyone who could solve this for me? Really worked my ass off trying to figure out such a simple thing, thanks!

oauth_token 是我的访问令牌,谁能帮我解决这个问题?试图弄清楚这么简单的事情真的让我很费劲,谢谢!

  • Oh and also, when I remove the header (which I actually think is unnecessary) it says im unauthorized, using the correct access token.
  • 哦,还有,当我删除标题(我实际上认为这是不必要的)时,它说我未经授权,使用了正确的访问令牌。

回答by John O.

The HttpWebRequestclass has a specific Acceptproperty for setting the 'Accept' header

HttpWebRequest班有一个特定Accept设置的“接受”头属性

Dim wr = CType(WebRequest.Create("https://api.twitch.tv/kraken/channel"), HttpWebRequest)
wr.Method = "GET"
wr.Headers.Add("Authorization: OAuth <oauth_token>")
wr.Accept = "application/vnd.twitchtv.v3+json"
Return CType(wr.GetResponse(), HttpWebResponse)