B4J Question Problems with HttpJob in API-REST GET

Nator

Member
Licensed User
Longtime User
Hello Community, it is possible that you can help me determine what could be the problem when consulting a REST API Get the code is the following

Get APi:
ApiRquest.Initialize("estaciones", Me)
    
    ApiRquest.Download("https://api.captahydro.com/devices")
    
    ApiRquest.GetRequest.SetHeader("APIKey", "4c823e19-e47a-yyyy-yyyy-xxxxxxxxxxxx")
    ApiRquest.GetRequest.SetHeader("OrganizationId", "29")
    ApiRquest.GetRequest.SetHeader("Content-Type","application/json")

the error returned is the following :

ResponseError. Reason: , Response: {"Message":"User is not authorized to access this resource with an explicit deny"}


when making the query in postman I have no problems and the records are returned without problems

postman.png


I also tried download2 and it doesn't work either, I would really appreciate your valuable help
 

OliverA

Expert
Licensed User
Longtime User
The first thing I would try is to not set the content type.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
please post the api documentation (or a link to it)
 
Upvote 0

Nator

Member
Licensed User
Longtime User
please post the api documentation (or a link to it)
It is in Spanish, but basically it says this:

api manual:
API connection
The API is hosted at the following link:
APIURL = https://api.captahydro.com
To connect, in EACH request you must send the credentials that are the identifier of the
organization plus the API Key. Both must be sent within the header of the query, in the following
Format:
APIKey:{API_KEY_GENERATED}
OrganizationId:{ORGANIZATION_ID}
API Features
In the provided link of the API, the following queries can be made:
devices: Request type GET
device{deviceId}: Request type GET
device{deivceId}/data: Request type GET
/devices
GET type request:
Gets the organization's devices and those that were shared with the organization.
Response format: If the response is wrong, it returns an error. In case of being
successful, it returns in JSON format, an array with the data of each device. This contains
always:
id : device identifier
name: name of the device (in amaru) And depending on the type of device, it returns the
last value measured by your sensors plus the time of the sensor measurement (in the format of
unix timestamp in milliseconds).
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Is it possible to see the full Postman api call which is successful.

can you click on the code button to the right of the main window.

1681243212860.png
 
Upvote 0

Nator

Member
Licensed User
Longtime User
Hello Community, it is possible that you can help me determine what could be the problem when consulting a REST API Get the code is the following

Get APi:
ApiRquest.Initialize("estaciones", Me)
   
    ApiRquest.Download("https://api.captahydro.com/devices")
   
    ApiRquest.GetRequest.SetHeader("APIKey", "4c823e19-e47a-yyyy-yyyy-xxxxxxxxxxxx")
    ApiRquest.GetRequest.SetHeader("OrganizationId", "29")
    ApiRquest.GetRequest.SetHeader("Content-Type","application/json")

the error returned is the following :

ResponseError. Reason: , Response: {"Message":"User is not authorized to access this resource with an explicit deny"}


when making the query in postman I have no problems and the records are returned without problems

View attachment 141131

I also tried download2 and it doesn't work either, I would really appreciate your valuable help
I think I figured out that I should send it as an array array(
'APIKey: 4c823e19-e47a-493f-8837-ade4efea9dfc',
'OrganizationId: 29'), but I don't know how to add this array to the head?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
can confirm that it does work in postman. gotta eat now.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
You can try the request against https://httpbin.org/anything from both postman and B4J and see if you can spot any differences. Just don't use your real api key. Also make sure you are using the correct Api key. If you are copying/pasting the key, make sure you're not accidentally including "hidden" characters
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
his key is valid. the error is a 403 which means authentication is correct, but there is
an authorization issue. he can get in the front door, but not to a higher story. the error
reported is typical of amazon aws, and you can see references to amazon aws in
postman's response. i wish he would post the full documentation. i'm thinking there is
something missing in what he's told us.
i also tried using an okhttp3 client directly (essentially what okhttputils2 does) in the
very unlikely event okhttputils2 does something the server doesn't like, but the error was
the same.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
it can be done with a browser and ajax. i get the same answer as with postman. chrome doesn't allow cors error, so i had to use (god forbid) ie. browser tacks on additional headers. maybe these are what server wants. i'm going to apply them to okhttputils2 request to see if that makes a difference. heading for breakfast now.
 

Attachments

  • capture.png
    capture.png
    12.1 KB · Views: 63
Upvote 0

Nator

Member
Licensed User
Longtime User
You can try the request against https://httpbin.org/anything from both postman and B4J and see if you can spot any differences. Just don't use your real api key. Also make sure you are using the correct Api key. If you are copying/pasting the key, make sure you're not accidentally including "hidden" characters
Thank you very much for the information, I did the tests with postman and b4j and both are almost identical, the only difference is the Postman-Token



code head:
{
  "args": {},
  "data": "",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate, br",
    "Apikey": "4c823e19-e47a-493f-3455-zxzxzxzzxzx",
    "Host": "httpbin.org",
    "Organizationid": "29",
    "User-Agent": "okhttp/4.9.0",
    "X-Amzn-Trace-Id": "Root=1-6436d8a7-7ea2af8736ce84475c3b6380"
  },
  "json": null,
  "method": "GET",
  "origin": "190.45.149.154",
  "url": "https://httpbin.org/anything"
}

{
    "args": {},
    "data": "",
    "files": {},
    "form": {},
    "headers": {
        "Accept": "*/*",
        "Accept-Encoding": "gzip, deflate, br",
        "Apikey": "4c823e19-e47a-493f-8837-zxzxzxzzxzx",
        "Host": "httpbin.org",
        "Organizationid": "29",
        "Postman-Token": "c6607704-68d0-4cb8-94fd-37ade38feaad",
        "User-Agent": "PostmanRuntime/7.32.2",
        "X-Amzn-Trace-Id": "Root=1-6436d903-7e265df92db563c504c90d03"
    },
    "json": null,
    "method": "GET",
    "origin": "190.45.149.154",
    "url": "https://httpbin.org/anything"
}
 
Upvote 0

Nator

Member
Licensed User
Longtime User
it can be done with a browser and ajax. i get the same answer as with postman. chrome doesn't allow cors error, so i had to use (god forbid) ie. browser tacks on additional headers. maybe these are what server wants. i'm going to apply them to okhttputils2 request to see if that makes a difference. heading for breakfast now.
Thank you very much, I tried with https://httpbin.org/anything and you can see the results below the head are almost the same with the exception of Postman-Token
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
4c823e19-e47a-493f-3455-zxzxzxzzxzx
4c823e19-e47a-493f-8837-zxzxzxzzxzx
Unless you, for some reason, are changing the 3455 from the B4J app to 8837 in Postman, the displayed portion of the API keys do not match, therefore you may not be using the same key for B4J as for Postman. Please make sure you're actually using the same API keys (right now it looks like you are not). Alternative, you could copy the B4J key (if they differ) into Postman and check the result(s)
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
yeah, 3455 is wrong. i can run a shell in b4j and get the output using 8837
 

Attachments

  • capture.png
    capture.png
    60.6 KB · Views: 65
Upvote 0
Top