title: Exporting data to Google Spreadsheet from Google App Engine (Java version)
body: <p>I am getting no where now. When I Google around to find a sample of "Exporting data to Google Spreadsheet from Google App Engine", I see a lot of Google Conversion API, Google Conversion API, Google Spreadsheet API and Google Docs API tutorials. But then they all deprecated from Google when I check at the Google site??? So, what is the most updated right now so I can make use of it?</p>
  
  <p>=====</p>
  
  <p>Okay now I use the Google Drive SDK via OAuth2 to create text file. But I have problem with this:</p>
  
  <p>this is the error:</p>
  
  <blockquote>
    <p>java.lang.NullPointerException<br>    at
    java.net.URI$Parser.parse(URI.java:3004)<br>  at
    java.net.URI.(URI.java:577)<br>     at
    com.google.api.client.http.GenericUrl.(GenericUrl.java:100)<br>     at
    com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:269)<br>
      at
    com.google.api.services.drive.Drive$Files$Insert.executeUnparsed(Drive.java:309)<br>
      at
    com.google.api.services.drive.Drive$Files$Insert.execute(Drive.java:331)<br>
      <strong>at
    com.company.dashboard.service.DriveService.initDoc(DriveService.java:84)</strong></p>
  </blockquote>
  
  <p>this is the code:</p>
  
  <pre><code>private GoogleCredential buildGoogleCredential(Credential credential) {
      try {
          logger.warning(oauth2Service.getClientCredential().toString());
          GoogleCredential googleCredential = new GoogleCredential.Builder()
                                                  .setClientSecrets(oauth2Service.getClientCredential())
                                                  .setTransport(new NetHttpTransport())
                                                  .setJsonFactory(new JacksonFactory()).build();
  
          googleCredential.setAccessToken(credential.getAccessToken());
          googleCredential.setRefreshToken(credential.getRefreshToken());
  
          return googleCredential;
  
      } catch (IOException e) {
          e.printStackTrace();
      }
  
      return null;
  }
  
  private Drive buildService(GoogleCredential credential) {
  
      return new Drive.Builder(new NetHttpTransport(), new JacksonFactory(), credential).build();
  }
  
  public void initDoc(HttpServletRequest req) 
          throws Exception {
      User user = UserServiceFactory.getUserService().getCurrentUser();
  
      Credential credential = oauth2Service.getStoredCredential(
              user.getUserId(), 
              (CredentialStore)req.getSession().getServletContext().getAttribute(OAuth2Constant.GOOG_CREDENTIAL_STORE));
  
      if (credential != null) {
          logger.warning("Using access token: " + credential.getAccessToken());
  
          try {
              GoogleCredential googleCredential = buildGoogleCredential(credential);
  
              Drive service = buildService(googleCredential);
  
              if (service == null) {
                  logger.warning("very bad!");
              }
  
              File body = new File();
              body.setTitle("My document");
              body.setDescription("A test document");
              body.setMimeType("text/plain");
  
              java.io.File fileContent = new java.io.File("document.txt");
              FileContent mediaContent = new FileContent("text/plain", fileContent);
  
              service.files().insert(body, mediaContent).execute();
              //File file = service.files().insert(body, mediaContent).execute();
              //System.out.println("File ID: " + file.getId());
  
          } catch (HttpResponseException e) {
            if (e.getStatusCode() == 401) {
                logger.warning(e.getMessage());
              // Credentials have been revoked.
              // TODO: Redirect the user to the authorization URL.
              throw new UnsupportedOperationException();
            }
          } catch (IOException e) {
            System.out.println("An error occurred: " + e);
          }
      }
  </code></pre>
  
  <p>oauth2Service.getClientCredential() returns (xxx = client id and client secret code, not shown in here)</p>
  
  <blockquote>
    <p>{"web":{"client_id":"xxx.apps.googleusercontent.com","client_secret":"xxx"}}</p>
  </blockquote>
  
  <p>This is my scope:</p>
  
  <pre><code>final static List&lt;String&gt; SCOPES = Arrays.asList("https://www.googleapis.com/auth/userinfo.profile", 
                                                   "https://www.googleapis.com/auth/userinfo.email",
                                                   "https://www.googleapis.com/auth/drive",
                                                   "https://www.googleapis.com/auth/docs",
                                                   "https://www.googleapis.com/auth/drive.file");
  final static String AUTH_RESOURCE_LOC = "/client_secrets.json";
  final static String OATH_CALLBACK = "http://localhost:8888/oauth2callback";
  </code></pre>
  
  <p>This line </p>
  
  <blockquote>
    <p>service.files().insert(body, mediaContent).execute();</p>
  </blockquote>
  
  <p>throws NullPointerException. Any idea what had gone wrong???</p>
  
  <p>P/S: Credential = com.google.api.client.auth.oauth2.Credential. I have my OAuth2 everything works good. I can retrieve user info without problem but not the Drive API. service is not null as you see I put it the log as "very bad" and it is not shown. No 401 exception is thrown means my Oauth2 is good with the scopes.</p>
  
  <p>=======</p>
  
  <h2>DARN!!!! Finally solved the issue!!! My code was totally correct! Just I enabled the wrong API! It should be Drive API instead of Drive SDK API :/</h2>