oauth-2.0 - 对 Feedly API 的授权请求会引发 Guzzle 错误请求?

标签 oauth-2.0 authorization guzzle feedly

目标是用授权代码交换访问和刷新 token 。

错误:

GuzzleHttp\Exception\ClientException #400

Client error response 
[url] http://sandbox.feedly.com/v3/auth/token?code=[auth_code]&client_id=sandbox&client_secret=[secret]&redirect_uri=https%253A%252F%252F[site url]&grant_type=authorization_code&state=%23 
[status code] 400 
[reason phrase] Bad Request

相关代码:

$client = new GuzzleHttp\Client();
$parameters = ['code'=>$_GET['code'],'client_id'=>'sandbox','client_secret'=> '[secret]','redirect_uri'=>urlencode('https://[site url]'),'grant_type'=>'authorization_code', 'state'=>'#'];
$params = http_build_query($parameters);
$request = $client->createRequest('POST', 'http://sandbox.feedly.com/v3/auth/token?'.$params);
$request->addHeader('Accept-Encoding','GZIP');
$request->setHeader('Authorization', "auth-code");
$request->addHeader('Content-Type','application/json');
$response = $client->send($request);
var_dump($response->json());

还尝试使用 state = "state.passed.in" 但抛出相同的错误。

您能指出代码片段中的错误吗?它使用 Feedly API v3 沙箱和 Guzzle HTTP 客户端。

如果遵循请求 URL,则会抛出“不允许获取”。

更新的代码片段:

$client = new GuzzleHttp\Client();
    $parameters = ['code'=>$_GET['code'],'client_id'=>'sandbox','client_secret'=> '[secret]','redirect_uri'=>urlencode('https://[site url]'),'grant_type'=>'authorization_code', 'state'=>'#'];
    $params = http_build_query($parameters);
    $request = $client->createRequest('POST', 'http://sandbox.feedly.com/v3/auth/token?'.$params);
    $response = $client->send($request);
    var_dump($response->json());

更新代码时出错:

GuzzleHttp\Exception\ServerException #522

Server error response [url] http://sandbox.feedly.com/v3/auth/token?code=[auth_code]&client_id=sandbox&client_secret=[secret]&redirect_uri=https%253A%252F%252F[site url]&grant_type=authorization_code&state=%23 
[status code] 522 
[reason phrase] Origin Connection Time-out  

注意:更新代码抛出相同的错误(几个小时后)

GuzzleHttp\Exception\ClientException #400

Client error response 
[url] http://sandbox.feedly.com/v3/auth/token?code=[auth_code]&client_id=sandbox&client_secret=[secret]&redirect_uri=https%253A%252F%252F[site url]&grant_type=authorization_code&state=%23 
[status code] 400 
[reason phrase] Bad Request

最佳答案

根据 OAuth 2.0 规范,从底部开始:

The client MUST use the HTTP "POST" method when making access token requests.

(来源:第 3.2 节。The OAuth 2.0 Authorization Framework )

这解释了为什么在浏览器中导航到请求 URL 会失败(导航问题为 GET 并且仅支持 POST 请求)。

下一点是客户端身份验证,更具体地说,是如何提供 client_idclient_secret 参数,以便服务器可以验证您是受信任的客户端应用程序。同样,根据规范,应通过两种方式传递此信息:

  1. 通过 [RFC2617] 中定义的 HTTP 基本身份验证方案,其中客户端标识符作为用户名传递,客户端 key 作为密码传递。此方法必须得到 OAuth 兼容服务器的支持,并且也是推荐的执行方式。*
  2. 通过在请求正文中包含客户端凭据,通常编码为 application/x-www-form-urlencoded(请参阅下面的示例)。此方法是可选的,在某些 OAuth 服务器中可能不可用。

在请求正文中传递客户端凭据的示例:

POST https://YOUR_NAMESPACE/oauth/token
 Content-type: application/x-www-form-urlencoded

client_id=YOUR_CLIENT_ID
&redirect_uri=http://YOUR_APP/callback
&client_secret=YOUR_CLIENT_SECRET
&code=AUTHORIZATION_CODE
&grant_type=authorization_code

(来源: OAuth Web Application Protocol 的第四步,单击第二步中的纯链接以查看完整授权代码授予流程中的所有原始 HTTP 请求)

现在,对于 Feedly 用例,我在文档中找不到任何有关支持 HTTP 基本身份验证的内容。他们确实对将代码交换为访问 token 所需的参数进行了以下说明:

Note: these parameters can either be passed in the URL, as form values, or in a JSON document. If you use a JSON document, make sure you pass the “Content-Type: application/json” header in the request.

(来源:Exchanging an auth code for a refresh token and an access token)

令人惊讶的一件事是,它们似乎允许在 URL 本身中传递客户端凭据,这是 OAuth 规范明确禁止的:

The parameters (client_id and client_secret) can only be transmitted in the request-body and MUST NOT be included in the request URI.

(来源:第 2.3.1 节。The OAuth 2.0 Authorization Framework )

总之,根据他们的文档,您所做的事情(在 URL 本身中传递参数)应该是可能的,除非文档不是最新的并且他们已经修复了不符合规范的问题并不再支持这一点。

此外,您所做的一些事情似乎是错误的。 code 参数永远不会在 Authorization header 中传递,因此除非您想尝试使用基本身份验证在该 header 中传递客户端凭据,否则我建议您删除此参数 header 。

我还会删除 Accept-Encoding header ,因为他们的文档没有提到除了返回 JSON 响应之外支持任何内容。如果您想保留该 header ,请将值从 gzip 更改为 application/json

最后,您也不会在请求正文中发送任何数据,因此您可能还需要删除 Content-Type header ,因为 Feedly 可能认为如果存在此 header ,则数据位于请求而不是 URL 上。

关于oauth-2.0 - 对 Feedly API 的授权请求会引发 Guzzle 错误请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39814400/

相关文章:

php - 如何同时执行多个 Guzzle 请求?

php - 在 Guzzle 中解决多个异步 Promise 时,我只能从第一个 Promise 获得结果

laravel - OAuth2 密码授予类型和 client_credential 授予类型是否相同?

Spring 安全 OAuth2 : Purge TokenStore

authorization - Dynamics CRM 365 - 无效的用户授权传递给平台的用户身份验证无效

web-config - 源文档中没有与/configuration/system.web/authorization/匹配的元素

oauth - 小工具 OAuth2 授权

authentication - linkedin oauth2登录返回 "504 Gateway Time-out"

c# - 使用 HttpWebRequest 从 C# 连接到 Tagpacker.com

php - 如何在 Guzzle 5 中发送 PUT 请求的参数?