【问题标题】:Guzzle HTTP client auth failGuzzle HTTP 客户端身份验证失败
【发布时间】:2015-04-27 19:57:06
【问题描述】:

我正在开发一个需要调用 API 以获取 json 响应的项目​​,但 Guzzle 客户端身份验证不起作用。它总是重定向到登录页面。 “nasa”是数据库。 这是我的代码:

<?php
require_once '../composer/vendor/autoload.php';
use GuzzleHttp\Client;
$client = new Client([
    'base_url' => ['http://fedview.bfountain.com/{datasource}/', ['datasource' => 'nasa']]]);
$response = $client->get('service/sbapp/goalByAgency', ['auth' =>  ['username', 'password']]);
echo $response->getBody();
?>

我试过了:

$response = $client->get('login.do?uname=username&passcode=password');

可以登录成功。

谁能帮我解决这个问题? (我正在使用 Guzzle 5)THX!

【问题讨论】:

  • 在 get 方法中提供身份验证凭据时,请尝试不使用“身份验证”密钥。例如:$response = $client-&gt;get('service/sbapp/goalByAgency', ['username', 'password']);
  • 哦。等待。使用 login.do 网址有效吗?
  • 这不是基本身份验证。首先使用login.do?uname=username&amp;passcode=password 获取并发送,然后获取并发送service/sbapp/goalByAgency

标签: api authentication client authorization guzzle


【解决方案1】:

这里是 WHY - 发生的逻辑我将不得不为您留下其余的故障排除。

1) 回声 $response->getBody(); // 打印出登录页面“它总是重定向到登录页面”

2) $response = $client->get('service/sbapp/goalByAgency', ['auth' => ['username', 'password']]); // 你的 $response 是一个登录页面,因为你要求 $client->获取登录页面或验证页面

3) 您需要成功地为 $client 进行身份验证 4) 然后使用 $client 通过 POST 方法请求数据(你错过了这一步)

查看我的其他帖子:How can I use Guzzle to send a POST request in JSON?

重做你的代码。你走在正确的轨道上。

【讨论】:

    猜你喜欢
    • 2019-06-27
    • 2019-01-08
    • 2016-02-22
    • 2015-06-13
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    相关资源
    最近更新 更多