User:Evigian/Sandbox/API:Centralauthtoken

MediaWiki version:
1.23

API documentation edit


action=centralauthtoken

(main | centralauthtoken)

Fetch a centralauthtoken for making an authenticated request to an attached wiki.

Returns a token that can be use to authenticate API requests on other wikis. For action API requests, put it in the centralauthtoken GET parameter. For REST API requests, add an Authorization: CentralAuthToken {token} header. In MediaWiki frontend logic, you can use the mediawiki.ForeignApi ResourceLoader module.


Example:
Fetch a centralauthtoken
api.php?action=centralauthtoken [open in sandbox]

Example edit

GET request edit

Description of script


Response edit

{
    "centralauthtoken": {
        "centralauthtoken": "5eb8f0b17c59e32c1a0bae893dccc720391d5f2"
    }
}


Sample code edit

Python edit

#!/usr/bin/python3

"""
    centralauthtoken.py

    MediaWiki Action API Code Samples
    Demo of `centralauthtoken` module: Fetch a centralauthtoken for making an authenticated request to an attached wiki.
    MIT license
"""

import requests

url = "https://en.wikipedia.org/w/api.php"

S = requests.Session()

PARAMS = {
    "action": "centralauthtoken",
    "format": "json",
}

R = S.get(url, data=PARAMS)
DATA = R.json()

print(DATA)

PHP edit

<?php

/*
    centralauthtoken.php

    MediaWiki Action API Code Samples
    Demo of `centralauthtoken` module: Fetch a centralauthtoken for making an authenticated request to an attached wiki.
    MIT license
"""
*/

<?php

$endPoint = "https://en.wikipedia.org/w/api.php"

$get_auth_token = getAuthToken();

function getAuthToken() {
	global $endPoint;
	
	$params = [
		"action" => "centralauthtoken",
		"format" => "json"
	]
		
	$ch = curl_init();

	curl_setopt( $ch, CURLOPT_URL, $endPoint );
	curl_setopt( $ch, CURLOPT_POST, true );
	curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params ) );
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
	curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
	curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );

	$output = curl_exec( $ch );
	curl_close( $ch );
}

Javascript edit

/*
    centralauthtoken.js

    MediaWiki Action API Code Samples
    Demo of `centralauthtoken` module: Fetch a centralauthtoken for making an authenticated request to an attached wiki.
    MIT license
*/

var request = require("request").defaults({jar: true}),
url = 'https://en.wikipedia.org/w/api.php';

function validatePassword() {
    var params = {
        action: "centralauthtoken",
        format: "json"
    };
    
    request.get({ url: url, form: params }, function (error, res, body) {
        if (error) {
            return;
        }
        console.log(body);
    });
}

MediaWiki JS edit

/*
    centralauthtoken.js

    MediaWiki Action API Code Samples
    Demo of `centralauthtoken` module: Fetch a centralauthtoken for making an authenticated request to an attached wiki.
    MIT license
*/

    var params = {
        action: "centralauthtoken",
        format: "json"
    },
	api = new mw.Api();

api.postWithToken( 'csrf', params ).done( function ( data ) {
	console.log( data );
} );



Demo app(s) edit

  • Add a link to the demo app. Embed an image of the demo if applicable

Possible errors edit

Code Info
notloggedin Anonymous users cannot obtain a centralauthtoken.

Parameter history edit

  • v1.x: Introduced parameter_1, parameter_2
  • v0.x: Deprecated parameter_3

Additional notes edit

See also edit

  • Add link to documentation of related modules