简体   繁体   English

HttpClient返回带有正确授权标头的401

[英]HttpClient returns 401 with correct authorisation header

I have been trying for a while now to consume the eventbrite api with vb.net, I am using the HttpClient to consume the api however it only returns a HTTP 401 Unathorised when I call the same method with the same headers using postman it returns the expected response with a HTTP 200 OK 我一直在尝试使用vb.net来使用eventbrite api,我正在使用HttpClient来使用api,但是当我使用HTTP 401 Unathorised调用具有相同标头的相同方法时,它仅返回HTTP 401 UnathorisedHTTP 200 OK预期响应

VB.Net VB.Net

Dim objClient As New HttpClient()

objClient.BaseAddress = New Uri("https://www.eventbriteapi.com/v3/")
objClient.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer", "IODVRTRFJ5FVEXZXXXXX")

Dim objResponse As HttpResponseMessage = Await objClient.GetAsync("events/search?organizer.id=77181XXXXX")

If objResponse.IsSuccessStatusCode Then

    Dim strJSON As String = Await objResponse.Content.ReadAsStringAsync
    txtOutput.Text = strJSON

Else

    txtOutput.AppendText(objResponse.ToString + vbCrLf)
    txtOutput.AppendText(objResponse.RequestMessage.ToString + vbCrLf)

End If

objClient.Dispose()

Request 请求

Method: GET, RequestUri: 'https://www.eventbriteapi.com/events/search?organizer.id=77181XXXXX', Version: 1.1, Content: <null>, Headers:
{
  Authorization: Bearer IODVRTRFJ5FVEXZXXXXX
}

Response 响应

StatusCode: 401, ReasonPhrase: 'UNAUTHORIZED', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Transfer-Encoding: chunked
  Connection: keep-alive
  x-xss-protection: 1; mode=block
  x-content-type-options: nosniff
  Vary: Accept
  X-UA-Compatible: IE=edge
  X-Frame-Options: SAMEORIGIN
  Access-Control-Allow-Origin: *
  Access-Control-Allow-Headers: Authorization
  Date: Fri, 28 Nov 2014 14:32:02 GMT
  P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
  Set-Cookie: SS=AE3DLHTSIyq8Ey6stmsFe7sH0LwxwTjQNw; Domain=.eventbriteapi.com; httponly; Path=/; secure
  Set-Cookie: eblang=lo%3Den_US%26la%3Den-us; Domain=.eventbriteapi.com; expires=Sat, 28-Nov-2015 14:32:02 GMT; httponly; Path=/
  Set-Cookie: SP=AGQgbblV535q50zSGYa6PvdeMsiuIPDnFlsnyVrk5VIvAnFRtrUHh7AU791a46nkYXJQhH3_VZLFgHuw4j8sAYXPy3l6adKHpQ5js-vjoXyJpTp51nd4Ewnhd-lS9UlI2YL0rUaHCkLvt4_buXJOvRuN222hINBjvQBsJPrR9woApj_ic0MT0cJcNIDsY40PnEOhH8p2xijrXVZHQa6fjwemsjgJEu_Vn6NBi4UO9hBL7sLl-eetYyE; Domain=.eventbriteapi.com; httponly; Path=/
  Set-Cookie: G=v%3D1%26i%3D1429e3af-ac09-4b67-b2a3-1f4473c28bcd%26a%3D51b%26s%3DAPDvTK5mqMI40Xz80zXcPKvFx7daGz_DOA; Domain=.eventbriteapi.com; expires=Sat, 28-Nov-2015 14:32:02 GMT; httponly; Path=/
  Set-Cookie: AN=; expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Path=/
  Server: nginx
  Allow: GET
  Allow: HEAD
  Allow: OPTIONS
  Content-Type: application/json
}

Postman 邮差

在此处输入图片说明

The reason I was getting the 401 Unauthorized was because when i was calling the api originally I was querying https://www.eventbriteapi.com/v3/events/search?organizer.id=77181XXXXX . 我获得401 Unauthorized的原因是,当我最初调用api时,我正在查询https://www.eventbriteapi.com/v3/events/search?organizer.id=77181XXXXX

This URL is not valid and should be https://www.eventbriteapi.com/v3/events/search/?organizer.id=77181XXXXX (note the extra / after search) 该URL无效,应为https://www.eventbriteapi.com/v3/events/search/?organizer.id=77181XXXXX (请注意多余的/搜索后)

Eventbrite automatically redirected me to the correct URL however it lost the authentication header and thus was unauthorized. Eventbrite自动将我重定向到正确的URL,但是它丢失了身份验证标头,因此未经授权。

Working VB.Net code: 正常的VB.Net代码:

Dim objClient As New HttpClient()

Try

    objClient.BaseAddress = New Uri("https://www.eventbriteapi.com/v3/")
    objClient.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer", Context.EventBriteApiToken)

    Dim objResponse As HttpResponseMessage = Await objClient.GetAsync("users/" + Context.EventBriteUserId + "/owned_events/?page=" + intPage.ToString)
    objResponse.EnsureSuccessStatusCode() '** Throws exception

    Dim strJSON As String = Await objResponse.Content.ReadAsStringAsync
    Return JsonConvert.DeserializeObject(Of EventBrite.EventSearchResponse)(strJSON)

Catch ex As Exception

    Throw ex

Finally

    objClient.Dispose()

End Try

I had a similar issue. 我有一个类似的问题。 Building .net application and I was using the HttpClient class. 构建.net应用程序,我正在使用HttpClient类。 The url I was calling was "http" and the server redirects it to https. 我呼叫的网址是“ http”,服务器将其重定向到https。 Apparently this drops the headers. 显然,这会删除标题。 Didnt happen through postman !! 通过邮递员没有发生!

Solution: use http S 解决方案:使用http S

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 401授权错误Windows Phone 8通知 - 401 Authorisation Error Windows Phone 8 Notifications ASP.NET Webmethod总是返回401 - ASP.NET Webmethod always Returns 401 使用 httpclient vb.net 获取标头响应 - Get header response using httpclient vb.net 如何使用HttpClient在(HTTP GET)Rest请求头集合中添加&#39;Content-Type&#39;头? - how to add 'Content-Type' header in (HTTP GET) Rest request header collection using HttpClient? MS Access SQL Select语句返回正确的字段,但行中没有数据 - MS Access SQL Select Statement Returns Correct Fields but No Data in the Rows 即使 SQL 语句正确,ExecuteNonQuery 始终返回 -1 - ExecuteNonQuery always returns -1 even when the SQL statement is correct Null 数据列返回输入字符串格式不正确错误 - Null data column returns input string was not in a correct format error 即使提供正确的用户名和密码,对于reader.HasRows()来说,它仍然返回false。 有任何想法吗? - Even when supplying the correct username and password, it still returns false for if reader.HasRows(). Any ideas? httpwebrequest 401响应 - httpwebrequest 401 response HttpClient中的超时TCP连接 - Timeout TCP Connection in HttpClient
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM