【问题标题】:Access Token and Refresh token are null in google drive api with service account访问令牌和刷新令牌在具有服务帐户的谷歌驱动器 API 中为空
【发布时间】:2017-09-05 01:04:59
【问题描述】:

当我运行我的代码时,我将访问令牌和刷新令牌设为空。没有编译错误。我不明白为什么会这样。请帮忙。

我正在从官方网站提供的示例中遵循这个示例。

实际上,我想做服务器到服务器的授权,这可以使用谷歌驱动器上的服务帐户来完成。我什至不想向用户显示同意屏幕。我正在关注位于

的谷歌驱动器文档

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

我的代码是:

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();
    }

}

}

【问题讨论】:

    标签: java google-app-engine google-drive-api service-accounts


    【解决方案1】:

    你可以参考这个thread,它建议使用credential.refreshToken();

    我们需要致电credential.refreshToken();
    然后
    String token=credential.getAccessToken();

    参考资料:

    【讨论】:

    • 您的链接对我有所帮助。谢谢。还需要知道一件事,你能告诉我应该从哪里获取 serviceAccountEmailAddress 和 serviceAccountUser
    猜你喜欢
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 2014-03-31
    • 2021-01-29
    • 2018-09-14
    • 1970-01-01
    相关资源
    最近更新 更多