简体   繁体   中英

Access Token and Refresh token are null in google drive api with service account

I am getting access token and refresh token as null when I run my code. There is no compilation error. I am not getting why this is happening. plz help.

I am following this example from samples provided at official site.

Actually, I want to do server to server authorization which can be done using service account at google drive. I even dont want to show consent screen to the user. I am following google drive documentation which is at

https://developers.google.com/identity/protocols/OAuth2ServiceAccount

My Code is :

public class TFT {

private static final String ApplicationName = "Boston";
private static final String serviceAccountEmailAddress = "boss@boston-163913.iam.gserviceaccount.com";
private static final String serviceAccountUser = "abc.pqr@yopmail.com";

private static List<File> retrieveAllFiles(Drive service) throws IOException {
    List<File> result = new ArrayList<>();
    Files.List request = service.files().list();

    do {
        FileList files = request.execute();

        result.addAll(files.getFiles());

        request.setPageToken(files.getNextPageToken());

    } while (request.getPageToken() != null && request.getPageToken().length() > 0);
    return result;
}

public static Drive getDriveService() throws IOException {

    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    GoogleCredential credential = null;

    try {
        credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(jsonFactory)
                .setServiceAccountId(serviceAccountEmailAddress)
                .setServiceAccountScopes(DriveScopes.all())
                .setServiceAccountUser(serviceAccountUser)
                .setServiceAccountPrivateKeyFromP12File(new java.io.File("/home/xpointers/Downloads/Boston-2b55ddf4a5c8.p12"))
                .build();

    } catch (GeneralSecurityException | IOException e) {
        e.printStackTrace();
    }

    Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).setApplicationName(ApplicationName)
            .build();
    return service;
}

private java.io.File getp12File(String fileName) {


    ClassLoader classLoader = getClass().getClassLoader();
    return new java.io.File(classLoader.getResource(fileName).getFile());

}

public static void main(String[] args) {

    try {

        Drive service = getDriveService();

        List<File> files;

        files = retrieveAllFiles(service);

        System.out.println(files);

    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

You may refer with this thread which suggests to use credential.refreshToken(); .

We need to call credential.refreshToken();
and then
String token=credential.getAccessToken();

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM