Jump to content
MediaWiki

API:Account creation/Sample code 1

From mediawiki.org

Python

[edit ]
#!/usr/bin/python3
"""
 create_account.py
 MediaWiki API Demos
 Demo of `createaccount` module: Create an account on a wiki without the
 special authentication extensions
 MIT license
"""
importrequests
S = requests.Session()
WIKI_URL = "http://dev.wiki.local.wmftest.net:8080"
API_ENDPOINT = WIKI_URL + "/w/api.php"
# First step
# Retrieve account creation token from `tokens` module
PARAMS_0 = {
 'action':"query",
 'meta':"tokens",
 'type':"createaccount",
 'format':"json"
}
R = S.get(url=API_ENDPOINT, params=PARAMS_0)
DATA = R.json()
TOKEN = DATA['query']['tokens']['createaccounttoken']
# Second step
# Send a post request with the fetched token and other data (user information,
# return URL, etc.) to the API to create an account
PARAMS_1 = {
 'action': "createaccount",
 'createtoken': TOKEN,
 'username': 'your_username',
 'password': 'your_password',
 'retype': 'retype_your_password',
 'createreturnurl': WIKI_URL,
 'format': "json"
}
R = S.post(API_ENDPOINT, data=PARAMS_1)
DATA = R.json()
print(DATA)

PHP

[edit ]
<?php
/*
 create_account.php
 MediaWiki API Demos
 Demo of `createaccount` module: Create an account on a wiki without the
 special authentication extensions
 MIT license
*/
$wikiUrl = "http://dev.wiki.local.wmftest.net:8080";
$endPoint = $wikiUrl . "/w/api.php";
$createAccount_Token = getCreateAccountToken(); // Step 1
createAccount( $createAccount_Token ); // Step 2
// Step 1: GET request to fetch createaccount token
function getCreateAccountToken() {
	global $endPoint;
	$params1 = [
		"action" => "query",
		"meta" => "tokens",
		"type" => "createaccount",
		"format" => "json"
	];
	$url = $endPoint . "?" . http_build_query( $params1 );
	$ch = curl_init( $url );
	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 );
	$result = json_decode( $output, true );
	return $result["query"]["tokens"]["createaccounttoken"];
}
// Step 2: POST request with the fetched token and other data (user information,
// return URL, etc.) to the API to create an account
function createAccount( $createAccount_Token ) {
	global $endPoint, $wikiUrl;
	$params2 = [
		"action" => "createaccount",
		"createtoken" => $createAccount_Token,
		"username" => "your_username",
		"password" => "your_password",
		"retype" => "retype_your_password",
		"createreturnurl" => $wikiUrl,
		"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( $params2 ) );
	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 );
	echo( $output );
}

JavaScript

[edit ]
/* 
 create_account.js

 MediaWiki API Demos
 Demo of `createaccount` module: Create an account on a wiki without the
 special authentication extensions
 MIT license
*/
varrequest=require('request').defaults({jar:true}),
wikiUrl="http://dev.wiki.local.wmftest.net:8080",
endPoint=wikiUrl+"/w/api.php";
// Step 1: GET request to fetch createaccount token
functiongetCreateAccountToken(){
varparams_0={
action:"query",
meta:"tokens",
type:"createaccount",
format:"json"
};
request.get({url:endPoint,qs:params_0},function(error,res,body){
if(error){
return;
}
vardata=JSON.parse(body);
createaccount(data.query.tokens.createaccounttoken);
});
}
// Step 2: POST request with the fetched token and other data (user information,
// return URL, etc.) to the API to create an account
functioncreateaccount(createaccount_token){
varparams_1={
action:"createaccount",
username:"your_username",
password:"your_password",
retype:"retype_your_password",
createreturnurl:wikiUrl,
createtoken:createaccount_token,
format:"json"
};
request.post({url:endPoint,form:params_1},function(error,res,body){
if(error){
return;
}
console.log(body);
});
}
// Start From Step 1
getCreateAccountToken();

MediaWiki JS

[edit ]
/*
	create_account.js
	MediaWiki API Demos
 Demo of `createaccount` module: Create an account on a wiki without the
 special authentication extensions
	MIT License
*/
varparams={
action:'query',
meta:'tokens',
type:'createaccount',
format:'json'
},
api=newmw.Api();
api.get(params).done(function(data){
vartoken=data.query.tokens.createaccounttoken,
params1={
action:'createaccount',
username:'your_username',
password:'your_password',
retype:'retype_your_password',
createreturnurl:'http:'+mw.config.get('wgServer'),
createtoken:token,
format:'json'
};
api.post(params1).done(function(data){
console.log(data);
});
});

AltStyle によって変換されたページ (->オリジナル) /