Topic on Project:Support desk

Not able to create new account on wiki using API

4
Summary by Dan.aluas

It worked. In my example I used the RestClient in a wrong manner.

Dan.aluas (talkcontribs)

Hello,

I'm trying to use the API to create accounts for some users.

My tests are performed from a C# application.

However, I always receive :

{"error": {

  "code": "notoken",

  "info": "The \"token\" parameter must be set.",

My steps are the following:

  • GET on: ?action=query&meta=tokens&type=createaccount
  • Get on: ?action=query&meta=authmanagerinfo&amirequestsfor=create
  • POST on: ?action=createaccount&format=json&token={token}&createreturnurl={URL}" passing username, password, retype and token received on first step as body.

Could you please tell what I'm doing wrong?

Ciencia Al Poder (talkcontribs)

Be sure you're passing the cookies you receive in previous requests

Dan.aluas (talkcontribs)

I wrote this test code:

RestClient client = new RestClient

            {

                CookieContainer = new CookieContainer(),

                BaseUrl = new Uri("http://192.168.56.1:90/api.php")

            };

            var request = new RestRequest("?action=query&meta=tokens&type=createaccount&format=json", Method.GET);

            var response = client.Execute(request);

            Console.WriteLine(response.Content);

            string cookieName = response.Cookies[0].Name;

            string cookieValue = response.Cookies[0].Value;

            var tokens = JsonConvert.DeserializeObject<TokenResponse>(response.Content);

            request = new RestRequest("?action=query&meta=authmanagerinfo&amirequestsfor=create&format=json", Method.GET);

            response = client.Execute(request);

            Console.WriteLine(response.Content);

            request = new RestRequest($"?action=createaccount&format=json&createreturnurl=http://localhost", Method.POST);

            MediaWikiUser user = new MediaWikiUser

            {

                CreateToken = tokens.Query.Tokens.CreateAccountToken,

                UserName = "Bobica",

                Password = "p@ssw0rd",

                Retype = "p@ssw0rd",

                Reason = "Daia"

            };

            request.AddBody(JsonConvert.SerializeObject(user));

            request.AddCookie(cookieName, cookieValue);

            response = client.Execute(request);

            Console.WriteLine(response.Content);

I'm always getting no token.

This post was hidden by 78.96.45.130 (history)