我已按照 Path API 进行操作关于如何验证用户。在教程身份验证过程中,用户开始重定向到以下 URL 并提示授予访问权限:
https://partner.path.com/oauth2/authenticate?response_type=code&client_id=THE_CLIENT_ID
之后,服务器将通过 URL 地址以授权代码形式给出响应(我已完成此步骤并获得代码)。
根据文档的解释,代码应该使用 /oauth2/access_token 与客户端 ID 和客户端 key (获取 access_token)交换为访问 token
但是我不知道如何通过 cURL 将数据 POST 到服务器,我已经尝试了很多 curl_setopt() 选项和组合,但它仍然没有给我任何结果。
从文档中,请求如下所示:
POST /oauth2/access_token HTTP/1.1
Host: partner.path.com
Content-Type: application/x-www-form-urlencoded
Content-Length: <LENGTH>
grant_type=authorization_code&client_id=CLIENT&client_secret=SECRET&code=CODE
cURL 格式如下:
curl -X POST \
-F 'grant_type=authorization_code' \
-F 'client_id=CLIENT_ID' \
-F 'client_secret=CLIENT_SECRET' \
-F 'code=CODE' \
https://partner.path.com/oauth2/access_token
服务器将给出如下响应:
HTTP/1.1 201 CREATED
Content-Type: application/json
Content-Length: <LENGTH>
{
"code": 201,
"type": "CREATED"
"reason": "Created",
"access_token": <ACCESS_TOKEN>,
"user_id": <USER_ID>,
}
最佳答案
要使用 cURL 在 PHP 中执行 POST 请求,您可以执行以下操作:
$handle = curl_init('https://partner.path.com/oauth2/access_token');
$data = array('grant_type' => 'authorization_code', 'client_id' => 'CLIENT', 'client_secret' => 'SECRET', 'code' => 'CODE');
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$resp = curl_exec($handle);
然后,您可以使用 json_decode($json_encoded)
从服务器响应中获取关联数组。
关于php - 使用 PHP cURL 在/oauth2/access_token 上 POST 数据并以 json 形式获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31416209/