如何使用 cURL 和 PHP 使用 Oauth2

Man*_*and 4 php curl

我无法让以下脚本工作:

我正在使用一个名为 swiftdil 的 api。他们的例子如下:

请求示例:

curl -X POST https://sandbox.swiftdil.com/v1/oauth2/token \
-H 'Content-Type: application/x-www-form-urlencoded' \
-u 'your_username:your_password'
Run Code Online (Sandbox Code Playgroud)

输出示例:

{
"access_token":"your_access_token",
"expires_in": 3600,
"refresh_expires_in": 1800,
"refresh_token": "your_refresh_token",
"token_type": "bearer",
"not-before-policy": 0,
"session_state": "your_session_state"
}
Run Code Online (Sandbox Code Playgroud)

因此,我必须提交凭据的网址是https://sandbox.swiftdil.com/v1/oauth2/token

我尝试过以下代码:

               // Api Credentials
                $url = 'https://sandbox.swiftdil.com/v1/oauth2/token';
                $username = "my_username";
                $password = "my_password";


                // Set up api environment

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: 
                application/x-www-form-urlencoded'));
                curl_setopt($ch, CURLOPT_HEADER, 1);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . 
                $password);


                // Give back curl result
                $output = curl_exec($ch);
                $info = curl_getinfo($ch);
                $curl_error = curl_error($ch);
                curl_close($ch);
                print_r($output);
                print_r($info);
                print_r($curl_error);
?>
Run Code Online (Sandbox Code Playgroud)

该脚本给我返回以下结果:

HTTP/1.1 400 Bad Request Server: nginx/1.13.8 Date: Tue, 15 May 2018 09:17:26 GMT Content-Type: text/html Content-Length: 173 Connection: close
Run Code Online (Sandbox Code Playgroud)

400 错误请求。

我错过了什么吗?我确实满足了上面给出的示例的需求,对吗?我确实打了电话,按要求提供了所有凭据,但仍然无法得到任何回报。

Mo *_*o A 5

我不是 PHP 开发人员,我主要开发 JavaScript。当我与其他 REST 服务集成时,我倾向于使用 Postman ( https://www.getpostman.com/ )。

请尝试以下操作:

  1. 尝试使用 Postman 成功连接 API(应该相对简单)。

  2. 成功后,Postman 能够自动生成 PHP 代码,然后您可以复制和粘贴该代码。与 JavaScript 一起工作就像一个魅力,不明白为什么它与 PHP 会有什么不同。


我刚刚根据您提供的内容在邮递员中填写了详细信息:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://sandbox.swiftdil.com/v1/oauth2/token",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Basic bXlfdXNlcm5hbWU6bXlfcGFzc3dvcmQ=",
    "Content-Type: application/x-www-form-urlencoded"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
Run Code Online (Sandbox Code Playgroud)

请注意,“授权:基本”可以代替“承载”用作基本授权机制(它也应该有效)。因此,将“bXlfdXNlcm5hbWU6bXlfcGFzc3dvcmQ”替换为base64编码的字符串“用户名:密码”(使用实际的用户名和密码)。

  • 投票是因为你也让我的生活变得轻松,因为你指出 Postman 已经生成了这段代码,因为我已经在 Postman 中模拟了我的调用。所以 20 秒后我能够测试正确的结果。 (3认同)