MediaWiki API error: Login failed: Unable to continue login. Your session most likely timed out

28 Views Asked by At

I try logging in with .php script into my local MediaWiki page. I am using a bot account credentials. But I am getting this error Login failed: Unable to continue login. Your session most likely timed out.

Code:

<?php
// Set your MediaWiki API endpoint
$apiUrl = 'http://localhost/sportwiki/api.php';

// Set your login credentials
$username = 'A.mov09@BulkPages';
$password = '0oa8kohha41hl2uq690cnlfpvbgk6kuf';

// Step 1: Get login token
$loginToken = getLoginToken($apiUrl);

// Step 2: Log in
login($apiUrl, $username, $password, $loginToken);

// Function to get login token
function getLoginToken($apiUrl) {
    $params = array(
        'action' => 'query',
        'meta' => 'tokens',
        'type' => 'login',
        'format' => 'json'
    );

    $url = $apiUrl . '?' . http_build_query($params);

    $response = file_get_contents($url);
    $data = json_decode($response, true);

    return $data['query']['tokens']['logintoken'];
}

// Function to log in
function login($apiUrl, $username, $password, $loginToken) {
    $params = array(
        'action' => 'login',
        'lgname' => $username,
        'lgpassword' => $password,
        'lgtoken' => $loginToken,
        'format' => 'json'
    );

    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);
    curl_close($ch);

    $data = json_decode($response, true);

    if ($data && isset($data['login']['result']) && $data['login']['result'] == 'Success') {
        echo 'Login successful!';
    } else {
        echo 'Login failed: ' . $data['login']['reason'];
    }
}
?>

I am logging in with bot account. I also tried the code from this page, but it outputs the same error.

0

There are 0 best solutions below