Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
How to login to wikipedia using mediawiki API through?

I already have two working bots in Wikipedia, but they use different method. Currently, I am trying to create a bot using C#. On the API documentation page, they have provided some sample codes in java, PHP, and python. But unfortunately, they haven't provided any sample code in C#. It is the only language I am familiar with.

This is the Wikimedia API:Edit documentation page. Would someone kindly convert the few words from java (or any other language) to C#?

If I could get only this code converted to C#, I can build the rest of the bot by myself. I asked help on the relevant noticeboard(s) on the wikimedia site(s), but nobody there is familiar with C#. That's why I am now asking this outside of wikipedia.

Thanks a lot in advance

This is the java source-code:

```

/*
edit.js

MediaWiki API Demos
Demo of `Login` module: Sending post request to login

MIT license
*/

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

// Step 1: GET request to fetch login token
function getLoginToken() {
var params = {
    action: 'query',
    meta: 'tokens',
    type: 'login',
    format: 'json'
};

request.get( { url: url, qs: params }, function ( error, res, body ) {
    var data;
    if ( error ) {
        return;
    }
    data = JSON.parse( body );
    loginRequest( data.query.tokens.logintoken );
} );
}

// Step 2: POST request to log in.
// Use of main account for login is not
// supported. Obtain credentials via Special:BotPasswords
// (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword
function loginRequest( loginToken ) {
var params = {
    action: 'login',
    lgname: 'bot_username',
    lgpassword: 'bot_password',
    lgtoken: loginToken,
    format: 'json'
};

request.post( { url: url, form: params }, function ( error, res, body ) {
    if ( error ) {
        return;
    }
    console.log( body );
} );
}

// Start From Step 1
getLoginToken();

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I can't go so deeply but I hope you can take a look at this page and find out how to do it

https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-6.0

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can try something like this

private static async Task LoginRequest(string loginToken)
{
    var data = new
    {
        action = "login",
        lgname = "bot_username",
        lgpassword = "bot_password",
        lgtoken = loginToken,
        format = "json"
    };
    using (var client = new HttpClient())
    using (var response = await client.PostAsJsonAsync(LOGIN_URL, data))
    {
        var responseString = await response.Content.ReadAsStringAsync();
        var responseObject = JsonConvert.DeserializeObject<JObject>(responseString);
        Console.WriteLine(responseObject);
    }
}

private static async Task<string?> GetLoginToken()
{
    var url = $"{LOGIN_URL}?action=query&meta=tokens&type=login&format=json";
    using (var client = new HttpClient())
    using (var response = await client.GetAsync(url))
    {
        var responseString = await response.Content.ReadAsStringAsync();
        var responseObject = JsonConvert.DeserializeObject<JObject>(responseString);
        return responseObject?["query"]?["tokens"]?["logintoken"]?.ToString();
    }
}

For converting the response you need

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

You can call these methods from your Main method like this:

const string LOGIN_URL = "https://test.wikipedia.org/w/api.php";

public static async Task Main()
{
    var loginToken = await GetLoginToken();
    await LoginRequest(loginToken ?? throw new Exception("No login token."));
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda