Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Oauth2 using cURL and PHP

Tags:

php

curl

I can't get the folling script to work:

I'm using an api called swiftdil. Their example is as follows:

Example request:

curl -X POST https://sandbox.swiftdil.com/v1/oauth2/token \
-H 'Content-Type: application/x-www-form-urlencoded' \
-u 'your_username:your_password'

Example output:

{
"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"
}

So the url I've to submit my credentials to is https://sandbox.swiftdil.com/v1/oauth2/token

I've tried the following code:

               // 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);
?>

The script is giving me back the following result:

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

400 Bad Request.

Am I missing something? I do fullfill the needs of the example given above right? I do make a postcall, give all the credenatials as asked, but still can't get anything back.

like image 869
Mand Avatar asked Oct 21 '25 12:10

Mand


1 Answers

I am not a PHP developer, I mostly do JavaScript. When I integrate with other REST services I tend to use Postman (https://www.getpostman.com/).

Try the following:

  1. Attempt to successfully connect with the API using Postman (should be relatively straightforward).

  2. When successful, Postman has the ability to generate PHP code automatically, which you can then copy and paste. Works like a charm with JavaScript, don't see why it will be any different with PHP.


I just filled in the details in postman based on what you provided:

<?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;
}

Please note, 'Authorization: Basic' can be used as basic authorization mechanism instead of 'Bearer' (it should work too). So replace 'bXlfdXNlcm5hbWU6bXlfcGFzc3dvcmQ' with the base64 encoded string 'username:password' (use actual username and password).

like image 148
Mo A Avatar answered Oct 23 '25 02:10

Mo A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!