google / mail-importer

Mail Importer for Gmail will upload the contents of a Thunderbird mail archive to Gmail and do its best to preserve the read state, flagged state, and folders of the messages.
Apache License 2.0
253 stars 91 forks source link

I am getting the error while implementing on Android :java.security.KeyStoreException: JKS not found #8

Open akshay3015 opened 5 years ago

akshay3015 commented 5 years ago

have integrated this code in android . i get error like this

java.security.KeyStoreException: JKS not found
2019-05-31 17:36:01.065 29980-29980/com.netwin.readmails W/System.err:     at java.security.KeyStore.getInstance(KeyStore.java:890)
2019-05-31 17:36:01.066 29980-29980/com.netwin.readmails W/System.err:     at com.google.api.client.util.SecurityUtils.getJavaKeyStore(SecurityUtils.java:53)
2019-05-31 17:36:01.066 29980-29980/com.netwin.readmails W/System.err:     at com.google.api.client.googleapis.GoogleUtils.getCertificateTrustStore(GoogleUtils.java:74)
2019-05-31 17:36:01.066 29980-29980/com.netwin.readmails W/System.err:     at com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport(GoogleNetHttpTransport.java:55)
2019-05-31 17:36:01.066 29980-29980/com.netwin.readmails W/System.err:     at com.netwin.readmails.MainActivity.getEmails(MainActivity.java:90)
2019-05-31 17:36:01.066 29980-29980/com.netwin.readmails W/System.err:     at com.netwin.readmails.MainActivity.onCreate(MainActivity.java:82)
2019-05-31 17:36:01.067 29980-29980/com.netwin.readmails W/System.err:     at android.app.Activity.performCreate(Activity.java:7003)
2019-05-31 17:36:01.067 29980-29980/com.netwin.readmails W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1230)
2019-05-31 17:36:01.067 29980-29980/com.netwin.readmails W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2899)
2019-05-31 17:36:01.067 29980-29980/com.netwin.readmails W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3046)
2019-05-31 17:36:01.067 29980-29980/com.netwin.readmails W/System.err:     at android.app.ActivityThread.-wrap11(Unknown Source:0)
2019-05-31 17:36:01.067 29980-29980/com.netwin.readmails W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1688)
2019-05-31 17:36:01.068 29980-29980/com.netwin.readmails W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:105)
2019-05-31 17:36:01.068 29980-29980/com.netwin.readmails W/System.err:     at android.os.Looper.loop(Looper.java:164)
2019-05-31 17:36:01.068 29980-29980/com.netwin.readmails W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6798)
2019-05-31 17:36:01.068 29980-29980/com.netwin.readmails W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2019-05-31 17:36:01.068 29980-29980/com.netwin.readmails W/System.err:     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
2019-05-31 17:36:01.068 29980-29980/com.netwin.readmails W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
2019-05-31 17:36:01.069 29980-29980/com.netwin.readmails W/System.err: Caused by: java.security.NoSuchAlgorithmException: JKS KeyStore not available
2019-05-31 17:36:01.069 29980-29980/com.netwin.readmails W/System.err:     at sun.security.jca.GetInstance.getInstance(GetInstance.java:159)
2019-05-31 17:36:01.069 29980-29980/com.netwin.readmails W/System.err:     at java.security.Security.getImpl(Security.java:590)
2019-05-31 17:36:01.070 29980-29980/com.netwin.readmails W/System.err:     at java.security.KeyStore.getInstance(KeyStore.java:887)
2019-05-31 17:36:01.070 29980-29980/com.netwin.readmails W/System.err:  ... 17 more

here is my code

import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.GmailScopes;
import com.google.api.services.gmail.model.Label;
import com.google.api.services.gmail.model.ListLabelsResponse;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity{
    private static final String APPLICATION_NAME = "Gmail API Java Quickstart";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static final String TOKENS_DIRECTORY_PATH = "tokens";

    TextView tvEmail;

    /**
     * Global instance of the scopes required by this quickstart.
     * If modifying these scopes, delete your previously saved tokens/ folder.
     */
    private static final List<String> SCOPES = Collections.singletonList(GmailScopes.GMAIL_LABELS);
    private static  String CREDENTIALS_FILE_PATH = "";

    /**
     * Creates an authorized Credential object.
     * @param HTTP_TRANSPORT The network HTTP Transport.
     * @return An authorized Credential object.
     * @throws IOException If the credentials.json file cannot be found.
     */

    private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        // Load client secrets.

        Uri path = Uri.parse("file:///android_asset/credentials.json");

        String newPath = path.toString();
        CREDENTIALS_FILE_PATH = newPath;

        InputStream in = MainActivity.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
        }
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvEmail = findViewById(R.id.tv_emails);
        getEmails();

    }

    private void getEmails(){
        final NetHttpTransport HTTP_TRANSPORT;
        try {
            HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                    .setApplicationName(APPLICATION_NAME)
                    .build();

            // Print the labels in the user's account.
            String user = "me";
            ListLabelsResponse listResponse = service.users().labels().list(user).execute();
            List<Label> labels = listResponse.getLabels();
            if (labels.isEmpty()) {
                System.out.println("No labels found.");
            } else {
                System.out.println("Labels:");
                for (Label label : labels) {
                    System.out.printf("- %s\n", label.getName());
                }
            }
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}