0

Please how can I pass a secret key as an authorization header in API call?

See the script below:

 <?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
$postData = $_POST;
$publicKey = $postData['publicKey'];
$bvn_number = $postData['bvn'];

//initializing
$ch = curl_init();

//used to send the request to the Api endpoint
curl_setopt($ch, CURLOPT_URL, "https://api.paystack.co/bank/resolve_bvn".$bvn_number."?seckey=".$secretKey);

//return instead of outputing directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//whether to include the header in the output set to false here

curl_setopt($ch, CURLOPT_HEADER, 0);

//execute the response

$output = curl_exec($ch);

//check for errors

if($output === FALSE){
echo "Invalid bvn number:" . curl_error($ch);
}

//close and free up the handel

curl_close($ch);

//display the output

print_r($output);

?>

Error message:

Error: { "status": false, "message": "No Authorization Header was found" }

jrswgtr
  • 2,287
  • 8
  • 23
  • 49
Teezyjay
  • 9
  • 1
  • 1
  • Possible duplicate of [PHP cURL custom headers](https://stackoverflow.com/questions/8115683/php-curl-custom-headers) – Horuth Aug 14 '19 at 14:43
  • `$url = 'https://api.paystack.co/bank/resolve?account_number=0022728151&bank_code=063'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer sk_live_a....')); $result = curl_exec($ch); curl_close($ch); echo($result);` Use this. Put secret key hardcoded in the Authorization Bearer – Wicfasho Apr 28 '20 at 10:39

2 Answers2

2

If it requires authorization bearer you have to add the word Bearer to the authentication header before the token or secret.

Here an example.

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer'. $token));    

example from reference:

GET /resource HTTP/1.1
Host: server.example.com
Authorization: Bearer mF_9.B5f-4.1JqM

Reference

Vidal
  • 2,605
  • 2
  • 16
  • 32
  • Still not working, I replace the line code with what you sent but still getting error message: { "status": false, "message": "Format is Authorization Bearer [secret key]" } – Teezyjay Aug 14 '19 at 16:15
0

You are puting the secret key in the query string.

change the line below:

curl_setopt($ch, CURLOPT_HEADER, 0);

to the code:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: ' . $secretKey));
Marcel Bezerra
  • 161
  • 1
  • 9
  • i tried it but see error message below: { "status": false, "message": "Format is Authorization Bearer [secret key]" } – Teezyjay Aug 14 '19 at 15:20
  • so write: ``` curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $secretKey)); ``` – Marcel Bezerra Aug 14 '19 at 16:58
  • I use: curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer $secretKey"]); and its now working perfectly, thank you – Teezyjay Aug 14 '19 at 20:41