yii2 rest api basic auth


(Chamindar2002) #1

Hi All,

I am developing an api using yii2 REST Api basicauth. I have successfully developed in my local xampp server. I have deployed the code to a vps running centos. But i am getting the following unathorized response:

{

"name": "Unauthorized",

"message": "Your request was made with invalid credentials.",

"code": 0,

"status": 401,

"type": "yii\\web\\UnauthorizedHttpException"

}

I have set the headers properly.

response headers:

Connection →Keep-Alive

Content-Type →application/json; charset=UTF-8

Date →Fri, 05 May 2017 08:40:49 GMT

Keep-Alive →timeout=5, max=100

Server →Apache

Transfer-Encoding →chunked

Www-Authenticate →Basic realm="api"

X-Powered-By →PHP/5.6.30

when i dump the headers i dont see the Authorization Header:

object(yii\web\HeaderCollection)#81 (1) {

["_headers":"yii\web\HeaderCollection":private]=>

array(10) {

["host"]=>


array(1) {


  [0]=>


  string(22) "avenierrpharma-api.dev"


}


["connection"]=>


array(1) {


  [0]=>


  string(10) "keep-alive"


}


["user-agent"]=>


array(1) {


  [0]=>


  string(113) "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36"


}


["cache-control"]=>


array(1) {


  [0]=>


  string(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' /> &quot;no-cache&quot;


}


[&quot;content-type&quot;]=&gt;


array(1) {


  [0]=&gt;


  string(33) &quot;application/x-www-form-urlencoded&quot;


}


[&quot;authorization&quot;]=&gt;


array(1) {


  [0]=&gt;


  string(30) &quot;Basic O2zbBtP1yVNpXx2xZhJbNxuR&quot;


}


[&quot;postman-token&quot;]=&gt;


array(1) {


  [0]=&gt;


  string(36) &quot;e6918220-a026-e143-c7ae-bc09e0927c54&quot;


}


[&quot;accept&quot;]=&gt;


array(1) {


  [0]=&gt;


  string(3) &quot;*/*&quot;


}


[&quot;accept-encoding&quot;]=&gt;


array(1) {


  [0]=&gt;


  string(19) &quot;gzip, deflate, sdch&quot;


}


[&quot;accept-language&quot;]=&gt;


array(1) {


  [0]=&gt;


  string(14) &quot;en-US,en;q=0.8&quot;


}

}

}

any idea why this is happening in the remote server ? Please help.


(Chamindar2002) #2

Problem solved…

For any one coming across this issue i have found the solution.

I added the following line in my .htaccess file and problem was fixed.

SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0

i found the solution from the thread below: https://github.com/yiisoft/yii2/issues/6631


(Niopremink) #3

Hi, I know this topic is old, but I have exactly the same problem and none of the solutions here helped me.

I’m using PHP cURL. Any ideas?

Regards