Wie kann ich die Sitzungs-ID nach erfolgreicher Authentifizierung in Odoo JSON-RPC für die Rechnungserstellung abrufen?Php

PHP-Programmierer chatten hier
Guest
 Wie kann ich die Sitzungs-ID nach erfolgreicher Authentifizierung in Odoo JSON-RPC für die Rechnungserstellung abrufen?

Post by Guest »

Ich entwickle ein PHP-Skript zur Integration mit Odoo mithilfe von JSON-RPC. Das Ziel besteht darin, einen Benutzer zu authentifizieren und dann eine benutzerdefinierte Rechnung in der Odoo-Datenbank zu erstellen. Der Authentifizierungsprozess gibt die Benutzer-ID (uid) erfolgreich zurück, aber die Sitzungs-ID (session_id) ist leer. Ohne die Sitzungs-ID schlagen nachfolgende Anfragen (z. B. zum Erstellen der Rechnung) aufgrund des Sitzungsablaufs fehl.
function odoo_jsonrpc($url, $method, $params, $session_id = null) {
$data = array(
"jsonrpc" => "2.0",
"method" => $method,
"params" => $params,
"id" => rand(1, 1000000),
);

$data_string = json_encode($data);

$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array_filter([
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
$session_id ? 'Cookie: session_id=' . $session_id : null,
]),
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_SSL_VERIFYPEER => false,
]);

$response = curl_exec($ch);

if (curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}

curl_close($ch);

return json_decode($response, true);
}

// 1. Authenticate
$auth_params = [
"db" => $db_name,
"login" => $username,
"password" => $password,
];

$auth_result = odoo_jsonrpc($url . '/web/session/authenticate', 'call', $auth_params);

if (!$auth_result || isset($auth_result['error'])) {
die("Authentication error: " . json_encode($auth_result));
}

$uid = $auth_result['result']['uid'];
$session_id = $auth_result['result']['session_id'];
echo "Authenticated with UID: $uid, Session ID: $session_id\n";

// 2. Create Invoice
$invoice_data = [
'name' => uniqid('INV-'),
'partner_id' => 9, // Replace with your partner ID
'user_id' => 2, // Replace with your User ID
'invoice_line_ids' => [[0, 0, [
'name' => 'Product 1',
'quantity' => rand(1, 5),
'price_unit' => rand(10, 100),
'account_id' => 1, // Replace with your account ID
'product_id' => 1, // Replace with your product ID
]]],
'move_type' => 'out_invoice',
];

$create_params = [
'model' => 'account.move',
'method' => 'create',
'args' => [$invoice_data],
];

$create_result = odoo_jsonrpc($url . '/web/dataset/call_kw', 'call', $create_params, $session_id);

if (!$create_result || isset($create_result['error'])) {
die("Invoice creation error: " . json_encode($create_result));
}

$invoice_id = $create_result['result'];
echo "Invoice created with ID: " . $invoice_id . "\n";


Problem – Ich habe überprüft, ob die Datenbank-, Login- und Passwortvariablen korrekt sind. Die Antwort gibt die UID erfolgreich zurück, sodass die Authentifizierung teilweise zu funktionieren scheint.
Der kürzeste Weg, mein Problem zu reproduzieren: Erstellen Sie ein Demokonto in Odoo und fügen Sie eine Datenbank und eine Rechnungsanwendung in der Datenbank hinzu, dann mit Ihrem Anmeldeinformationen führen diesen Code aus

function odoo_jsonrpc($url, $method, $params, $session_id = null) {
$data = array(
"jsonrpc" => "2.0",
"method" => $method,
"params" => $params,
"id" => rand(1, 1000000),
);

$data_string = json_encode($data);

$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array_filter([
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
$session_id ? 'Cookie: session_id=' . $session_id : null,
]),
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_SSL_VERIFYPEER => false,
]);

$response = curl_exec($ch);

if (curl_errno($ch)) {
die('Curl error: ' . curl_error($ch));
}

curl_close($ch);

return json_decode($response, true);
}

// 1. Authenticate
$auth_params = [
"db" => $db_name,
"login" => $username,
"password" => $password,
];

$auth_result = odoo_jsonrpc($url . '/web/session/authenticate', 'call', $auth_params);

if (!$auth_result || isset($auth_result['error'])) {
die("Authentication error: " . json_encode($auth_result));
}

$uid = $auth_result['result']['uid'];
$session_id = $auth_result['result']['session_id'];
echo "Authenticated with UID: $uid, Session ID: $session_id\n";


Ich habe versucht, Protokolle in der Odoo-Weboberfläche zu finden, die ich nicht finden konnte, und habe die Anmeldeinformationen überprüft, um festzustellen, ob das Problem dadurch verursacht wird

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post