Friday, January 3, 2014

"com.google.android.gms.auth.GoogleAuthException: Unknown" in GoogleAuthUtils.getToken() call

I was recently developing a simple Android application that would access Google Spreadsheets. To access the spreadsheet, the users would have to authorize the app to access their data. To get that authorization, I will have to use this command:

mToken = GoogleAuthUtil.geToken(MyActivity.this, accountName, "oauth2:https://spreadsheets.google.com/feeds https://docs.google.com/feeds");


The problem was that, I was getting this error when getToken() executed:


Exception in getToken() - GoogleAuthException
com.google.android.gms.auth.GoogleAuthException: Unknown
at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
  at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
  at somepackage.LoginHomeActivity$2.doInBackground(LoginHomeActivity.java:80)
  at somepackage.LoginHomeActivity$2.doInBackground(LoginHomeActivity.java:1)
  at android.os.AsyncTask$2.call(AsyncTask.java:264)
  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
  at java.lang.Thread.run(Thread.java:856)

After a bit of digging around, I realize that the reason for the error is the scope string. Here, I am using this:

oath2:https://spreadsheets.google.com/feeds https://docs.google.com/feeds

It should be this: https://docs.google.com/feeds/ https://docs.googleusercontent.com/ https://spreadsheets.google.com/feeds/

That is, I don't have an option of choosing just a bunch of scopes. I will have to use all of the urls as scope.

Just FYI, I got the scope value for Google Spreadsheet from here 

Thursday, January 2, 2014

Google Spreadsheet API - Java - com.google.gdata.util.ParseException: Unrecognized content type:application/binary

I was developing an application using Google Spreadsheet API.  I was trying to load a particular spreadsheet in my application. This was the code:

URL spreadSheetUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
SpreadsheetQuery query = new SpreadsheetQuery(spreadSheetUrl);
query.setTitleQuery("xyz");
query.setTitleExact(true);
SpreadsheetFeed spreadSheetFeed = service.getFeed(query, SpreadsheetFeed.class);

I was getting this error:

com.google.gdata.util.ParseException: Unrecognized content type:application/binary
com.google.gdata.client.Service.parseResponseData(Service.java:2136)
com.google.gdata.client.Service.parseResponseData(Service.java:2098)
com.google.gdata.client.Service.getFeed(Service.java:1136)
com.google.gdata.client.Service.getFeed(Service.java:1077)
com.google.gdata.client.GoogleService.getFeed(GoogleService.java:676)
com.google.gdata.client.Service.getFeed(Service.java:1034)

Apparently, this is because I did not give user credentials to access the spreadsheet. For some reason, Google does not return a valid response if the user is not logged in. It could return - NotAuthorizedException or something like that. But it keeps returning ParseException.

Wednesday, December 18, 2013

Errors running builder Android Resource Manager error when creating a new Android project in Eclipse

Recently, I had updated my Android SDK Manager but had not updated my Eclipse Android plugins.  This lead to a strange error when I tried to create a new Android project.


Errors occurred during the build.Errors running builder 'Android Resource Manager' on project *.java.lang.NullPointerException


There were many solutions in stackoverflow.
http://stackoverflow.com/questions/20043521/errors-running-builder-android-resource-manager-on-adt
http://stackoverflow.com/questions/18096315/mac-error-create-android-project-errors-running-builder-android-resource-man

But none of these worked for me.

To solve this, I tried updating my Eclipse Android plugins. And immediately, the error was gone and I could create a new project without any errors.


  1. In Eclipse, Help menu -> Check for Updates
  2. Select all Android related plugins (most probably will have the word Android in its name)
  3. Click Next, Accept the agreement page then proceed to install the plugin.

Thursday, October 31, 2013

Great offer from Microsoft for Xbox

I got this offer from Microsoft as a part of Google Adsense when I was browsing through one of the websites.

Someone in Microsoft would have thought.. "An offer for 3 cents.. Who could resist that?"





Thursday, October 3, 2013

Create a Google App Engine project that uses Spring + using Maven to create + Eclipse as IDE


I did not find any tutorial to create a maven project that uses Google App Engine and Spring and use Eclipse IDE.  So, I thought I would document what I had done recently to create a Spring MVC project using Maven and deploys to Google App Engine; using Eclipse as IDE.

I am assuming you have Eclipse plugin for Maven (m2e) installed to Eclipse.

Creation of project:
We need to use maven plugin to create a new Google App Engine project.  I used instructions to create using command line from here.  I am sure we can use Eclipse maven project creation wizard as well.

