shell – 使用cURL登录堆栈溢出
我正在开发一个项目,我想通过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 https://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>
Google登录服务是针对您使用的特定服务(Google文档与Google Analytics(分析)和Google地图等)).您指定的服务(lh2)特定于Google Picasa.
不幸的是,似乎没有OpenID的等效代码(至少不是我可以找到!) 从Google回来的页面应该包含一个登录表单.如果你看这个,应该可以构建卷曲调用来登录;那么应该将您重定向到SO(或您要登录的任何OpenID页面)登录. 事实证明,这样做有点棘手,因为您必须解析出一些表单域才能将其提交回Google,因为Google不会发回直接的HTTP重定向,而是发送带有< ; 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 https://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" (请注意,我似乎有一个略微不同的卷曲版本,所以我不得不稍微改变-w选项;我猜我的版本会适合你,但你可能需要调整它.) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |