Behörighet saknas, fel 401, vid använding av nyss erhållen SessionKey

Added by Johan over 9 years ago

Hejsan

Har problem med att använda SessionKey. Jag får fel 401 när jag ska skicka kommandon till API't. Just nu testar jag med att hämta Nordnets "predefined instrument lists" via https://BASE_URL/API_VERSION/lists (självklart med rätt info i BaseURL + APIversion.

Kör login sekunden innan jag försöker med detta, så nyckeln är inte för gammal.

/Olof Rymark, via Johan Nygren

Jag använder denna kod (VB.net 3.5).. någon som har nåt tips:
(Skey är Session Key, requestPath är "lists")

Function GetGet(ByVal Skey As String, ByVal requestPath As String) As String
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Dim answer As String = ""

Try
' Create the web request
request = DirectCast(WebRequest.Create(nExtLogin.BASE_URL & "/" & nExtLogin.VERSION & "/" & requestPath), HttpWebRequest)
' Add authentication to request  
request.Credentials = New NetworkCredential(Skey, Skey)
' alternativt försök som inte funkar heller
'Dim nwc As NetworkCredential = New NetworkCredential(Skey, Skey)
'request.Credentials = nwc.GetCredential(New System.Uri(nExtLogin.BASE_URL & "/" & nExtLogin.VERSION & "/" & requestPath), "basic")
' Get response  
response = DirectCast(request.GetResponse(), HttpWebResponse)
' Get the response stream into a reader  
reader = New StreamReader(response.GetResponseStream())
' Console application output  
answer = reader.ReadToEnd()
Console.WriteLine(answer)
Finally
If Not response Is Nothing Then response.Close()
End Try
Return answer
End Function

Replies (7)

RE: Behörighet saknas, fel 401, vid använding av nyss erhållen SessionKey - Added by Johan over 9 years ago

Koden igen..

Function GetGet(ByVal Skey As String, ByVal requestPath As String) As String
        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader
        Dim answer As String = "" 

        Try
            ' Create the web request  
            request = DirectCast(WebRequest.Create(nExtLogin.BASE_URL & "/" & nExtLogin.VERSION & "/" & requestPath), HttpWebRequest)

            ' Add authentication to request  
            request.Credentials = New NetworkCredential(Skey, Skey)

            ' alternativt försök som inte funkar heller
            'Dim nwc As NetworkCredential = New NetworkCredential(Skey, Skey)
            'request.Credentials = nwc.GetCredential(New System.Uri(nExtLogin.BASE_URL & "/" & nExtLogin.VERSION & "/" & requestPath), "basic")

            ' Get response  
            response = DirectCast(request.GetResponse(), HttpWebResponse)

            ' Get the response stream into a reader  
            reader = New StreamReader(response.GetResponseStream())

            ' Console application output  
            answer = reader.ReadToEnd()
            Console.WriteLine(answer)
        Finally
            If Not response Is Nothing Then response.Close()
        End Try
        Return answer
    End Function

RE: Behörighet saknas, fel 401, vid använding av nyss erhållen SessionKey - Added by Johan over 9 years ago

Nu hittade jag supporten.. lägger in det som ett ärende där..

RE: Behörighet saknas, fel 401, vid använding av nyss erhållen SessionKey - Added by Nordnet Simon over 9 years ago

Hej Johan,

vi har sett liknande problem med .Net och Authentication Basic via Network Credentials.

Prova följande workaround - där header fältet sätts manuellt. Exemplet är i C#

byte[] authBytes = Encoding.UTF8.GetBytes((Skey+ ":" + Skey).ToCharArray());
request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(authBytes);

Det finns andra som har haft liknande problem där dom föreslagit liknande workaround: http://stackoverflow.com/questions/1702426/httpwebrequest-not-passing-credentials

/Simon

RE: Behörighet saknas, fel 401, vid använding av nyss erhållen SessionKey - Added by Nordnet Simon over 9 years ago

Hej Olof skulle det ha varit.

RE: Behörighet saknas, fel 401, vid använding av nyss erhållen SessionKey - Added by Johan over 9 years ago

Ditt tips löste problemet! Tack!

mvh

Olof Rymark (via Johan Nygrens)

Här är VB-koden, om det är någon annan som behöver den:

Dim authBytes As Byte() = Encoding.UTF8.GetBytes((Skey & ":" & Skey).ToCharArray)
request.Headers("Authorization") = "Basic " & Convert.ToBase64String(authBytes)

RE: Behörighet saknas, fel 401, vid använding av nyss erhållen SessionKey - Added by sunh 11 days ago

This photo is really good!!! 먹튀검증 I'm curious how you can find 스포츠중계 and upload the material I wanted 먹튀검증 Your material related to 꽁머니, 안전놀이터 is perfect. Also, don't you want to get material related to 토토사이트? <a href="https://subtitles.ee/meogtwigeomjeungsite/">먹튀검증사이트</a>I'm yours메이저토토사이트. 메이저안전놀이터 the best. We look forward to a lot of activities안전놀이터 in the future. If you have time, please visit mysite 꽁머니 along with 안전놀이터, our site 토토사이트 also has the same information. We ask for your interest in our site as well. This was the above 먹튀검증.

RE: Behörighet saknas, fel 401, vid använding av nyss erhållen SessionKey - Added by sunh 11 days ago

가장확실한먹튀검증커뮤니티 a nice comment! 먹튀검증 Nice to meet you. 먹튀신고 I live in 먹튀 a different country 먹튀검증업체 from you. 먹튀커뮤니티 Your writing will be 먹튀사이트검증 of great help to 먹튀사이트 me and to many other people living in 메이저놀이터 our country. 메이저안전놀이터추천 I was looking for a post like this, 메이저놀이터추천 but I finally found it. 메이저놀이터 Thank you so much for 사설토토 letting me read this. 메이저사설토토 This post touched me a lot. 사설토토사이트추천 Actually, 사설토토사이트 I'm running a website. 사설놀이터 I'd like to write on my site, 안전놀이터 too, referring to your post. 토토안전놀이터 I'm still lacking a lot, but I'm working hard! 안전놀이터 If you're interested, 토토사이트 please visit my site. 토토추천 I'd appreciate it if you could read my article and leave your comments. 메이저토토사이트 Thank you again for leaving me good posts, 먹튀없는토토사이트 and I will visit often and read other posts. 토토사이트추천 Be careful of COVID-19, 토토사이트 have a good day!

(1-7/7)