我正在开发一个项目,我想通过cURL登录Stack Overflow。
我使用Google作为我的openID提供商,这意味着我需要首先通过其API登录Google。
这是我到目前为止的代码:
#!/bin/sh
. ./params.sh #the script with $username and $password
curl --silent https://www.google.com/accounts/ClientLogin \
-d Email=$username -d Passwd=$password \
-d accountType=GOOGLE \
-d source=localhost-test-1 \
-d service=lso \
-o tokens
. ./tokens
echo $Auth; #$Auth is correct here - I did not get a BadAuth error.
endpoint="https://www.google.com/accounts/o8/id";
curl http://stackoverflow.com/users/authenticate \
-d "openid_identifier=$endpoint" \
-w %{redirect_url}> ./google_url
google_url=$(cat ./google_url);
echo $google_url;
echo;
echo;
echo;
curl -L --silent --header "Authorization: GoogleLogin auth=$Auth" $google_url;
此时我从Google获取了一个页面,告诉我Stack Overflow需要信息,我必须登录。根据this page,--header ... $Auth
部分应该算作登录并将我重定向到Stack Overflow。
这是我运行此脚本时获得的表单:
<form id="gaia_universallogin"
action="https://www.google.com/accounts/ServiceLoginAuth?service=lso" method="post">
<input type="hidden" name="continue" id="continue"
value="https://www.google.com/accounts/o8/ud?st=SOME_KEY" />
<input type="hidden" name="service" id="service"
value="lso" />
<input type="hidden" name="dsh" id="dsh"
value="SOME_NEG_NUMBER" />
</form>
当我尝试下面的答案时,我收到以下错误:
Can't call method "attr" on an undefined value at - line 8.
curl: (3) <url> malformed
--></style>
这是google2.html
<form id="gaia_loginform"
action="https://www.google.com/accounts/ServiceLoginAuth?service=lso" method="post" >
<input type="hidden" name="continue" id="continue" value="https://www.google.com/accounts/o8/ud?st=RNADOM" />
<input type="hidden" name="service" id="service" value="lso" />
<input type="hidden" name="dsh" id="dsh" value="NEG_NUMEBER" />
<input type="hidden" name="GALX" value="ABCD" />
<input type="text" name="Email" id="Email" />
<input type="password" name="Passwd" id="Passwd" >
<input type="checkbox" name="PersistentCookie" id="PersistentCookie" value="yes"
<input type="hidden" name='rmShown' value="1" />
<input type="submit" class="gaia le button" name="signIn" id="signIn" />
<input type="hidden" name="asts" >
</form>
答案 0 :(得分:9)
Google登录服务特定于您正在使用的特定服务(Google文档与Google Analytics与Google地图等)。您指定的服务(lh2)特定于Google Picasa。
不幸的是,似乎没有OpenId的等效代码(至少,不是我能找到的!)
您从Google收到的页面应包含登录表单。如果你看一下,应该可以构建一个curl调用来登录;然后应该将您重定向回到SO(或您要登录的任何openID页面)登录。
事实证明,这样做有点棘手,因为您必须解析一些表单字段以将其提交给Google,并且因为Google不会发回直接的HTTP重定向,而是HTML文档带有<meta http-equiv="redirect" ...>
标记。当然,你必须启用cookie。但是这在使用curl的脚本中是可能的 - 以下对我有用:
#!/bin/bash
# Utility function for parsing values out of an HTML form
get_value()
{
local tagtype="$1" attrname="$2" attrvalue="$3" getattr="$4"
perl -MHTML::TreeBuilder - "$@" <<EOF
@args=@ARGV;
\$h=HTML::TreeBuilder->new;
\$h->parse_file("$htmlfile");
while (\$#args > 0) {
\$h=\$h->look_down(_tag => shift @args,
shift @args => shift @args);
}
print \$h->attr(shift @args);
EOF
}
# Empty the cookie jar
cj="cookiejar"
rm -f "$cj"
# Attempt to log in to SO. This will redirect to a google URL.
endpoint="https://www.google.com/accounts/o8/id"
google_url=`curl -L -s -S http://stackoverflow.com/users/authenticate \
-d "openid_identifier=$endpoint" \
-o /dev/null -b "$cj" -c "$cj" \
-w %{url_effective}`
echo $google_url
echo
echo
# Retrieve the form from Google
htmlfile=googleform.html
curl -L -s -S -o "$htmlfile" -b "$cj" -c "$cj" "$google_url"
# Parse out the form fields
form_url=`get_value form id gaia_loginform action`
fdsh=`get_value form id gaia_loginform input name dsh value`
fcontinue=`get_value form id gaia_loginform input name continue value`
fservice=`get_value form id gaia_loginform input name service value`
fGALX=`get_value form id gaia_loginform input name GALX value`
frmShown=`get_value form id gaia_loginform input name rmShown value`
fsignIn=`get_value form id gaia_loginform input name signIn value`
fEmail='INSERT LOGIN EMAIL HERE'
fPasswd='INSERT PASSWORD HERE'
# Submit the login form
htmlfile=google2.html
curl -L -s -S -o "$htmlfile" -b "$cj" -c "$cj" --data-urlencode dsh="$fdsh" \
--data-urlencode continue="$fcontinue" \
--data-urlencode service="$fservice" \
--data-urlencode GALX="$fGALX" \
--data-urlencode Email="$fEmail" \
--data-urlencode Passwd="$fPasswd" \
--data-urlencode rmShown="$frmShown" \
--data-urlencode signIn="$fsignIn" \
"$form_url"
# Interpret the redirect
redirect=`get_value meta http-equiv refresh content | sed "s/^.*'\(.*\)'.*$/\1/"`
# Follow it
htmlfile=google3.html
curl -L -s -S -o "$htmlfile" -b "$cj" -c "$cj" "$redirect"
(请注意,我似乎有一个与你略有不同的curl版本,所以我不得不稍微改变-w
选项;我猜我的版本对你有用,但你可能需要调整它。)