【问题标题】:Api Bundle Symfony set authorization headerApi Bundle Symfony 设置授权标头
【发布时间】:2014-03-01 10:59:48
【问题描述】:

在 Symfony 2 中,我使用这个捆绑库 (https://github.com/LeaseWeb/LswApiCallerBundle) 来发出 API 请求。 这是执行此操作的主要功能:

$output = $this->get('api_caller')->call(new HttpPostJson($url, $parameters));

我想为 oAuth 2 设置一个身份验证标头!

谢谢

【问题讨论】:

  • httppostjson()中的第二个参数是LeaseWeb/LswApiCallerBundle/Call/CurlCall.php的一个对象,它的构造函数是public function __construct($url,$requestObject,$asAssociativeArray=false) { $这个->url = $url; $this->requestObject = $requestObject; $this->associativeArray = $associativeArray; $this->generateRequestData(); }
  • @abbiya 谢谢!但是我应该把 Authorization: $authorization 放在哪里?在我的例子中

标签: symfony oauth authorization


【解决方案1】:

我大约 1 个月前使用了那个库。我想通了只是自定义类 HttpPostJson。

你应该这样做:

in Lsw\ApiCallerBundle\Call

public function makeRequest($curl, $options, $authorization)
{
    $curl->setopt(CURLOPT_URL, $this->url);
    $curl->setopt(CURLOPT_POST, 1);
    $curl->setopt(CURLOPT_POSTFIELDS, $this->requestData);
    $curl->setopt(CURLOPT_HTTPHEADER, array(
        'Authorization: ' . $authorization
    ));
    $curl->setoptArray($options);
    $this->responseData = $curl->exec();
}

我刚刚加了

    $curl->setopt(CURLOPT_HTTPHEADER, array(
        'Authorization: ' . $authorization
    ));

在每个需要授权的 API 调用中。

【讨论】:

    【解决方案2】:

    您不必编写代码,因为库已经准备好接收任何 curl 选项。

    像关联数组一样传递 httpheaders 作为下一个示例:

    //it's a fake test
    $url = 'http://www.example.com';
    $data = array();
    $returnAssociativeArray = true;
    
    //add curl options
    $options = array(
        'userpwd' => 'demo:privateKey'
        'httpheader' => array('Content-type' => 'application/json')
    );
    
    
    $json = $this->container->get('api_caller')->call(
                new HttpPostJson(
                    $url,
                    $data,
                    $returnAssociativeArray,
                    $options
                )
            );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-14
      • 2021-11-21
      • 2015-12-02
      • 2016-09-10
      • 2021-04-07
      • 2016-07-13
      • 1970-01-01
      • 2015-11-05
      相关资源
      最近更新 更多