Warning: Creating default object from empty value in /home/agilemed/aendeavors.com/wp-includes/ms-load.php on line 138

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/agilemed/aendeavors.com/wp-includes/ms-load.php:138) in /home/agilemed/aendeavors.com/wp-content/plugins/amazon-product-in-a-post-plugin/amazon-product-in-a-post.php on line 102

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/agilemed/aendeavors.com/wp-includes/ms-load.php:138) in /home/agilemed/aendeavors.com/wp-content/plugins/amazon-product-in-a-post-plugin/amazon-product-in-a-post.php on line 102
Android Programming: Google Service/App Authentication » Agile Tech

«

»

Dec
23

Android Programming: Google Service/App Authentication

While many of the Google applications already have Android-based ports, some of the more “specialty apps” such as AdSense don’t have a mobile interface. Luckily, the Google API allows for an easy login method using an HTTP Post request. One of our current applications, SimpleAdMonitor, uses this method to download an AdSense user’s daily and all time totals.

While the solution is fairly easy to implement, the actual POST request appears to run quite slowly, at least over the Android emulator and somewhat on the device as well.

The first step to generating an authorized session is to post the user’s login details to the Google ServiceLogin:

   1: String login = "https://www.google.com/accounts/ServiceLoginAuth?service=adsense&hl=en-US&ltmpl=login&ifr=true&passive=true&rm=hide&nui=3&alwf=true&continue=https%3A%2F%2Fwww.google.com%2Fadsense%2Fgaiaauth&followup=https%3A%2F%2Fwww.google.com%2Fadsense%2Fgaiaauth";
   2: DefaultHttpClient client = new DefaultHttpClient();
   3: String websiteData = null;
   4: URI uri = new URI(login);
   5: HttpPost method = new HttpPost(uri);
   6: method.addHeader("User-Agent", "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.10) Gecko/20071115 Firefox/2.0.0.10");
   7: method.addHeader("Pragma", "no-cache");
   8: method.addHeader("Content-Type", "application/x-www-form-urlencoded");

On line 1, the address specifies a Google service to authorize against. This service can be changed if you need to access some other application. Once this request is formed, the user’s login information can be added using a NameValuePair list. These are then set on the HttpPost method using setEntity:

   1: List <NameValuePair> loginInfo = new ArrayList <NameValuePair>();
   2: loginInfo.add(new BasicNameValuePair("Email", username));
   3: loginInfo.add(new BasicNameValuePair("Passwd", password));
   4: loginInfo.add(new BasicNameValuePair("null", "Sign+in"));
   5: HttpEntity entity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
   6:
   7: method.setEntity(entity);
   8: HttpResponse res = client.execute(method);

One item to note on line 3 is that the param used for the password is “Passwd”. Once this data is posted, the server will return a response that should be evaluated for a correct login. If the login is valid, the page should respond with a link to a redirect page that needs to again be posted to:

   1: String login2 = "https://www.google.com/accounts/CheckCookie?continue=https%3A%2F%2Fwww.google.com%2Fadsense%2Fgaiaauth&followup=https%3A%2F%2Fwww.google.com%2Fadsense%2Fgaiaauth&hl=en-US&service=adsense&ltmpl=login&chtml=LoginDoneHtml";
   2: method.setURI(new URI(login2));
   3: res = client.execute(method);

At this point, the server should respond with the data from the redirect URL, in this case, the main AdSense page. Further requests can now be made to the server using the same method without needing to re-authorize the user. As I mentioned previously, there are some latency issues with this method that I haven’t quite sorted out yet. The lag isn’t horrible, but is fairly significant when waiting for two fairly small pieces of data. Some additional digging will hopefully find out what is causing the delay and how to improve it.

Leave a Reply

Your email address will not be published.


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>