PHP 使用基本身份验证和不记名令牌
PHP Guzzle with basic auth and bearer token
我正在尝试与 infojobs-api 建立联系,文档解释了如何以这种方式建立联系:
GET /api/1/application HTTP/1.1
Host: api.infojobs.net
Authorization: Basic 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 给我返回这个错误:
Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: GET https://api.infojobs.net/api/1/curriculum
resulted in a 401 No Autorizado
response:
{"error":"102","error_description":"Client credentials not valid","timestamp":"2016-06-25T14:08:54.774Z"}
in /app/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:107
所以我做错了什么,但我没有发现错在哪里。
任何想法,谢谢。
奥斯卡
我看到你的请求的 HTTP headers:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==,Bearer 07d18fac-77ea-461f-9bfe-a5e9d98deb3d
您有一个 Authorization
header,其中包含一个逗号分隔值。他们彼此并不分开。所以你不能像你所做的那样从Guzzle的auth
键中受益。
您应该做的是手动设置授权 header:
$newresponse = $basicauth->request(
'GET',
'api/1/curriculum',
['debug' => true],
['headers' =>
[
'Authorization' => "Basic {$credentials},Bearer {$acceso->access_token}"
]
]
)->getBody()->getContents();
对于 post 调用这对我有用:
$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();
我正在尝试与 infojobs-api 建立联系,文档解释了如何以这种方式建立联系:
GET /api/1/application HTTP/1.1
Host: api.infojobs.net Authorization: Basic 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 给我返回这个错误:
Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error:
GET https://api.infojobs.net/api/1/curriculum
resulted in a401 No Autorizado
response: {"error":"102","error_description":"Client credentials not valid","timestamp":"2016-06-25T14:08:54.774Z"} in /app/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:107
所以我做错了什么,但我没有发现错在哪里。
任何想法,谢谢。
奥斯卡
我看到你的请求的 HTTP headers:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==,Bearer 07d18fac-77ea-461f-9bfe-a5e9d98deb3d
您有一个 Authorization
header,其中包含一个逗号分隔值。他们彼此并不分开。所以你不能像你所做的那样从Guzzle的auth
键中受益。
您应该做的是手动设置授权 header:
$newresponse = $basicauth->request(
'GET',
'api/1/curriculum',
['debug' => true],
['headers' =>
[
'Authorization' => "Basic {$credentials},Bearer {$acceso->access_token}"
]
]
)->getBody()->getContents();
对于 post 调用这对我有用:
$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();