Adding Spring dependencies:
To add Spring dependencies, add dependencies.


  <!-- Spring Dependencies -->  
           <dependency>   
                <groupId>org.springframework</groupId>   
                <artifactId>spring-core</artifactId>   
                <version>${spring.version}</version>   
           </dependency>   
           <dependency>   
                <groupId>org.springframework</groupId>   
                <artifactId>spring-context</artifactId>   
                <version>${spring.version}</version>   
           </dependency>   
           <dependency>   
                <groupId>org.springframework</groupId>   
                <artifactId>spring-beans</artifactId>   
                <version>${spring.version}</version>   
           </dependency>   
           <!-- Spring MVC framework -->   
           <dependency>   
                <groupId>org.springframework</groupId>   
                <artifactId>spring-webmvc</artifactId>   
                <version>${spring.version}</version>   
           </dependency>   
           <dependency>   
                <groupId>org.springframework</groupId>   
                <artifactId>spring-web</artifactId>   
                <version>${spring.version}</version>   
           </dependency>   


Tips:

  • Till Spring 2.5.6, the framework used to provide a single all-encompassing dependency.  From Spring 3.0.x on wards, that is not the case.  So, we will have to include every dependency in pom.xml file.
  • It is important to have same version for all Spring dependencies.  If Spring dependencies don't have the same version, it gives out an error:

 [INFO] java.lang.NoSuchFieldError: APPLICATION_CONTEXT_ID_PREFIX  
 [INFO] at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:430)  
 [INFO] at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)  
 [INFO] at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)  

  • Dependencies can be checked using this command: mvn dependency:tree
  • When I was iterating through these steps multiple times, I found some old dependencies lingering around. If that happens, this command would clear things up: mvn clean


Create web.xml:
If web.xml has not been created already, create it under ${project}/src/main/webapp/WEB-INF

Add DispatcherServlet to web.xml:
This is a standard Spring stuff you would do in any Spring MVC project.  (I created MVC project, so I did this.  It may or may not done in other cases)  Also, depending on the name given to DispatcherServlet in web.xml, create <dispatcher-servlet-name>-servlet.xml xml file.

Deploying this application now will not work.  Here are some steps I took to get rid of the errors:

  1. First, we need to clean up the maven project.  Right click project -> Run As -> maven clean.
  2. Right click project -> Properties -> Project Facet -> Select "Dynamic Web Project".
  3. Again, Right click project -> Properties -> Deployment Assembly -> Add -> In popup, "Java Build Path Entries" -> Maven Dependencies -> Finish.  All this does is to copy dependency jar to .../WEB-INF/lib.
Hopefully, this helps someone create a project without any errors.
Thanks to these links:

http://stackoverflow.com/a/12600686/1700184
http://stackoverflow.com/a/6083776/1700184
https://developers.google.com/appengine/docs/java/tools/maven#creating_a_new_maven_app_engine_project_using_skeleton-archetype

Tuesday, October 1, 2013

pom.xml for Google App Engine + Spring

I am working on a new project that uses GAE (Google App Engine) and Spring.

I faced a few errors while trying to run the project - all related to incorrect configuration in pom.xml

