2

I have to send bearer at headers. I saw that I have to add hashMap with values:

val headerMap = HashMap<String, String>()
headerMap["Authorization: Bearer "] = context!!.getSharedPreferences("app_data", 0).getString("access_token", "")!!

and then send data with url:

webView.loadUrl(link, headerMap)

but as a result I see that I send the wrong format of this token:

authorization=bearer :token

How I can fix it because with that token I can't get data from page?

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
Andrew
  • 1,947
  • 2
  • 23
  • 61

1 Answers1

8

Can you try to do it this way

val bearer = "Bearer " + context!!.getSharedPreferences("app_data", 0).getString("access_token", "")!!

val headerMap = HashMap<String,String>()
headerMap["Authorization"] = bearer
webView.loadUrl(link, headerMap)

You need to think, you are using a HashMap so, means it has a Key and a Value, Key is the Header name and then the Value is the value of that Header name so in this case is :

Header name --> Authorization

Header value --> Bearer <your_access_token>

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • 1
    it didn't help me :( I sent - authorization=Bearer token, but I have to send Authorization: Bearer token – Andrew Jun 25 '19 at 09:25
  • Can you verify that your data from `SharedPreferences` is working ok? – Skizo-ozᴉʞS ツ Jun 25 '19 at 09:29
  • I recommend you to check this one : https://stackoverflow.com/questions/41440736/send-authorization-header-with-every-request-in-webview-using-okhttp-in-android/41442806 – Skizo-ozᴉʞS ツ Jun 25 '19 at 09:33
  • my data from `SharedPreferences` is working ok, and I don't receive any errors in log cat, I also saw this question, but I think that maybe I can reach appropriate result with current solution? – Andrew Jun 25 '19 at 09:37
  • Also you tried to print headerMap and you realized that the output is in lowercase and not getting the accessToken correctly? – Skizo-ozᴉʞS ツ Jun 25 '19 at 09:42
  • here is my output of headermap - Authorization=Bearer token, I have to add two dots and remove equality, and also at web view I think I will receive all at lowercase and I don't understand why ;( – Andrew Jun 25 '19 at 09:45
  • Can you please provide how do you see this text? And the changes you did into your code? – Skizo-ozᴉʞS ツ Jun 25 '19 at 09:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195497/discussion-between-andrew-goroshko-and-skizo-oziks). – Andrew Jun 25 '19 at 09:51