Search code examples
javaandroidgoogle-apigoogle-oauth

java.awt.Desktop class


I am using a Google API for android. Since Google API/G Suite Quickstart for android refers to their java examples, I am trying to implement this:

GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(
                    ReadMail.HTTP_TRANSPORT, ReadMail.JSON_FACTORY, clientSecrets, ReadMail.SCOPES)
                    .setDataStoreFactory(ReadMail.DATA_STORE_FACTORY)
                    .setAccessType("offline")
                    .build();


    AuthorizationCodeInstalledApp authCodeInstalledApp = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver());
    Credential credential = authCodeInstalledApp.authorize("user");

The problem, I am encountering now is that Android just support a subset of JDK classes. Therefore java.awt.Desktop isn't supported. But I really need this class, since AuthorizationCodeInstalledApp's authorize() will soon or later call its intern function browse(). This function needs the Desktop class.

Is there a way to get that class for android? Or is there another workaround to authenticate with Google?


Solution

  • I solved it by myself now. Instead of trying to get the Desktop class from java.awt.Desktop, I just overwrote the on Authorization method:

    AuthorizationCodeInstalledApp ab = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()){
            protected void onAuthorization(AuthorizationCodeRequestUrl authorizationUrl) throws IOException {
                    String url = (authorizationUrl.build());
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    main_Activity.startActivity(browserIntent);
                }
            };
    

    The reason I did this is because authorize() will call onAuthorization() which will call browse(), which checks if Desktop is supported or not. So by rewriting the onAuthorization() method, I won't need that class anymore. My rewritten class will just start a new Browserwindow with that authorization-URL in your android device.

    I hope, I was able to help anyone, who encounteres this problem.