Android Question How can I get the API key to get recognized?

Good day!

I am very new to B4a, and I am trying to get a token from my colleague's API. Below are the sample parameters, and the error I encountered. Your help is very much appreciated.

Acquire CSRF
GET Parameters:
URL: https://xxxxxxxxxxx.ph/api/user/csrf
Key: apiKey
Value: xxxxxxxxxx


B4X:
Dim j As HttpJob
    
    j.Initialize("", Me)
    
    j.Download("https://xxxxxxxxxxx.ph/api/user/csrf")
    j.GetRequest.SetHeader("Authorization", "Basic Og==")
    j.GetRequest.SetHeader("apiKey", "xxxxxxxxxx")
    
    
    Wait For (j) JobDone (j As HttpJob)
    If j.Success Then
        Dim jp As JSONParser
        jp.Initialize(j.GetString)
        Log(jp)
        
    Else
        Log("Error: " & j.ErrorMessage)
    End If
    j.Release

ERROR

Logger connected to: HUAWEI STK-L22
--------- beginning of system
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
ResponseError. Reason: , Response: {"status":false,"error":"Set API Key in Request Header"}
Error: {"status":false,"error":"Set API Key in Request Header"}
 
The only thing that I can say is that the server expects something else. Maybe the header key should be Key.
It returns the same error.

Not sure if this will help. Attached are the parameters I am using when testing in Postman. Please see attached screenshots. Thanks!
 
Upvote 0
Maybe you need to change the content type (GetRequest.SetContentType)?

Do you get a different error if you send a wrong authorization value?
Good day!

I tried adding
B4X:
j.GetRequest.SetContentEncoding("json")
But it is still giving the same error:

ERROR

Logger connected to: HUAWEI STK-L22
--------- beginning of system
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
ResponseError. Reason: , Response: {"status":false,"error":"Set API Key in Request Header"}
Error: {"status":false,"error":"Set API Key in Request Header"}
 
Upvote 0
You set it incorrectly. It might be "application/json" though you don't need to guess. Carefully check the headers in postman.
Tried this as well, j.GetRequest.SetContentEncoding("application/json"). But it still does not work.

Also, the keys and values I posted was also used by my colleague on a different Android IDE, and it worked fine on my colleague's side.
 
Upvote 0
This means that you are doing something wrong. Maybe the basic authentication is wrong. You can let OkHttpUtils2 do it for you by removing the authorization header and setting Job.Username / Password.
Hi Erel,

I apologize for the long response on this matter. I was busy this pas few days. Below is my latest code, and I'm not sure how I'm going to use the job.username.
B4X:
Dim j As HttpJob
    
    j.Initialize("j", Me)
    j.Download("https://xxxxx.ph/api/user/csrf")
    j.GetRequest.SetContentType("application/json")
    j.GetRequest.SetHeader("Authorization", "Basic Og==")
    j.GetRequest.SetHeader("apiKey", "xxxxx")
    Wait For (j) JobDone (j As HttpJob)
    If j.Success Then
        Dim jp As JSONParser
        jp.Initialize(j.GetString)
        Log(jp)
        
    Else
        Log("Error: " & j.ErrorMessage)
    End If
    j.Release

Also, below is the code snippet from postman that I am also considering

POSTMAN CODE SNIPPET



I apologize if this concern is taking quite a while already. I really am in need of your solution. Thanks!
 
Upvote 0

The error is still the same. below is the error, by the way.

ERROR

Logger connected to: HUAWEI STK-L22
--------- beginning of system
--------- beginning of main
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
ResponseError. Reason: , Response: {"status":false,"error":"Set API Key in Request Header"}
Error: {"status":false,"error":"Set API Key in Request Header"}
 
Upvote 0
It is impossible to help without the server details. If you like, send me all the information needed to make a request in a private message.
Okay Ere. I will try to ask my colleague regarding this. Thank you very much for your patience and attention on this matter.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
My guess is that the error message is misleading.

This code works in B4J:
B4X:
Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://xxxx/api/user/csrf")
    j.GetRequest.SetHeader("KEY", "xxx")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log(j.GetString)
    End If
    j.Release

It also works in B4A if you change to http instead of https. You need to add this line to the manifest editor:
B4X:
CreateResourceFromFile(Macro, Core.NetworkClearText)

The header is added properly. It seems like the problem is related to the SSL configuration.
 
Upvote 0
Fixed! Thank you so so sooo much!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…