Guzzle digest auth not working

为君一笑 提交于 2019-12-12 03:25:21

问题


I'm trying to make an API that use digest as authentication, When I access API via curl command line using this command, it's work

curl --digest --user website:website http://localhost/api/test/users

but when running api client using Guzzle 6 php library using this code

$handler = new GuzzleHttp\Handler\CurlHandler();
    $stack = GuzzleHttp\HandlerStack::create($handler); // Wrap w/ middleware

    $client = new GuzzleHttp\Client(['base_uri' => 'http://localhost', 'handler' => $stack]);
    try {
        $request = new GuzzleHttp\Psr7\Request('GET', $req_uri, [
            'auth' => ['website', 'website', 'digest']
        ]);
        $response = $client->send($request, ['timeout' => 2]);
    } catch (Exception $e) {
        echo $e->getMessage();
        die();
    }

    return $response;

It's not working, it says

401 Unauthorized` response: {"status":false,"error":"Unauthorized"}

how to fix this? and how to implement custom handler guzzle php in the right way ?


回答1:


I had the same problem, figured out that it may depend on the version of your curl php extension. I didn't have the possibility to upgrade it, so I had to handle digest auth myself (basically the info in wikipedia is enough https://en.wikipedia.org/wiki/Digest_access_authentication).

You send the first request and get a response with code 401 and "WWW-Authenticate" header (it contains some values you'll need to generate your hashes), then just generate "Authorization" header and use it in your guzzle requests.

Sorry I can't provide a code example at the moment, maybe later, if you don't make it yourself :)




回答2:


I had the same issue and I noticed php-curl extension wasn't installed at all. After installing it digest auth worked. To install the extension type:

sudo apt-get install php-curl



来源:https://stackoverflow.com/questions/37127894/guzzle-digest-auth-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!