POSTgREST API Request is success however no data is returned #3667
-
Server : nginx/1.18.0 (Ubuntu) Description of issueI am trying to fetch data from postgresql database using REST API called say https://api.xxxxx.dev/iot-db using a python code. A JWT token has to be generated from microsoft and pass it as bearer token. Everything is alright however the api returns [ ] . When i checked the response status code it is 200, which means that the request is success, however the api is not returning any data from the postgresql database. The entire code is below. import requests
from msal import ConfidentialClientApplication
from typing import List, Dict, Any, Union
TENANT_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
SCOPE = ['api://xxxxxxxxxxxxxxxxxxxxxxx/.default']
data_endpoint_config = {
'projects': {
'fields': 'projects?select=id,name,route,imeis,subline,ws10Icon,routeSecondLayer',
'order': '&order=name.asc',
},
'latestDeviceData': {
'fields': 'get_latest_device_data',
'order': '&order=timestamp.desc',
},
'data': {
'fields': 'data?select=id,version,imei,timestamp,network,data,project,location,latest',
'order': '&order=timestamp.desc',
},
}
def get_access_token(tenant_id: str, client_id: str, client_secret: str, scope: List[str]) -> Union[str, None]:
authority = f"https://login.microsoftonline.com/{tenant_id}"
app = ConfidentialClientApplication(
client_id,
authority=authority,
client_credential=client_secret,
)
result = app.acquire_token_for_client(scopes=scope)
if 'access_token' in result:
return result['access_token']
else:
print('Failed to retrieve access token:', result.get('error_description', 'Unknown error'))
return None
def call_api_data(access_token: str, query: str) -> Union[Dict[str, Any], List[Dict[str, Any]]]:
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json',
}
url = f"https://api.xxxxx.dev/iot-db/{query}"
print(f"Requesting URL: {url}") # Debug: Print URL
print(f"Request Headers: {headers}") # Debug: Print headers
try:
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an error for bad status codes
# Debug: Print response status code and full response
print(f"Response Status Code: {response.status_code}")
print("Response Content:", response.text)
return response.json()
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
print("Response content:", response.text) # Print full response content
except Exception as err:
print(f"Other error occurred: {err}")
return []
def get_device_data(query_name: str) -> Union[List[Dict[str, Any]], Dict[str, Any]]:
try:
access_token = get_access_token(TENANT_ID, CLIENT_ID, CLIENT_SECRET, SCOPE)
if access_token:
query_config = data_endpoint_config.get(query_name, {})
query_fields = query_config.get('fields', '')
query_order = query_config.get('order', '')
query = f"{query_fields}{query_order}" # Combine fields and order
if query:
return call_api_data(access_token, query)
else:
print(f'No query found for query name: {query_name}')
else:
print('Failed to retrieve access token.')
except Exception as e:
print(f'Error retrieving data: {str(e)}')
return []
if __name__ == '__main__':
query_name = 'data'
device_data = get_device_data(query_name)
print('Device Data:', device_data) Out put of the code : Requesting URL:
Response Status Code: 200 I have decoded the token and this is how the payload of the token looks like
I am stuck with this and i have no idea how to resolve this issue. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
What did you set the |
Beta Was this translation helpful? Give feedback.
-
Yes the role name exists. The role is able to fetch data directly in PostgreSQL |
Beta Was this translation helpful? Give feedback.
Hello, it worked finally. The problem was with the role and access within the PostgresSQL database.
CREATE ROLE "b4022076-068e-4f1e-8c91-56a78be78308"NOLOGIN;
GRANT web_anon TO "b4022076-068e-4f1e-8c91-56a78be78308";
GRANT USAGE ON SCHEMA iot TO "b4022076-068e-4f1e-8c91-56a78be78308";
GRANT SELECT ON ALL TABLES IN SCHEMA iot TO "b4022076-068e-4f1e-8c91-56a78be78308";
i did this queries again restarted the api and it worked. Thanks for the help.