19

我正在尝试与 infojobs-api 建立联系,文档说明了如何以这种方式进行连接:

GET /api/1/application HTTP/1.1
主机:api.infojobs.net 授权:基本 QWxhZGRpbjpvcGVuIHNlc2FtZQ==,Bearer 07d18fac-77ea-461f-9bfe-a5e9d98deb3d

https://developer.infojobs.net/documentation/user-oauth2/index.xhtml

这是我的代码:

$basicauth = new Client(['base_uri' => 'https://api.infojobs.net']);

$credentials = base64_encode(CLIENT_ID .':' . CLIENT_SECRET ) ;

$newresponse = $basicauth->request(
  'GET',
  'api/1/curriculum',
  ['debug' => true], 
  ['auth' => 
    ['Basic', $credentials] ,
    ['Bearer', $acceso->access_token]
  ]
)->getBody()->getContents();

d($newresponse);

API/Guzlle 给了我这个错误:

致命错误:未捕获的 GuzzleHttp\Exception\ClientException:客户端错误:GET https://api.infojobs.net/api/1/curriculum导致401 No Autorizado响应:{"error":"102","error_description":"Client credentials not valid","timestamp":"2016-06-25T14:08 /app/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:107 中的:54.774Z"}

所以我做错了什么,但我没有找到问题所在。

任何想法,谢谢。

奥斯卡

4

2 回答 2

45

当我看到您的请求的 HTTP 标头时:

Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==,Bearer 07d18fac-77ea-461f-9bfe-a5e9d98deb3d

您有一个Authorization包含逗号分隔值的标题。他们彼此并不分离。所以你不能auth像你所做的那样从 Guzzle 的密钥中受益。

您应该做的是手动设置授权标头:

$newresponse = $basicauth->request(
    'GET',
    'api/1/curriculum',
    ['debug'   => true], 
    ['headers' => 
        [
            'Authorization' => "Basic {$credentials},Bearer {$acceso->access_token}"
        ]
    ]
)->getBody()->getContents();
于 2016-07-14T09:51:28.513 回答
1

对于一个帖子电话,这对我有用:

$guzzle = new Client(['base_uri' => self::APIURL]);

$raw_response = $guzzle->post($endpoint, [
  'headers' => [ 'Authorization' => 'Bearer ' . $publicKey ],
  'body' => json_encode($data),
]);

$response = $raw_response->getBody()->getContents();
于 2022-01-26T13:39:53.547 回答