2010-05-21 2 views
14

프로젝트 작업 중이며 cURL을 통해 스택 오버플로에 로그인하고 싶습니다.cURL을 사용하여 스택 오버플로로 로깅

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; 

내가 this page에 따르면, --header ... $Auth 부분에 로그인 할 수 있습니다. 여기

내가 지금까지 가지고있는 코드입니다 로그인으로 간주하여 Stack Overflow로 리디렉션해야합니다. 여기
Can't call method "attr" on an undefined value at - line 8. 
    curl: (3) <url> malformed 
--></style> 

google2.html

의 출력은 : 나는 다음과 같은 오류가 아래의 답을하려고하면

<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> 

:이 스크립트를 실행할 때 다음

내가 얻는 형태이다
<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

컬의 UserAgent 변경을 시도해 보셨습니까? – ZyX

+1

ZyX - 어떤 이유로 UA가 변경 되었습니까? 이 시점에서 UA 특정 코드를 제공하는 것만으로는 로그인하지 않습니다. –

+1

PHP 사용자 에이전트로 로그인하는 것을 허용하지 않는 보안 기능을 가지고있을 수도 있습니다 ... –

답변

9

Google 로그인 서비스는 사용중인 특정 서비스에만 해당됩니다 (Go ogle 문서 대 Google Analytics vs Google지도 등). 귀하가 지정한 서비스 (lh2)는 Google Picasa에만 적용됩니다.

는 불행하게도 (적어도 내가 찾을 수없는 것을!)

는 로그인 양식을 포함해야 다시 구글에서 얻을 페이지 오픈 ID에 상응하는 코드가있을 것 같지 않습니다. 당신이 그것을 보았다면, 로그인 할 컬 호출 (curl invocation)을 구성 할 수 있어야합니다. 그런 다음 다시 로그인해야하는 SO (또는 로그인하려는 openID 페이지)로 리디렉션해야합니다.

양식 필드 중 일부를 구문 분석해야하기 때문에이 작업이 다소 까다로운 것으로 나타났습니다 Google에 다시 제출해야하며 Google이 직접 HTTP 리디렉션을 보내지 않기 때문에 <meta http-equiv="redirect" ...> 태그가있는 HTML 문서를 보내야합니다. 물론 쿠키를 활성화해야합니다. 그러나 이것은 컬을 이용하여 스크립트에서 모든 수 있습니다 - 다음 작품을 나를 위해 :

#!/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 - "[email protected]" <<EOF 
     @[email protected]; 
     \$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" 

(나는 당신에게서 컬의 약간 다른 버전을 갖고있는 것 같다 있습니다, 그래서 약간 -w 옵션을 변경했다; 내 버전이 맞을 것 같지만 조정할 필요가 있습니다.)

+0

형태로 서비스 코드를 찾았고 그것을 위의 코드 ("lso")로 바꿨다. . 그러나 이것은 여전히 ​​로그인하지 않습니다. –

+0

내가 말한 것은 말풍선을 사용하여 양식 자체를 제출하는 것입니다. 양식의 내용 (FORM 태그의 내용 만)을 게시하면 추가 힌트를 제공 할 수 있습니다. .. – psmears

+0

@psmears : 양식을 게시했습니다. –

관련 문제