I’m working on a project and I want to log into Stack Overflow via cURL.
I use Google as my openID provider which means that I need to log into Google first via its API.
Here is the code I have so far:
#!/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;
At this point I get a page from Google telling me that Stack Overflow wants information and I have to log in. According to this page, the --header ... $Auth part should count as a login and redirect me to Stack Overflow.
Here is the form I get when I run this script:
<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>
When I try the answer below I get the following error:
Can't call method "attr" on an undefined value at - line 8.
curl: (3) <url> malformed
--></style>
here is the output from 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>
The Google login service is specific to the particular service you’re using (Google docs vs Google Analytics vs Google Maps etc). The service ode you’ve specified (lh2) is specific to Google Picasa.
Unfortunately there doesn’t seem to be an equivalent code for OpenId (at least, not that I could find!)
The page that you get back from Google should contain a login form. If you look at that, it should be possible to construct a curl invocation to log in; that should then redirect you back to SO (or whichever openID page you want to log in to) logged in.
It turns out that doing this is a little tricky, because you have to parse out some of the the form fields to submit them back to Google, and because Google doesn’t send back a straight HTTP redirect, but an HTML doc with a
<meta http-equiv="redirect" ...>tag. And of course you have to enable cookies. But this is all possible in a script using curl – the following works for me:(Note that I seem to have a slightly different version of curl from you, so I had to change the
-woptions slightly; I’m guessing my version will work for you but you may need to tweak it.)