Reputation: 49
Receiving 401 Authentication Access error when trying to request Hosted Payment Field access token via PHP. I have followed this tutorial -> Easy start with BlueSnap hosted payment fields; read about these similar issues -> 1. BlueSnap integration with node js and angular 2. Error getting payment_field_token in Bluesnap API as well as read through the basic auth info http://developers.bluesnap.com/docs/authentication with no luck. Can anyone figure this out?
<?php
$TokenRequest=curl_init();
curl_setopt($TokenRequest, CURLOPT_URL, "https://sandbox.bluesnap.com/services/2/payment-fields-tokens");
curl_setopt($TokenRequest, CURLOPT_HEADER, 1);
curl_setopt($TokenRequest, CURLOPT_HTTPHEADER, array("Authorization: Basic CREDENTIALS_HERE", "Content-type: application/json"));
curl_setopt($TokenRequest, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($TokenRequest, CURLOPT_RETURNTRANSFER, true);
$TokenResponse=curl_exec($TokenRequest);
list($Headers, $Response)=explode("\r\n\r\n", $TokenResponse, 2);
$Headers=explode("\n", $Headers);
foreach($Headers as $Header)
{
if (stripos($Header, "Location")!==false)
{
$Token=trim(str_replace("Location: ", "", $Header));
}
}
?>
Upvotes: 1
Views: 1414
Reputation: 101
In the following URL you will find how to create the credentials to be able to generate the token: https://developers.bluesnap.com/docs/api-credentials
once you have the username and password, substitute the data of the variables: $username, $password:
the following code works for me
$host = 'https://sandbox.bluesnap.com/services/2/payment-fields-tokens';
$username = 'API_1644786117605226907536';
$password = '654321Pm!';
$TokenRequest=curl_init();
curl_setopt($TokenRequest, CURLOPT_URL, $host);
curl_setopt($TokenRequest, CURLOPT_HEADER, 1);
curl_setopt($TokenRequest, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($TokenRequest, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($TokenRequest, CURLOPT_RETURNTRANSFER, true);
$TokenResponse=curl_exec($TokenRequest);
list($Headers, $Response)=explode("\r\n\r\n", $TokenResponse, 2);
$Headers=explode("\n", $Headers);
foreach($Headers as $Header)
{
if (stripos($Header, "Location")!==false)
{
$Token=trim(str_replace("Location: ", "", $Header));
}
}
print_r($Token);
$HOSTEDFIELDTOKENID = str_replace("https://sandbox.bluesnap.com/services/2/payment-fields-tokens/", "", $Token);
echo '<br><br><br><br>';
print_r($HOSTEDFIELDTOKENID);
Upvotes: 0
Reputation: 1
I work for BlueSnap.
A 401 error usually means a permission issue. Did you white list your IP in the BlueSnap console. Docs on how to do that can be found here: https://developers.bluesnap.com/docs/api-credentials . If you can provide me the actual JSON object (or XML) you are sending I can try to trace the exact error and try to determine root cause.
I found your API calls and you are doing a GET instead of a POST. The create a token you need to call a POST.
Upvotes: 0