Some of the problems I faced are:

  • [ERROR] Failed to execute goal net.kindleit:maven-gae-plugin:0.8.0:run (default-cli) on project XYZ: ${gae.home} is not a directory: ${gae.home} is not a directory:[ERROR] Please set the sdkDir configuration in your pom.xml to a valid directory. Make sure you have correctly extracted the app engine sdk.
  • [ERROR] Failed to execute goal net.kindleit:maven-gae-plugin:0.8.0:run (default-cli) on project XYZ: Execution default-cli of goal net.kindleit:maven-gae-plugin:0.8.0:run failed: Plugin net.kindleit:maven-gae-plugin:0.8.0 or one of its dependencies could not be resolved: Could not transfer artifact com.google.appengine:appengine-tools-sdk:jar:1.4.0 from/to central (http://repo.maven.apache.org/maven2): No response received after 60000 -> [Help 1]





 <?xml version="1.0"?>  
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>xx.xx.xx</groupId>  
  <artifactId>XYZ</artifactId>  
  <packaging>war</packaging>  
  <version>0.0.1</version>  
  <name>XYZ Webapp</name>  
  <url>http://maven.apache.org</url>  
       <dependencies>  
      <!-- Spring framework -->  
      <dependency>  
           <groupId>org.springframework</groupId>  
                <artifactId>spring</artifactId>  
                <version>2.5.6</version>  
           </dependency>  
           <dependency>   
                <groupId>org.springframework</groupId>   
                <artifactId>spring-core</artifactId>   
                <version>${spring.version}</version>   
           </dependency>   
           <dependency>   
                <groupId>org.springframework</groupId>   
                <artifactId>spring-context</artifactId>   
                <version>${spring.version}</version>   
           </dependency>   
           <dependency>   
                <groupId>org.springframework</groupId>   
                <artifactId>spring-beans</artifactId>   
                <version>${spring.version}</version>   
           </dependency>   
           <!-- Spring MVC framework -->   
           <dependency>   
                <groupId>org.springframework</groupId>   
                <artifactId>spring-webmvc</artifactId>   
                <version>${spring.version}</version>   
           </dependency>   
           <dependency>   
                <groupId>org.springframework</groupId>   
                <artifactId>spring-web</artifactId>   
                <version>${spring.version}</version>   
           </dependency>   
           <!-- Apache Commons Upload -->   
           <dependency>   
                <groupId>commons-fileupload</groupId>   
                <artifactId>commons-fileupload</artifactId>   
                <version>1.2.2</version>   
           </dependency>   
           <!-- Apache Commons Upload -->   
           <dependency>   
                <groupId>commons-io</groupId>   
                <artifactId>commons-io</artifactId>   
                <version>1.3.2</version>   
           </dependency>   
            <!-- JSTL -->   
           <dependency>   
                <groupId>javax.servlet</groupId>   
                <artifactId>jstl</artifactId>   
                <version>1.1.2</version>   
           </dependency>   
            <dependency>   
                <groupId>taglibs</groupId>   
                <artifactId>standard</artifactId>   
                <version>1.1.2</version>   
           </dependency>  
           <dependency>  
                <groupId>com.google.appengine</groupId>  
                <artifactId>appengine-tools-sdk</artifactId>  
                <version>1.8.0</version>  
           </dependency>  
           <dependency>  
                <groupId>com.google.appengine</groupId>  
                <artifactId>appengine-api-1.0-sdk</artifactId>  
                <version>1.8.0</version>  
           </dependency>  
           <dependency>  
       <groupId>net.kindleit</groupId>  
       <artifactId>gae-runtime</artifactId>  
       <version>${gae}</version>  
       <type>pom</type>  
     </dependency>  
      </dependencies>  
      <build>   
      <finalName>SpringFileUpload</finalName>   
           <plugins>   
                <plugin>   
                     <groupId>org.apache.tomcat.maven</groupId>   
                     <artifactId>tomcat7-maven-plugin</artifactId>   
                     <version>2.1</version>   
                     <configuration>   
                          <url>http://localhost:8080/manager/text</url>   
                          <server>my-tomcat</server>   
                          <path>/SpringFileUpload</path>   
                     </configuration>   
                </plugin>   
                <plugin>   
                     <groupId>org.apache.maven.plugins</groupId>   
                     <artifactId>maven-compiler-plugin</artifactId>   
                     <version>3.0</version>   
                     <configuration>   
                          <source>${jdk.version}</source>   
                          <target>${jdk.version}</target>   
                     </configuration>   
                </plugin>  
                <plugin>  
                     <groupId>net.kindleit</groupId>  
                     <artifactId>maven-gae-plugin</artifactId>  
                     <version>0.8.0</version>  
                     <configuration>  
                          <unpackVersion>1.8.0</unpackVersion>  
                          <gae.home>C:\Users\koushik\.m2\repository\com\google\appengine\appengine-java-sdk\1.8.0\appengine-java-sdk-1.8.0</gae.home>  
                     </configuration>  
                </plugin>  
           </plugins>   
      </build>  
      <profiles>  
        <profile>  
          <id>repositories</id>  
                <repositories>  
                     <repository>  
                          <id>maven-gae-plugin-repo</id>  
                          <name>maven-gae-plugin repository</name>  
                          <url>http://maven-gae-plugin.googlecode.com/svn/repository</url>  
                     </repository>  
                </repositories>  
                <pluginRepositories>  
                     <pluginRepository>  
                          <id>maven-gae-plugin-repo</id>  
                          <name>maven-gae-plugin repository</name>  
                          <url>http://maven-gae-plugin.googlecode.com/svn/repository</url>  
                     </pluginRepository>  
                </pluginRepositories>  
                <properties>  
                     <gae.home>C:\Users\koushik\.m2\repository\com\google\appengine\appengine-java-sdk\1.8.0\appengine-java-sdk-1.8.0</gae.home>  
                </properties>  
        </profile>  
      </profiles>  
      <properties>  
           <spring.version>3.0.5.RELEASE</spring.version>  
           <jdk.version>1.5</jdk.version>  
           <gae.home>C:\Users\koushik\.m2\repository\com\google\appengine\appengine-java-sdk\1.8.0\appengine-java-sdk-1.8.0</gae.home>  
           <gae>1.4.0</gae>  
      </properties>  
 </project>  

Thursday, September 26, 2013

A Quick tip on Data Structure for DFS/BFS

Breadth-First Search and Depth-First Search are two very common graph traversal algorithms for any Graph.

There are two ways of representing graph - Adjacency Matrix and Adjacency Lists.  Well, this link gives more ways of representing a Graph.  But, let us not get into that.

Both DFS and BFS requires us to iterate over the children of a node.  This will be done for almost all nodes.

  • Iterating through all children is O(n) in Adjacency Matrix.
  • It is O(branches) in Adjacency Lists.

In a sparsely connected Graph, O(branches) is much smaller than O(n).  So, if we need to do a DFS, we should always choose Adjacency Lists over Adjacency Matrix.