title: Java to Google Spreadsheet
body: <p>I was trying to do programming using Java to connect to Google Spreadsheet to do data retrieval or modifying data in the cells.</p>
  
  <p>My Google spreadsheet link is <a href="https://docs.google.com/spreadsheets/d/1UXoGD2gowxZ2TY3gooI9y7rwWTPBOA0dnkeNYwUqQRA" rel="nofollow">https://docs.google.com/spreadsheets/d/1UXoGD2gowxZ2TY3gooI9y7rwWTPBOA0dnkeNYwUqQRA</a></p>
  
  <p>I looked at the <a href="https://developers.google.com/google-apps/spreadsheets/" rel="nofollow">Sheets API</a> and it requires link like </p>
  
  <p><code>https://spreadsheets.google.com/feeds/worksheets/key/private/full</code></p>
  
  <p>I have tried different forms of the links, such as:</p>
  
  <ol>
  <li><p><code>https://spreadsheets.google.com/feeds/worksheets/1UXoGD2gowxZ2TY3gooI9y7rwWTPBOA0dnkeNYwUqQRA/private/full</code></p></li>
  <li><p><code>https://spreadsheets.google.com/feeds/worksheets/1UXoGD2gowxZ2TY3gooI9y7rwWTPBOA0dnkeNYwUqQRA/private/full</code></p></li>
  </ol>
  
  <p>They gave me different kinds of errors respectively:</p>
  
  <ol>
  <li><code>com.google.gdata.util.ParseException: Unrecognized content type:application/binary</code></li>
  <li><code>com.google.gdata.util.RedirectRequiredException: Moved Temporarily</code></li>
  </ol>
  
  <p>I have no idea how to connect to the Googl Spreadsheet using Java. Please help me if you have experience on this.</p>
  
  <pre><code>import com.google.gdata.client.authn.oauth.*;
  import com.google.gdata.client.spreadsheet.*;
  import com.google.gdata.data.*;
  import com.google.gdata.data.batch.*;
  import com.google.gdata.data.spreadsheet.*;
  import com.google.gdata.util.*;
  import org.testng.annotations.Test;
  
  import java.io.IOException;
  import java.net.*;
  import java.util.*;
  
  public class TestGoogleSheetsAPI {
  
      @Test
      public void testConnectToSpreadSheet() throws ServiceException, IOException {
          SpreadsheetService service = new SpreadsheetService("google-spreadsheet");
  
          URL SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/worksheets/1UXoGD2gowxZ2TY3gooI9y7rwWTPBOA0dnkeNYwUqQRA/public/full");
          SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
          List&lt;SpreadsheetEntry&gt; spreadsheets = feed.getEntries();
  
          if (spreadsheets.size() == 0) {
              // TODO: There were no spreadsheets, act accordingly.
          }
  
          SpreadsheetEntry spreadsheet = spreadsheets.get(0);
          System.out.println(spreadsheet.getTitle().getPlainText());
      }
  }
  </code></pre>
  
  <p>I didn't use <code>service.setUserCredentials("xxx@gmail", "password")</code> because I will have another error, which is <code>com.google.gdata.util.AuthenticationException: Error authenticating (check service name)</code></p>