# Error 401 Unauthorized

**URL:** <https://community.dreamfactory.com/t/error-401-unauthorized/1942>\
**Category:** Authentication & Security\
**Created:** [December 20, 2015, 10:22am UTC](https://community.dreamfactory.com/t/error-401-unauthorized/1942 "2015-12-20T10:22:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tusharsjain](https://avatars.discourse-cdn.com/v4/letter/t/e480ec/32.png) [@tusharsjain](https://community.dreamfactory.com/u/tusharsjain)\
**Post date:** [December 20, 2015, 10:22am UTC](https://community.dreamfactory.com/t/error-401-unauthorized/1942/1 "2015-12-20T10:22:15Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![charliefinale](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@charliefinale](https://community.dreamfactory.com/u/charliefinale)\
**Post date:** [December 20, 2015, 4:57pm UTC](https://community.dreamfactory.com/t/error-401-unauthorized/1942/2 "2015-12-20T16:57:16Z")

</div>

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); } ?\>

---

<div class="post-metadata">

**Author:** ![tusharsjain](https://avatars.discourse-cdn.com/v4/letter/t/e480ec/32.png) [@tusharsjain](https://community.dreamfactory.com/u/tusharsjain)\
**Post date:** [December 28, 2015, 3:55am UTC](https://community.dreamfactory.com/t/error-401-unauthorized/1942/3 "2015-12-28T03:55:32Z")

</div>

Hi Charlie

Thanks a ton, it really helped…
