واجهة برمجة التطبيقات:Tokens
| واجهة برمجة التطبيقات لتطبيق Action على ميدياويكي |
|---|
| الأساسيات |
| التصديق |
| الحسابات والمستخدمين |
| عمليات الصفحات |
| البحث |
| أدوات المطورين |
| برامج تعليمية |
| ع · ن · ت |
تقدم وحدة واجهة برمجة التطبيقات:Tokens البرمجية الرموز المطلوبة لتنفيذ التصرفات التي تعدل بيانات مثل القيد في السجل أو التعديل أو نقل صفحة، وكذا مراقبة التغييرات ومتابعتها. لكل تصرف تحتاج للحصول على نوع محدد من الرموز. على سبيل المثال: لو كنت ترغب في تسجيل الدخول إلى موقع ويكي مستخدمًا واجهة برمجة التطبيقات لتطبيق Action، سوف تحتاج لرمز من النوع «login» كي تواصل العمل.
للمساعدة في ترحيل كود قديم، ارجع إلى تقادم متغيرات رموز واجهة برمجة التطبيقات القديمة
All tokens change every time you visit a different page or refresh the page. Tokens are generated for both logged in and logged out users. A secret hash that is used to compute all other tokens is stored in whatever storage engine is specified at Manual:$wgSessionCacheType.
Be careful not to post your tokens publicly, for example in screenshots when trying to get technical help. Someone who has one of your un-expired tokens can do things as if they were logged into your account.
توثيق واجهة برمجة التطبيقات
meta=tokens
- Source: MediaWiki
- License: GPL-2.0-or-later
Gets tokens for data-modifying actions.
- type
Types of token to request.
- Values (separate with | or alternative): createaccount, csrf, deleteglobalaccount, login, patrol, rollback, setglobalaccountstatus, userrights, watch
- To specify all values, use *.
- Default: csrf
- Retrieve a csrf token (the default).
- api.php?action=query&meta=tokens [open in sandbox]
- Retrieve a watch token and a patrol token.
- api.php?action=query&meta=tokens&type=watch|patrol [open in sandbox]
مثال
طلب GET
النتيجة
{ "batchcomplete":"", "query":{ "tokens":{ "logintoken":"9ed1499d99c0c34c73faa07157b3b6075b427365+\\" } } }
عينة من الكود البرمجي
Python
#!/usr/bin/python3 """ tokens.py MediaWiki API Demos Demo of `Token` module: Fetch token of type `login` MIT License """ importrequests S = requests.Session() URL = "https://www.mediawiki.org/w/api.php" PARAMS = { "action": "query", "meta": "tokens", "type": "login", "format": "json" } R = S.get(url=URL, params=PARAMS) DATA = R.json() LOGIN_TOKEN = DATA['query']['tokens']['logintoken'] print(LOGIN_TOKEN)
PHP
<?php /* tokens.php MediaWiki API Demos Demo of `Token` module: Fetch token of type `login` MIT License */ $endPoint = "https://www.mediawiki.org/w/api.php"; $params = [ "action" => "query", "meta" => "tokens", "type" => "login", "format" => "json" ]; $url = $endPoint . "?" . http_build_query( $params ); $ch = curl_init( $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // Wikis such as Wikimedia Commons require that you also set a user agent through CURLOPT_USERAGENT $output = curl_exec( $ch ); curl_close( $ch ); $result = json_decode( $output, true ); echo( $result['query']['tokens']['logintoken'] . "\n" );
Perl
#!/usr/bin/perl # tokens.pl # MediaWiki API Demos # Demo of `Token` module: Fetch token of type `login` # WTFPL usestrict; usewarnings; useJSON; useLWP::UserAgent; useURI; my$url=URI->new('https://www.mediawiki.org/w/api.php'); my%params=( 'action'=>'query', 'meta'=>'tokens', 'type'=>'login', 'format'=>'json', ); $url->query_form(%params); my$ua=LWP::UserAgent->new(); my$response=$ua->get($url); my$data=JSON::decode_json($response->content); my$login_token=$data->{'query'}{'tokens'}{'logintoken'}; print"$login_token\n";
Node.js
/* tokens.js MediaWiki API Demos Demo of `Token` module: Fetch token of type `login` MIT License */ varrequest=require('request'), url="https://www.mediawiki.org/w/api.php"; varparams={ action:"query", meta:"tokens", type:"login", format:"json" }; request.get({url:url,qs:params},function(error,response,body){ body=JSON.parse(body); console.log(body.query.tokens.logintoken); });
MediaWiki JS
/* tokens.js MediaWiki API Demos Demo of `Token` module: Fetch token of type `csrf` MIT License */ varparams={ action:'query', meta:'tokens', type:'csrf', format:'json' }, api=newmw.Api(); api.get(params).done(function(data){ console.log(data.query.tokens.csrftoken); });
BASH
exportMW_URL="https://www.mediawiki.org/w/" exportAPI_URL="api.php" # 0ドル <token type> # mainly csrf and login functionget-token(){ API_URL="api.php" RESULT=$(curl-fsSL-XPOST\ -daction=query\ -dmeta=tokens\ -dtype="1ドル"\ -dformat=json\ -ccookie.txt\ -bcookie.txt\ "${MW_URL}${API_URL}") RESULT=${RESULT/*token\":\"} TOKEN=${RESULT%\\\"*} echo"$TOKEN" } TOKEN=$(get-tokenlogin)
الأخطاء المحتملة
| الكود | معلومات |
|---|
تاريخ المتغيرات
- v1.27: إضافة
login,createaccount
انظر أيضا
- API:Extensions#Edit token
- سبيل متقادم للحصول على الرموز: API:Tokens (action)