title: Google Drive/OAuth - Can't figure out how to get re-usable GoogleCredentials
body: <p>I've successfully installed and run the <a href="https://developers.google.com/drive/quickstart">Google Drive Quick Start application called DriveCommandLine</a>. I've also adapted it a little to GET file info for one of the files in my Drive account.</p>
  
  <p>What I would like to do now is save the credentials somehow and re-use them without the user having to visit a web page each time to get an authorization code. I have checked out <a href="https://developers.google.com/accounts/docs/OAuth2">this page</a> with instructions to Retrieve and Use OAuth 2.0 credentials. In order to use the example class (MyClass), I have modified the line in DriveCommandLine where the Credential object is instantiated:</p>
  
  <pre><code>Credential credential = MyClass.getCredentials(code, "");
  </code></pre>
  
  <p>This results in the following exception being thrown:</p>
  
  <pre><code>java.lang.NullPointerException
      at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:187)
      at com.google.api.client.json.jackson.JacksonFactory.createJsonParser(JacksonFactory.java:84)
      at com.google.api.client.json.JsonFactory.fromInputStream(JsonFactory.java:247)
      at com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.load(GoogleClientSecrets.java:168)
      at googledrive.MyClass.getFlow(MyClass.java:145)
      at googledrive.MyClass.exchangeCode(MyClass.java:166)
      at googledrive.MyClass.getCredentials(MyClass.java:239)
      at googledrive.DriveCommandLine.&lt;init&gt;(DriveCommandLine.java:56)
      at googledrive.DriveCommandLine.main(DriveCommandLine.java:115)
  </code></pre>
  
  <p>I've been looking at these APIs (Google Drive and OAuth) for 2 days now and have made very little progress. I'd really appreciate some help with the above error and the problem of getting persistent credentials in general. </p>
  
  <p>This whole structure seems unnecessarily complicated to me. Anybody care to explain why I can't just create a simple Credential object by passing in my Google username and password?</p>
  
  <p>Thanks,
  Brian O Carroll, Dublin, Ireland</p>
  
  <p><strong>* Update *</strong></p>
  
  <p>Ok, I've just gotten around the above error and now I have a new one.</p>
  
  <p>The way I got around the first problem was by modifying MyClass.getFlow(). Instead of creating a GoogleClientServices object from a json file, I have used a different version of GoogleAuthorizationCodeFlow.Builder that allows you to enter the client ID and client secret directly as Strings:</p>
  
  <pre><code>flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, "&lt;MY CLIENT ID&gt;", "&lt;MY CLIENT SECRET&gt;", SCOPES).setAccessType("offline").setApprovalPrompt("force").build();
  </code></pre>
  
  <p>The problem I have now is that I get the following error when I try to use flow (GoogleAuthorizationCodeFlow object) to exchange the authorization code for the Credentials object:</p>
  
  <pre><code>An error occurred: com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
  {
    "error" : "invalid_scope"
  }
  googledrive.MyClass$CodeExchangeException
          at googledrive.MyClass.exchangeCode(MyClass.java:185)
          at googledrive.MyClass.getCredentials(MyClass.java:262)
          at googledrive.DriveCommandLine.&lt;init&gt;(DriveCommandLine.java:56)
          at googledrive.DriveCommandLine.main(DriveCommandLine.java:115)
  </code></pre>
  
  <p>Is there some other scope I should be using for this? I am currently using the array of scopes provided with MyClass:</p>
  
  <pre><code>private static final List&lt;String&gt; SCOPES = Arrays.asList(
          "https://www.googleapis.com/auth/drive.file",
          "https://www.googleapis.com/auth/userinfo.email",
          "https://www.googleapis.com/auth/userinfo.profile");
  </code></pre>
  
  <p>Thanks!</p>