doulin6761 2016-05-10 01:30
浏览 97

Guzzle摘要认证不起作用

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 ?

  • 写回答

2条回答 默认 最新

  • douweiduo7526 2016-05-16 20:48
    关注

    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 :)

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line