4

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.

2 Answers 2

5

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Upvoted because you made my life easy as well for pointing out that Postman already produces this code as i already had mocked up my calls in Postman.. So 20 seconds later i was able to test with correct results.
2

You also need to set the curl post fields by setting the below option as per your data.

"curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'code' => $code,
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => $redirect_uri,
'grant_type' => 'authorization_code'
))";

If still not work, you can find the curl error as :

if(curl_error($ch))
{
    echo 'error:' . curl_error($ch);
}

1 Comment

Thanks for your reply. I just found this on the API documentation: All API requests must be made over HTTPS. Any requests made over HTTP will fail. REQUEST HEADER Authorization required To request an access token, send your client_id and secret values as the HTTP basic authentication credentials. If you use cURL, specify -u “client_id:secret”. When you call APIs, send the value as the OAuth 2.0 access token with the authentication type set as Bearer. For example: Authorization: Bearer . How would I implement this into the code i'm using right now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.