public void login( String user, String passwd ) { try { List<NameValuePair> queryParams = new ArrayList<NameValuePair>(); queryParams.add( new BasicNameValuePair("service", "bookmarks") ); queryParams.add( new BasicNameValuePair("passive", "true") ); queryParams.add( new BasicNameValuePair("nui", "1") ); queryParams.add( new BasicNameValuePair("continue", "https://www.google.com/bookmarks/l") ); queryParams.add( new BasicNameValuePair("followup", "https://www.google.com/bookmarks/l") ); HttpGet get = new HttpGet( "https://www.google.com/accounts/ServiceLogin?" + URLEncodedUtils.format(queryParams, "UTF-8") ); HttpResponse resp = http.execute(get, this.ctx); // this just gets the cookie but I can ignore it... if ( resp.getStatusLine().getStatusCode() != 200 ) throw new RuntimeException( "Invalid status code for ServiceLogin " + resp.getStatusLine().getStatusCode() ); resp.getEntity().consumeContent(); String galx = null; for ( Cookie c : cookieStore.getCookies() ) if ( c.getName().equals( "GALX" ) ) galx = c.getValue(); if ( galx == null ) throw new RuntimeException( "GALX cookie not found!" ); HttpPost loginMethod = new HttpPost("https://www.google.com/accounts/ServiceLoginAuth"); // post parameters: List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("Email", user)); nvps.add(new BasicNameValuePair("Passwd", passwd)); nvps.add(new BasicNameValuePair("PersistentCookie", "yes")); nvps.add(new BasicNameValuePair("GALX", galx)); nvps.add(new BasicNameValuePair("continue", "https://www.google.com/bookmarks/l")); loginMethod.setEntity(new UrlEncodedFormEntity(nvps)); resp = http.execute( loginMethod, this.ctx ); if ( resp.getStatusLine().getStatusCode() != 302 ) throw new RuntimeException( "Unexpected status code for ServiceLoginAuth" + resp.getStatusLine().getStatusCode() ); resp.getEntity().consumeContent(); Header checkCookieLocation = resp.getFirstHeader("Location"); if ( checkCookieLocation == null ) throw new RuntimeException("Missing checkCookie redirect location!"); // CheckCookie: get = new HttpGet( checkCookieLocation.getValue() ); resp = http.execute( get, this.ctx ); if ( resp.getStatusLine().getStatusCode() != 302 ) throw new RuntimeException( "Unexpected status code for CheckCookie" + resp.getStatusLine().getStatusCode() ); resp.getEntity().consumeContent(); this.authInitialized = true; Log.i(TAG, "Final redirect location: " + resp.getFirstHeader("Location").getValue() ); Log.i(TAG, "Logged in."); } catch ( IOException ex ) { Log.e(TAG, "Error during login", ex ); throw new RuntimeException("IOException during login", ex); } }
#include <QtNetwork/QNetworkAccessManager> #include <QtNetwork/QNetworkReply> #include <QUrl> #include <QHttp> #include <QNetworkCookie> #include <QNetworkCookieJar> [...] private: QString galx; QNetworkAccessManager* nManLoginStep_1; QNetworkAccessManager* nManLoginStep_2; QNetworkAccessManager* nManLoginStep_3; QNetworkCookieJar* CookieJar;
galx = ""; nManLoginStep_1 = new QNetworkAccessManager(this); nManLoginStep_2 = new QNetworkAccessManager(this); nManLoginStep_3 = new QNetworkAccessManager(this); CookieJar = new QNetworkCookieJar(this); nManLoginStep_1->setCookieJar(CookieJar); nManLoginStep_2->setCookieJar(CookieJar); nManLoginStep_3->setCookieJar(CookieJar); connect(nManLoginStep_1, SIGNAL(finished(QNetworkReply*)), this, SLOT(LoginStep_1_cookie(QNetworkReply*))); connect(nManLoginStep_2, SIGNAL(finished(QNetworkReply*)), this, SLOT(LoginStep_2_ServiceLoginAuth(QNetworkReply*))); connect(nManLoginStep_3, SIGNAL(finished(QNetworkReply*)), this, SLOT(LoginStep_3_check(QNetworkReply*)));
void Application::Login() { QUrl url("https://accounts.google.com/ServiceLogin"); url.addQueryItem("service","bookmarks"); url.addQueryItem("passive", "true"); url.addQueryItem("nui", "1"); url.addQueryItem("continue", "https://www.google.com/bookmarks/l"); url.addQueryItem("followup", "https://www.google.com/bookmarks/l"); nManLoginStep_1->get(QNetworkRequest(url)); } void Application::LoginStep_1_cookie(QNetworkReply *r) { QByteArray data = r->readAll(); QString str(data); //here I'm able to check the content of the website QUrl url1("https://accounts.google.com/ServiceLogin"); QList<QNetworkCookie> cookies = CookieJar->cookiesForUrl(url1); foreach (QNetworkCookie c, cookies) { QByteArray a(c.name()); QString s(a); if (s.toStdString() == "GALX" ) {galx = c.value();} } if (galx.trimmed() == "") { std::cout << "ERROR: No GALX cookie found!\n"; } //-------------------------------------------------------------- QUrl url("https://www.google.com/accounts/ServiceLoginAuth"); QUrl postData; postData.addQueryItem("Email","EMAIL@EMAIL.COM"); postData.addQueryItem("Passwd", "PASSWORD"); postData.addQueryItem("PersistentCookie", "yes"); postData.addQueryItem("GALX", galx); postData.addQueryItem("continue", "https://www.google.com/bookmarks/l"); QNetworkRequest request(url); nManLoginStep_2->post(request, postData.encodedQuery() ); } void Application::LoginStep_2_ServiceLoginAuth(QNetworkReply *r) { QByteArray data = r->readAll(); QString str(data); //here I get a website with the error: "the cookies in your webbrowser are deactivated }