There is a project on which authorization in Google services occurs through a redirect link that the application generates and allows you to log in to further work with Google services. Does anyone have a sample code in java, how to implement access to their services without a browser. I found the only link that answers my question - http://open-thoughts.net/article/read/11 , but there are some moments that are not clarified by this appeal to you. The example code for authorization is an example for .NET only.

UPD Now I use this method for authorization

private static Credential authorize() throws Exception { // load client secrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(OAuth2Sample.class.getResourceAsStream("/res/client_secret.json"))); if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) { System.out.println("Enter Client ID and Secret from https://code.google.com/apis/console/ " + "into oauth2-cmdline-sample/src/main/resources/client_secrets.json"); System.exit(1); } // set up authorization code flow GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, JSON_FACTORY, clientSecrets, SCOPES).setAccessType("offline").setApprovalPrompt("force").build(); // authorize return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("my@email.com"); } 

But when doing it, a redirect to the browser still happens, where permission for the application is displayed, how to get rid of the browser to solve this problem?

  • Isn't that what you need? developers.google.com/identity/protocols/… - Nofate
  • Apparently, but there is no more clarity from this, compared to the example that he indicated above, nothing new, unfortunately, did not see - abbath0767

1 answer 1

The first time you need to get your hands through the browser. Then you need to get from credential : CLIENT_ID , CLIENT_SECRET , REFRESH_TOKEN . Then we go in and everything:

 final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE); HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); Credential credential = new GoogleCredential.Builder() .setClientSecrets(CLIENT_ID, CLIENT_SECRET) .setJsonFactory(JSON_FACTORY).setTransport(HTTP_TRANSPORT).build() .setRefreshToken(REFRESH_TOKEN); credential.refreshToken(); return new Drive.Builder( HTTP_TRANSPORT, JSON_FACTORY, credential) .setApplicationName(APPLICATION_NAME) .build();