title: Gmail API: Credentials for Gmail API
body: <p>This is code given in the <a href="https://developers.google.com/gmail/api/quickstart/java" rel="nofollow noreferrer">"Java Quickstart"</a> tutorial for Gmail API. This is what I need to do to create a credential for the app:</p>
  
  <pre><code>GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
      httpTransport, jsonFactory, clientSecrets, Arrays.asList(SCOPE))
      .setAccessType("online")
      .setApprovalPrompt("auto").build();
  
  String url = flow.newAuthorizationUrl().setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI)
      .build();
  System.out.println("Please open the following URL in your browser then type"
      + " the authorization code:\n" + url);
  
  // Read code entered by user.
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String code = null;
  try {
     code = br.readLine();
  } catch (IOException e) {
     e.printStackTrace();
  }
  
  // Generate Credential using retrieved code.
  GoogleTokenResponse response = null;
  try {
     response = flow.newTokenRequest(code)
             .setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI).execute();
  } catch (IOException e) {
     e.printStackTrace();
  }
  GoogleCredential credential = new GoogleCredential()
      .setFromTokenResponse(response);
  </code></pre>
  
  <p><br>
  Is there something I can do to automate the above process like it is done here to get the credential for further use?</p>
  
  <p>The below example is for Google Tasks.</p>
  
  <pre><code>GoogleAccountCredential credential =
     GoogleAccountCredential.usingOAuth2(this, Collections.singleton(TasksScopes.TASKS));
  </code></pre>