Jump to content
  • 0

PHP using Basic Authentication for API


gsgriffin

Question

I can use the bearer auth id for making a PUT to table, and that works, but I would like to use the client ID and client secret using basic Auth for the CURL in PHP.

I've used base64_encode("clientid:clientsecret") to get the $auth.  $url is going to same URL that works with the bearer id. $data is the same that also works with the bearer auth approach.  Since the Caspio documentation on using the Basic Auth is only 2 sentences long and gives no examples or help, this is what I have, but it does not authenticate.

$auth=base64_encode("my client id:my client secret");
$update=array('TestField'=> "quick Value");
$postdata=json_encode($update);

$url = 'my url.../tables/Test/records?response=rows&q.where=PID%3D1

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
    CURLOPT_USERPWD => $auth,
    CURLOPT_CUSTOMREQUEST => "PUT",
    CURLOPT_POSTFIELDS => $data,
    CURLOPT_HTTPHEADER => array(
        "accept: application/json",
        "cache-control: no-cache",
        "content-type: application/json",
        'Content-Length: ' . strlen($data)
    )
));

$result = curl_exec($curl);

curl_close($curl);

echo $result;

Am I misunderstanding this option?  I would like to authenticate without having to use the bearer since that expires.  I want a permanent solution that never expires and thought that is what this approach is for.

 

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...