API:账户创建

From MediaWiki.org
Jump to navigation Jump to search
This page is a translated version of the page API:Account creation and the translation is 48% complete.

Other languages:
Deutsch • ‎English • ‎dansk • ‎español • ‎français • ‎italiano • ‎polski • ‎português • ‎română • ‎български • ‎русский • ‎العربية • ‎ไทย • ‎中文 • ‎日本語 • ‎한국어
MediaWiki版本: 1.27
此页面记录了自MediaWiki 1.27起的创建帐户的API。 此处提供了早期版本中API的文档:Api:Account creation/pre-1.27

API文档


action=createaccount (create)

(main | createaccount)
  • This module requires write rights.
  • This module only accepts POST requests.
  • Source: MediaWiki
  • License: GPL-2.0-or-later

Create a new user account.

The general procedure to use this module is:

  1. Fetch the fields available from action=query&meta=authmanagerinfo with amirequestsfor=create, and a createaccount token from action=query&meta=tokens.
  2. Present the fields to the user, and obtain their submission.
  3. Post to this module, supplying createreturnurl and any relevant fields.
  4. Check the status in the response.
    • If you received PASS or FAIL, you're done. The operation either succeeded or it didn't.
    • If you received UI, present the new fields to the user and obtain their submission. Then post to this module with createcontinue and the relevant fields set, and repeat step 4.
    • If you received REDIRECT, direct the user to the redirecttarget and wait for the return to createreturnurl. Then post to this module with createcontinue and any fields passed to the return URL, and repeat step 4.
    • If you received RESTART, that means the authentication worked but we don't have a linked user account. You might treat this as UI or as FAIL.
Parameters:
createrequests

Only use these authentication requests, by the id returned from action=query&meta=authmanagerinfo with amirequestsfor=create or from a previous response from this module.

Separate values with | or alternative. Maximum number of values is 50 (500 for bots).
createmessageformat

Format to use for returning messages.

One of the following values: html, wikitext, raw, none
Default: wikitext
createmergerequestfields

Merge field information for all authentication requests into one array.

Type: boolean (details)
createpreservestate

Preserve state from a previous failed login attempt, if possible.

If action=query&meta=authmanagerinfo returned true for hasprimarypreservedstate, requests marked as primary-required should be omitted. If it returned a non-empty value for preservedusername, that username must be used for the username parameter.

Type: boolean (details)
createreturnurl

Return URL for third-party authentication flows, must be absolute. Either this or createcontinue is required.

Upon receiving a REDIRECT response, you will typically open a browser or web view to the specified redirecttarget URL for a third-party authentication flow. When that completes, the third party will send the browser or web view to this URL. You should extract any query or POST parameters from the URL and pass them as a createcontinue request to this API module.

createcontinue

This request is a continuation after an earlier UI or REDIRECT response. Either this or createreturnurl is required.

Type: boolean (details)
createtoken

A "createaccount" token retrieved from action=query&meta=tokens

This parameter is required.
*
This module accepts additional parameters depending on the available authentication requests. Use action=query&meta=authmanagerinfo with amirequestsfor=create (or a previous response from this module, if applicable) to determine the requests available and the fields that they use.

创建一个账户

该过程大致有三个步骤:

#从API:Authmanagerinfo 获取字段,从API:令牌 获取令牌。

  1. Send a POST request with the fetched token, user information and other fields, and return URL to the API.
  2. Deal with the response, which might involve further POST requests to supply more information.

Example 1: Process on a wiki without special authentication extensions

A wiki without special authentication extensions can be rather straightforward. If your code knows which fields will be required, it might skip the call to API:Authmanagerinfo and just assume which fields will be needed (i.e. username, password & retyped password, email, possibly realname).

If you are creating an account for someone else, you'll need to specify a reason for the same by including a reason parameter to the POST request. You could also use mailpassword in place of password and retype parameters to have MediaWiki send the new user a temporary password via email.

POST请求

回应

{
    "createaccount": {
        "status": "PASS",
        "username": "Zane"
    }
}

示例代码

create_account.py

#!/usr/bin/python3

"""
    create_account.py

    MediaWiki Action API Code Samples
    Demo of `createaccount` module: Create an account on a wiki without the 
    special authentication extensions

    MIT license
"""

import requests

S = requests.Session()

WIKI_URL = "https://test.wikipedia.org"
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)

Example 2: Process on a wiki with a CAPTCHA extension

Note the first step below could, if you'd rather, be done as two steps: one to fetch the fields available from API:Authmanagerinfo and another to fetch the token from API:令牌 .

First step: Fetch fields available from API:Authmanagerinfo and token from API:令牌

Second step: Send a post request along with a create account token, user information and return URL

示例代码

Note this code sample separates the API:Authmanagerinfo and API:令牌 requests, and generally assumes there will be a CAPTCHA and no other complications.

Example 3: Account creation on a wiki with a CAPTCHA, an OpenID extension, and a two-factor authentication extension enabled

First step: Fetch fields available from API:Authmanagerinfo and token from API:令牌

The fetching of API:Authmanagerinfo and API:令牌 is largely the same as in the previous example, and so is not repeated here. The list of requests returned by API:Authmanagerinfo will include definitions for both the CAPTCHA extension and the OpenID extension.

Second step: Answer the CAPTCHA and select OpenID authentication.

The client would be expected to redirect the user's browser to the provided redirecttarget.

The OpenID provider would authenticate, and redirect to Special:OpenIDConnectReturn on the wiki, which would validate the OpenID response and then redirect to the createreturnurl provided in the first POST to the API with the code and state parameters added.

The client gets control of the process back at this point and makes its next API request.

Third step: Back from OpenID.

The client posts the code and state back to the API. The API's response has the two-factor authentication extension prompting the user to set up their second factor.

Now the client would prompt the user to set up a new account in their two-factor authentication app and enter the current code, or allow the user to skip 2FA setup. 我们假设用户确实设置了2FA(双因素验证)。

第四步:设置双因素身份验证。

帐户创建终于成功了。

If at any point account creation fails, a response with status FAIL will be returned, along with a message to display to the user.

可能的错误

代码 信息
badtoken 无效的创建帐户令牌
notoken token参数必须被设置。
mustpostparams 以下参数在查询字符串中被找到,但必须在POST正文中:createtoken。
missingparam 需要"createcontinue"和⧼word-separator⧽"createreturnurl"中的至少一个参数。
authmanager-create-no-primary 提供的凭据不能用于账户创建。
invalidemailaddress 邮箱地址格式不正确,请输入正确的邮箱地址或清空该输入框。
badretype 您所输入的密码并不相同。
userexists 用户名已存在。请使用其他名称。
captcha-createaccount-fail 验证码错误或丢失。
acct_creation_throttle_hit 在最近一天,使用您的IP地址访问此wiki的访客创建了num个账户,这是此时间段内允许创建的最大数量。因此,使用此IP地址的访客目前不能再创建更多账户。

如果您加入了以贡献维基媒体项目为中心的活动中的话,请参见请求临时提升IP限制一文以帮助解决此问题。

附加提醒

如果您已登录,则在创建帐户时您的用户名也会被记录。

  • 在执行此页面上提供的代码段时,请记住:
    • 一旦创建维基上的帐户,将无法删除该帐户。
    • Always use https://test.wikipedia.org/w/api.php as the endpoint, so that you don't accidentally create accounts on production wikis.
  • MediaWiki site administrators and extension developers can disable this API feature by inserting the following line in the configuration file:
$wgAPIModules['createaccount'] = 'ApiDisabled';


参见