0

我正在使用谷歌照片 Java API 将文件上传到谷歌照片。我从这里这里浏览了多个版本的示例代码,但没有一个完全奏效。我当前的问题是当我尝试初始化 PhotosLibraryClient 时,它给出了 java.lang.IncompatibleClassChangeError:

这是使用的代码:

package photosAPI;

import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.photos.library.v1.PhotosLibraryClient;
import com.google.photos.library.v1.PhotosLibrarySettings;
import com.google.photos.library.v1.proto.BatchCreateMediaItemsResponse;
import com.google.photos.library.v1.proto.MediaItem;
import com.google.photos.library.v1.proto.NewMediaItem;
import com.google.photos.library.v1.proto.NewMediaItemResult;
import com.google.photos.library.v1.upload.UploadMediaItemRequest;
import com.google.photos.library.v1.upload.UploadMediaItemResponse;
import com.google.photos.library.v1.util.NewMediaItemFactory;
import com.google.rpc.Code;
import com.google.rpc.Status;

import java.io.*;
import java.util.List;

public class Photos {

    private PhotosLibraryClient photosLibraryClient;

    public Photos() throws IOException {
        InputStream in = Photos.class.getResourceAsStream("/GPFS-c29aa6fb95a3.json");
        GoogleCredentials credentials = GoogleCredentials.fromStream(in);
        PhotosLibrarySettings settings = PhotosLibrarySettings.newBuilder()
                .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
                .build();

        photosLibraryClient = PhotosLibraryClient.initialize(settings);
    }

    /**
     *
     * @param f The image to upload
     * @return The media item
     */
    public NewMediaItem uploadImage(File f) {
        UploadMediaItemRequest uploadRequest = null;
        try {
            uploadRequest = UploadMediaItemRequest.newBuilder()
                    .setFileName(f.getName())
                    .setDataFile(new RandomAccessFile(f, "r"))
                    .build();

            UploadMediaItemResponse uploadResponse = photosLibraryClient.uploadMediaItem(uploadRequest);

            if (uploadResponse.getError().isPresent()) {
                UploadMediaItemResponse.Error error = uploadResponse.getError().get();

                System.out.println(error.toString());
            } else {
                String uploadToken = uploadResponse.getUploadToken().get();

                return NewMediaItemFactory.createNewMediaItem(uploadToken);
            }
        } catch (FileNotFoundException e) {
            System.out.println("File Not Found!");
            e.printStackTrace();
        }

        return null;
    }

    public void processUploads(List<NewMediaItem> newItems) {
        BatchCreateMediaItemsResponse response = photosLibraryClient.batchCreateMediaItems(newItems);

        for (NewMediaItemResult itemResponse : response.getNewMediaItemResultsList()) {
            Status status = itemResponse.getStatus();
            if (status.getCode() == Code.OK_VALUE) {
                MediaItem createdItem = itemResponse.getMediaItem();

                System.out.println("Successfully processed: " + createdItem.getFilename());
            } else {
                System.out.println("Error!");
            }
        }
    }
}

和错误:

Exception in thread "main" java.lang.IncompatibleClassChangeError: Implementing class
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.google.api.gax.grpc.GrpcCallContext.withCredentials(GrpcCallContext.java:126)
    at com.google.api.gax.grpc.GrpcCallContext.withCredentials(GrpcCallContext.java:64)
    at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:159)
    at com.google.photos.library.v1.internal.stub.GrpcPhotosLibraryStub.create(GrpcPhotosLibraryStub.java:247)
    at com.google.photos.library.v1.internal.stub.PhotosLibraryStubSettings.createStub(PhotosLibraryStubSettings.java:241)
    at com.google.photos.library.v1.internal.InternalPhotosLibraryClient.<init>(InternalPhotosLibraryClient.java:175)
    at com.google.photos.library.v1.PhotosLibraryClient.<init>(PhotosLibraryClient.java:101)
    at com.google.photos.library.v1.PhotosLibraryClient.initialize(PhotosLibraryClient.java:114)
    at photosAPI.Photos.<init>(Photos.java:36)
    at Main.encode(Main.java:226)
    at Main.main(Main.java:36)

编辑:这是我正在使用的所有 gradle 库:

compile 'io.grpc:grpc-netty-shaded:1.19.0'
compile 'io.grpc:grpc-protobuf:1.19.0'
compile 'io.grpc:grpc-stub:1.19.0'
compile group: 'net.lingala.zip4j', name: 'zip4j', version: '1.3.2'
compile 'com.google.photos.library:google-photos-library-client:1.1.0'
4

0 回答 0