HttpClient 返回 401 并带有正确的授权标头 [英] HttpClient returns 401 with correct authorisation header

查看:41
本文介绍了HttpClient 返回 401 并带有正确的授权标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 vb.net 使用 eventbrite api,我使用 HttpClient 来使用 api 但是它只返回一个 HTTP 401 Unathorised 当我使用邮递员调用具有相同标头的相同方法时,它会返回带有 HTTP 200 OK

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

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()

请求

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

响应

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
}

邮递员

推荐答案

我得到 401 Unauthorized 的原因是因为我最初调用 api 时正在查询 https://www.eventbriteapi.com/v3/events/search?organizer.id=77181XXXXX.

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.

此网址无效,应为 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)

Eventbrite 自动将我重定向到正确的 URL,但是它丢失了身份验证标头,因此未经授权.

Eventbrite automatically redirected me to the correct URL however it lost the authentication header and thus was unauthorized.

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

这篇关于HttpClient 返回 401 并带有正确的授权标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