API authorisation error when trying to update a contact but ok when adding/deleting

Can someone help me please as I think I’m going insane :slight_smile:

My API is quite happily adding and deleting contacts and tags … but when I try and update a contact record I am met with the error:

{“fault”:{“faultstring”:“Invalid Access Token”,“detail”:{“errorcode”:“keymanagement.service.invalid_access_token”}}}

My code is as follows:

$keapID=get_keapID_from_email($emailAddress); //this in an internal function and works
echo(“
KEAP :: “.$keapID);
echo(”
https://api.infusionsoft.com/crm/rest/v1/contacts/$keapID”); //used so I can see the URL endpoint that is being called
echo(“
”);

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => “https://api.infusionsoft.com/crm/rest/v1/contacts/”.$keapID,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => “”,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => “PATCH”,
CURLOPT_POSTFIELDS => $contactData,
CURLOPT_HTTPHEADER => array(
“Authorization: Bearer $accesstoken”,
“Content-Type: application/json”
),
));

The value $accesstoken is created by calling a function that refreshes the access token by supplying the api Accesskey and secret (when the code runs, this does work and is the same process for adding/deleting records and tags)

I am at a loss - everything works except for updating a contact record, when I get this error:

{“fault”:{“faultstring”:“Invalid Access Token”,“detail”:{“errorcode”:“keymanagement.service.invalid_access_token”}}}

I have also tried putting the baase64_encoded api string in the header too, like this:

CURLOPT_HTTPHEADER => array(
“Authorization: Basic $apikey”,
“Authorization: Bearer $accesstoken”,
“Content-Type: application/json”
),

with - and without - the Bearer token … when I do this, I get this error message:

{“fault”:{“faultstring”:“Invalid access token”,“detail”:{“errorcode”:“oauth.v2.InvalidAccessToken”}}}

Looking at the documentation and other examples, then I shouldn’t be passing the api key, but I thought I’d try

If someone can help please as this is driving me insane and I’ve got too many grey hairs already :smiley: :smiley:

OK, ignore me … turns out that between me getting the access token and storing that as a variable, my code made another call to refresh the token which meant the stored variable was out of date

sorry!!!