14

我正在做一个项目,我想通过 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;

此时我从谷歌得到一个页面,告诉我 Stack Overflow 需要信息,我必须登录。根据这个页面,这--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>
4

1 回答 1

9

谷歌登录服务特定于您使用的特定服务(谷歌文档、谷歌分析、谷歌地图等)。您指定的服务代码 (lh2) 特定于 Google Picasa。

不幸的是,似乎没有 OpenId 的等效代码(至少,我找不到!)

您从 Google 返回的页面应包含登录表单。如果您看一下,应该可以构造一个 curl 调用来登录;然后应该将您重定向回登录的 SO(或您要登录的任何 openID 页面)。

事实证明,这样做有点棘手,因为您必须解析出一些表单字段才能将它们提交回 Google,而且因为 Google 不会发回直接的 HTTP 重定向,而是带有<meta http-equiv="redirect" ...>标签的 HTML 文档. 当然,您必须启用 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稍微更改选项;我猜我的版本对您有用,但您可能需要对其进行调整。)

于 2010-05-29T18:20:54.170 回答