如何使用MediaWiki的api,curl和bash登录? [英] How to login using mediawiki's api, curl, and bash?

查看:109
本文介绍了如何使用MediaWiki的api,curl和bash登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对流程的理解:
从mediawiki登录手册 https://www.mediawiki.org/wiki/API:Login

My understanding of the process:
From mediawikis login manual https://www.mediawiki.org/wiki/API:Login

使用MediaWiki的Web服务API时,您可能需要您的应用程序或客户端登录.这涉及提交登录查询,构建cookie以及通过重新提交带有返回的确认令牌的登录请求来确认登录.

When using MediaWiki's web service API, you will probably need your application or client to log in. This involves submitting a login query, constructing a cookie, and confirming the login by resubmitting the login request with the confirmation token returned.

1)尝试使用用户名和密码登录,这将作为响应html的一部分而失败,并显示"result ="NeedToken"".响应还将包含下一次登录尝试时要传递的令牌.
2)尝试再次登录,这次除了un/pw外还传递了令牌.这应该以'result ="Success"'

1) Attempt to login with username and password, this will fail with 'result="NeedToken"' as part of response html. Response will also contain the token to be passed in for the next login attempt.
2) Attempt to login again, this time passing in token in addition to un/pw. This should return with 'result="Success"'

我的代码:

###Attempt first login setup cookie jar
loginRes1=$(curl --cookie-jar cjar -X POST "$domain/wiki/api.php?action=login&lgname=$lgname&lgpassword=$lgpassword")

###Grab the token from login attempt
lgtoken=$(echo $loginRes1 |sed -rn "s/.*token="([0-9a-zA-Z]+)".*/\1/p" )

###Attempt second login, this time passing token as well
loginRes2=$(curl --cookie-jar cjar -X POST "$domain/wiki/api.php?action=login&lgname=$lgname&lgpassword=$lgpassword&lgtoken=$lgtoken")

结果:

echo $loginRes1
###Only relevant html from echo shown below, cleaned up into xml syntax
<?xml version="1.0"?> <api> <login result="NeedToken" token="944af711913a037cfb8b90d477d51f6c" cookieprefix="ronk" sessionid="isqvhm955lj35g1q2e2klme091" /> </api> 

echo $loginRes2 
###Only relevant html from echo shown below, cleaned up into xml syntax
<?xml version="1.0"?> <api> <login result="NeedToken" token="ffdd1aa6dc3699df26b9de6dd1c6d5a5" cookieprefix="ronk" sessionid="fdahoh4gh7junrqm1tk2p1qd25" /> </api> 

我仍然第二次获得NeedToken结果,而不是我期望的成功.

I'm still getting the NeedToken result the second time, instead of Success as I would expect.

通过浏览器登录
我可以使用提交表单的浏览器正常登录,发布请求包含4个参数:wpName,wpPassword,wpLoginAttempt,wpLoginToken

Logging in via browser
I can login normally with a browser with a form submission, the post request contains 4 parameters: wpName, wpPassword, wpLoginAttempt, wpLoginToken

wpName=myName&wpPassword=myPassword&wpLoginAttempt=Log+in&wpLoginToken=d3fe3a1de6fbc934acd3039149f3c56d


其他说明
1)我确认通过浏览器正常登录时,un/pw可以正常工作.
2)我不清楚我是否正确使用curl cookie-jar语法
3)我不知道要连接的MediaWiki的版本,它是最近安装的,可能是最高的稳定版本.
4)您会注意到在成功的浏览器尝试中,参数使用wp前缀而不是lg,如果我更改curl尝试以匹配(即wpName,wpPassword),则返回结果为:

Other Notes
1) I confirmed that the un/pw works when logging in normally through a browser.
2) It's unclear to me if I'm using the curl cookie-jar syntax appropriately
3) I don't know the version of mediawiki I'm connecting to, it was installed recently and is likely the highest stable version.
4) You'll notice in the successful browser attempt, the parameters have the wp prefix instead of lg, if I change the curl attempt to match (i.e. wpName, wpPassword) then the returned result is:

<?xml version="1.0"?> <api> <warnings> <main xml:space="preserve">Unrecognized parameters: 'wpName', 'wpPassword'</main> </warnings> <login result="NoName" /> </api> 

推荐答案

curl的--cookie-jar选项仅告诉curl将cookie保存到jar中.它不会告诉curl从罐中加载饼干.

The --cookie-jar option to curl only tells curl to save cookies to the jar. It doesn't tell curl to load cookies from the jar.

要卷曲以从罐中加载cookie,您需要使用-b选项指定要使用的cookie jar.

To get curl to load cookies from the jar you need to use the -b option to specify the cookie jar to use.

这篇关于如何使用MediaWiki的api,curl和bash登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