• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Resolved Curl doesn't pass parameters to Plesk Api

serreri

New Pleskian
Server operating system version
Debian 10
Plesk version and microupdate number
Plesk Obsidian Web Pro Edition Ver. 18.0.58
Hello everyone.

I generated my Api-Key in ssh and the generated key works perfectly in the Plesk REST API page, where I can
to read and create the clients.

I am using a curl script I found on this forum that is supposed to be working but on my server I can't
get it to work.

The code is this:

//-----------------------------------
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://****.com:8443/api/v2/clients');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n \"name\": \"John Rambo\",\n \"company\": \"Crow\",\n \"login\": \"john-Crow\",\n \"status\": 0,\n \"email\": \"[email protected]\",\n \"locale\": \"en-US\",\n \"owner_login\": \"admin\",\n \"external_id\": \"link:12345\",\n \"description\": \"Nice guy\",\n \"password\": \"Pass__Crow\",\n \"type\": \"customer\"\n}");

$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'X-Api-Key: ********key****';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);

//----------------------------------

For a reason I do not understand I get no error output, but the customer is not created in my plesk.

I searched through the various forum posts but could not find a solution

Is anyone kind enough to give me a tip?

Hi thanks
 
You can omit the owner_login or replace the owner_login value with anything other than admin.
 
You can omit the owner_login or replace the owner_login value with anything other than admi
Unfortunately, nothing changes, I get no results.
I also tried to generate errors by changing the key but nothing happens.

Yet this script should work.

DeepL Translate: I always use it apologize for any mistakes
 
Ok I solved it in part:

Using API-Key generated in SSH (plesk bin secret_key -c -ip-address xx.xxx.xx.xxx -description "Key")
the curl returns "error: The requested URL returned error: 401 Unauthorized"

At this point I created my API-Key directly in my php page:

curl_setopt($ch, CURLOPT_URL, 'https://xxxxx:8443/api/v2/auth/keys');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{}");
curl_setopt($ch, CURLOPT_USERPWD, 'admin' . ':' . 'xxxpasswordxxx');
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Accept: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

After creating my API-Key now I can create im my clients in the plesk.

What I don't understand is how can I generate an api Key without using my server login information with CURLOPT_USERPWD.

It does not seem safe to me to use /api/v2/auth/keys,

Is there another more secure system?

Hi

Sorry for the translation, I use DeepL.
 
Make sure the IP address you use to generate the API KEY is actually the right IP address of the computer or server you are running your script from (which could be a IPv4 or IPv6 IP). If a wrong IP address is used while generating the API KEY, the API cannot be accessed.
 
The ip used is that of my server and was generated in SSH with the command

plesk bin secret_key -c -ip-address xx.xxx.xx.xxx -description "Key"

but when I use the key in the curl I get the 401 message again.

Probably the generated key is a root key and is not accepted in the site pages. Maybe a permissions issue.

I have a doubt however about the generated key inside my site, and it is this:

Does X-API-Key once it has been generated have an expiration date?

Can it be used forever in my script?


Regards
 
The ip used is that of my server and was generated in SSH with the command

plesk bin secret_key -c -ip-address xx.xxx.xx.xxx -description "Key"
The IP address used create the API key needs to match the IP address from which the API call gets made. So if you used your server IP address to generate the API key, you also need to run the PHP script from your server. Are you running the PHP script from a domain on your server? Or are running it from your local computer/laptop?

Probably the generated key is a root key and is not accepted in the site pages. Maybe a permissions issue.
It's perfectly fine if you generated the key as Root.

Does X-API-Key once it has been generated have an expiration date?
No, the API key does not have an expiration date.
 
the script is launched from my site inside my server, so same Ip.

I will try again to regenerate the key from root and use it in my php script always inside my server, maybe I made a mistake the first time.
Thanks
 
So I can confirm that by creating X-API-Key via the SSH command by also entering the ip of the server

plesk bin secret_key -c -ip-address 00.000.000.000

the curl script present on the server returns error 401:

The requested URL returned error: 401 Unauthorized

Whereas if I create the key by omitting the server ip

plesk bin secret_key -c

then everything works and the client is created.

This was my error

Thank you
 
Back
Top