使用 curl 通过 SSO 重定向获取 MediaWiki 页面信息

Using curl to fetch MediaWiki page info through SSO redirects

tl;dr:对 URL 的 HTTP 请求通过 Chrome 和 Firefox 有效,但使用 curl 失败。

详情:

我的公司使用 MediaWiki 作为 Intranet wiki。我的应用程序曾经成功地使用 cURL 通过以下命令获取有关页面的信息:

curl -s -k -u "username:password" "https://wiki.intranet/mainwiki/api.php?action=query&titles=SomePage&prop=info&format=xml"

但是,IT 部门最近更改了 wiki 以使用某种 Single-Sign On 系统。如果我请求 URL 现在我得到一系列 HTTP 302 重定向,经过以下位置:

当我使用 cURL 请求最后一个 URL 时,我从服务器收到 400(错误请求)响应。用-I参数显示headers,这里是:

> curl -S -k -I -u "username:password" "https://wiki.intranet/sso/?request_uri=/mainwiki/api.php%3faction=query&titles=SomePage&prop=info&format=xml"
HTTP/1.1 400 Bad Request
Date: Wed, 29 Apr 2015 20:47:49 GMT
Server: Apache/2.2.3 (CentOS)
Connection: close
Content-Type: text/html; charset=iso-8859-1

当我使用 Google Chrome 请求相同的 URL 时,我看到了我想要的结果!

如何使用 curl 获取信息?


编辑: 当我使用 Firefox 请求原始 URL 时,它也经过相同的重定向,然后显示两个 401(需要授权)状态。当我在 Firefox 中填写 username/password 时,它会通过另一个重定向 回到原来的 url,点它得到一个状态 200 响应与我想要的 XML。

最后通过添加以下内容使它起作用:

curl -s -k-L --ntlm -c cookies.txt-u "user:pass" "originalurl"

请注意,某些页面建议您可以在 Windows 上使用 -u : 来传递您的凭据。这对我不起作用。

这在 Windows 上使用以下 curl 版本有效:

c:\> curl --version
curl 7.36.0 (x86_64-w64-mingw32) libcurl/7.36.0 OpenSSL/1.0.1c zlib/1.2.8
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smtp smtps telnet tftp
Features: Largefile NTLM SSL libz TLS-SRP