This topic is locked
[SOLVED]

REST API - ODOO Authentication

2/6/2024 1:10:04 PM
PHPRunner General questions
mic'',) author

Hi,

My 1st time using REST API here, I have this credentials below and no problem on postman

My authentication (in postman body)
https://staging-company.odoo.com/v1/api/authenticate/

{
"jsonrpc": "2.0",
"params": {
"login": "api_user@mail.com",
"password": "1234",
"db": "company-staging"
}
}

I have trigger in my customer data once I create new customer in phprunner, I have button that will push the record to ODOO via REST API

$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
),
CURLOPT_URL => 'https://staging-company.odoo.com/v1/api/customers/',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array(
"active" => "1",
"company_type" => "person",
"name" => "COMPANY TEST ONLY",
"first_name" => "COMPANY",
"middle_name" => "TEST",
"last_name" => "ONLY",
"street" => "31 ABC Street",
"city" => "Manila",
"zip" => "1234",
"country_id" => "176",
"state_id" => "1",
"phone" => "09765437843",
"mobile" => "09123458489",
"email" => "inditest1@sample.com",
"contact_address" => "31 ABC Street",
"vat" => "000–123–456–003",
"industry_id" => "5",
"property_payment_term_id" => "2 Months",
"ref" => "RefTest1",
"company_registry" => "ComRegTest1",
"property_account_payable_id" => "1",
"property_account_receivable_id" => "1",
"customer_rank" => "1",
"supplier_rank" => "0",
"credit_limit" => "10000",
"credit" => "3000"),
));
curl_exec($curl);
curl_close($curl);
echo $response;

nothing happen when I click the button, what did I missed?

TIA

admin 2/6/2024

It is not very clear how PHPRunner applies here since you are using your own custom PHP code here.

What happens when you run this code manually?

mic'',) author 2/6/2024

nothing happen, what did I missed?

I put the code in the server using custom button

W
wpl 2/7/2024

Your code says:

echo $response;

But nothing was assigned to $response, so it seems to be empty. Therefore, you should try:

$response = curl_exec($curl);

beforehand and see what will be returned.

Regards

admin 2/7/2024

I don't think you understood the question.

Did you run this code manually, outside of PHPRunner?

mic'',) author 2/8/2024

Hi Sergey,

Did you run this code manually, outside of PHPRunner?

Yes

and tried this on custom buttom on server side to check if theres a response, but nothing happen as well

tho what I found, I need the authenticated session and I tried this

img alt

img alt
I got a request for authetication inside phprunner but, no idea how do I get the authenticated session_id

Ive tried the authentication (code below) outside phprunner but, unfornately Ive got 0 value response heres my code, no idea what I missed.

<?php
$body_data = array(
'jsonrpc' => '2.0',
'params' => array(
'login' => 'company_api_user@email.com',
'password' => '#########'
)
);

$data_string = json_encode($body_data);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://staging-companyname.odoo.com/v1/api/authenticate/");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING,'');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: '.strlen($data_string),
//'Cookie: session_id='.json_decode($res)->result->session_id
)
);

$res= curl_exec($curl);
print_r($res);
print_r(json_decode($res)->result->session_id); // just to display the authenticated Session ID
curl_close($curl);

the response

0
Warning: Attempt to read property "result" on null
Warning: Attempt to read property "session_id" on null

Any thoughts? Thanks in advance

admin 2/8/2024

You need to make sure that your code runs outside of PHPRunner first. Once you got it working you can copy and paste it into button's Server event. By placing the untested code there yuo are just adding an extra level of complexity.

Your REST API provider should provide some sample code that you can use. Also, Postman can generate PHP code for you.

mic'',) author 2/8/2024

Hi Sergey

Yes and I've done testing outside PHPR

img alt

Also, integrating this inside PHPRunner via custom custom button.

img alt

img alt

I just add this (code below), and created a function in After Application Initialized, to get the return authenticated session.

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

I dont need to use xmlrpc for authentication when integrating to ODOO

Creating, updating and deleting of customers, product successfully done on test server.