User:XZise/Login with cURL

From mediawiki.org

In case you want to check an API call without using any framework in between (to prevent that from being the reason for the failure) cURL is quite helpful but for that you might need to be logged in which is a two step process:

$ PASS=  # the password
$ NAME=  # the username without prefix like: XZise
$ API=   # the API path like: https://test.wikipedia.org/w/api.php
$ curl -b cookie.lwp -c cookie.lwp -d action=login -d lgname=$NAME -d lgpassword=$PASS -d format=json $API
{"login":{"result":"NeedToken","token":"xx…xx","cookieprefix":"testwiki","sessionid":"xx…xx"}}

That token is then copied into the next request:

$ curl -b cookie.lwp -c cookie.lwp -d action=login -d lgname=$NAME -d lgpassword=$PASS --data-urlencode lgtoken=xx…xx -d format=json $API
{"login":{"result":"Success","lguserid":x,"lgusername":"x","lgtoken":"xx…xx","cookieprefix":"testwiki","sessionid":"xx…xx"}}

And then you are logged in. If you have jq installed you don't need to manually parse the JSON response:

$ PASS=  # the password
$ NAME=  # the username without prefix like: XZise
$ API=   # the API path like: https://test.wikipedia.org/w/api.php
$ LGTOKEN=$(curl -b cookie.lwp -c cookie.lwp -d action=login -d lgname=$NAME -d lgpassword=$PASS -d format=json $API | jq '.login.token' -r)
$ curl -b cookie.lwp -c cookie.lwp -d action=login -d lgname=$NAME -d lgpassword=$PASS --data-urlencode lgtoken=$LGTOKEN -d format=json $API
{"login":{"result":"Success","lguserid":x,"lgusername":"x","lgtoken":"xx…xx","cookieprefix":"testwiki","sessionid":"xx…xx"}}