Error 401 Unauthorized

Hi All,

I am new to dreamfactory, i have generated a very simple API which connects to an AWS Sql Server and fetches data.

the API is working fine from the APIDocs, but when i try to access the same via URL in a browser i get an error.

Error: 401
Message: Unauthorized

Request URL: https://df-dftest1.enterprise.dreamfactory.com/api/v2/sql/_table/Members?api_key=6498

Please its very urgent for me to find a solution, Thanks in advance.

Best,

Tushar

Hi Tushar

I faced this error several times. Firstly simply putting the URL in a browser is not the solution, because there is certain data that needs to be sent in the headers, including authorization. You are best set to understand the requirements if you use something like the REST console which is an extension to google chrome. You need to send in the headers your API key, your API name.

Below I include 2 functions that I use, one to get a token, and once I have that, the other to access my database. It is written in PHP - but if you use something else, you can probably work out from this what needs to be in the headers and body, which as I say can be tested in chrome`s REST console without and PHP at all.

Note you need to replace yourAPPDB, urls and IP addresses and API key with your own.

<?php function sessiontoken ($email,$password, $duration) { $headers = array( 'Content-type: application/json', 'Accept: application/json', 'DreamFactory-Application-Name: YourAPPDB' ); $jsoncreds = '{ "email": "user@yourapp.com", "password": "user123", "duration": 720 }' ; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL,"http://xxx.xxx.xxx.xxx:80/api/v2/user/session"); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $jsoncreds); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $result = curl_exec ($curl); echo curl_error($curl); curl_close ($curl); echo "
"; $token = json_decode($result,true); $sessiontoken = $token[session_token] ; return $sessiontoken; } function gettable($table,$sessiontoken) { $headr = array(); $headr[] = 'Content-type: application/json'; $headr[] = 'Accept: application/json'; $headr[] = 'DreamFactory-Application-Name: YourAPPDB'; $headr[] = 'X-DreamFactory-Session-Token :'. $sessiontoken; $headr[] = 'X-DreamFactory-Api-Key : d18fd67xxxxxxxxa3a35b0462adeb3567f3abf1dxxxx'4; $url = "http://yourapp.com:80/api/v2/yourAPPDB/_table/" . $table; $curl2 = curl_init(); curl_setopt($curl2, CURLOPT_URL,$url); curl_setopt($curl2, CURLOPT_GET, 1); curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl2, CURLOPT_HTTPHEADER, $headr); $cats = curl_exec ($curl2); echo curl_error($curl2); curl_close ($curl2); echo "
"; $arrayoutput = json_decode($cats,true); return ($arrayoutput); } ?>

Hi Charlie

Thanks a ton, it really helped…