有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Box Java sdk API返回错误代码404未找到

我需要创建一个实用程序,从box文件夹下载文件。但是我无法让它工作:

    package com.box.sdk.example;

import com.box.sdk.BoxConfig;
import com.box.sdk.BoxDeveloperEditionAPIConnection;
import com.box.sdk.BoxFile;
import com.box.sdk.BoxFolder;
import com.box.sdk.BoxItem;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class PlayGround {

    public static void main(String[] args) {
        Path configPath = Paths.get("config.json");
        Path currentDir = Paths.get("").toAbsolutePath();
        try (BufferedReader reader = Files.newBufferedReader(configPath, Charset.forName("UTF-8"))) {
            BoxConfig boxConfig = BoxConfig.readFrom(reader);
            BoxDeveloperEditionAPIConnection client = BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(boxConfig);
            String folderId = "125601757844";
            BoxFolder folder = new BoxFolder(client, folderId);
            String folderName = folder.getInfo().getName();
            Path localFolderPath = currentDir.resolve(Paths.get(folderName));
            if (!Files.exists(localFolderPath)) {
                localFolderPath = Files.createDirectory(localFolderPath);
            } else {
                localFolderPath = resetLocalFolder(localFolderPath);
            }

            for (BoxItem.Info itemInfo : folder) {
                if (itemInfo instanceof BoxFile.Info) {
                    BoxFile.Info fileInfo = (BoxFile.Info) itemInfo;
                    BoxFile file = new BoxFile(client, fileInfo.getID());
                    String localFilePath = localFolderPath.resolve(Paths.get(fileInfo.getName())).toAbsolutePath()
                            .toString();
                    FileOutputStream stream = new FileOutputStream(localFilePath);
                    file.download(stream);
                    stream.close();
                }
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    static Path resetLocalFolder(Path localFolderPath) throws IOException {
        Files.list(localFolderPath).forEach(file -> {
            System.out.println(file.getFileName());
            try {
                Files.delete(file.toAbsolutePath());
            } catch (IOException e) {
            }
        });
        Files.delete(localFolderPath);
        localFolderPath = Files.createDirectory(localFolderPath);
        return localFolderPath;
    }
}

运行此代码时,会出现以下异常:

   Exception in thread "main" com.box.sdk.BoxAPIResponseException: The API returned an error code [404 | lsgp4zgkfg6qipxg.0d094ed7daa5f78921603840e0fa470e1] not_found - Not Found
    at com.box.sdk.BoxAPIResponse.<init>(BoxAPIResponse.java:92)
    at com.box.sdk.BoxJSONResponse.<init>(BoxJSONResponse.java:32)
    at com.box.sdk.BoxAPIRequest.trySend(BoxAPIRequest.java:680)
    at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:382)
    at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:349)
    at com.box.sdk.BoxFolder.getInfo(BoxFolder.java:289)
    at com.box.sdk.example.PlayGround.main(PlayGround.java:26)

注意:我可以使用开发者令牌运行上面的代码,持续1小时,但是我不能在这样的易失性代码上构建我的生产应用程序

    BoxAPIConnection client = new BoxAPIConnection("3i8b5sPnxUotd5etDuUkzGjXXzBphty9");

共 (0) 个答案