【问题标题】:The API package 'memcache' or call 'Get()' was not found未找到 API 包“memcache”或调用“Get()”
【发布时间】:2014-01-20 16:06:49
【问题描述】:

我正在研究可在以下站点找到的 Google Drive API 示例:Using Service AccountsInserting a FileSetting Permissions。正如您可能猜到的,我刚刚开始使用 Google API。因此,我确信答案就在那里,但我对这个主题的了解还不够广泛,无法解释这些发现。

无论如何,使用 Google 的示例代码,我已经为我的服务帐户成功连接到云端硬盘。但是,我不能插入文件。我收到以下错误:

com.google.apphosting.api.ApiProxy$CallNotFoundException:未找到 API 包“memcache”或调用“Get()”。 在 com.google.apphosting.api.ApiProxy$1.get(ApiProxy.java:162) 在 com.google.apphosting.api.ApiProxy$1.get(ApiProxy.java:160) 在 com.google.appengine.api.utils.FutureWrapper.get(FutureWrapper.java:86) 在 com.google.appengine.api.memcache.MemcacheServiceImpl.quietGet(MemcacheServiceImpl.java:26) 在 com.google.appengine.api.memcache.MemcacheServiceImpl.get(MemcacheServiceImpl.java:49) 在 com.google.appengine.api.appidentity.AppIdentityServiceImpl.getAccessToken(AppIdentityServiceImpl.java:188) 在 com.google.api.client.googleapis.extensions.appengine.auth.oauth2.AppIdentityCredential.intercept(AppIdentityCredential.java:93) 在 com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859) 在 com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequestWithoutGZip(MediaHttpUploader.java:545) 在 com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequest(MediaHttpUploader.java:562) 在 com.google.api.client.googleapis.media.MediaHttpUploader.executeUploadInitiation(MediaHttpUploader.java:519) 在 com.google.api.client.googleapis.media.MediaHttpUploader.resumableUpload(MediaHttpUploader.java:384) 在 com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:336) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:418) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460) 在 DriveTest.GDrive.insertFile(GDrive.java:80) 在 DriveTest.GDrive.putFile(GDrive.java:58) 在 DriveTest.App.main(App.java:28) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) 未找到 API 包“memcache”或调用“Get()”。

代码几乎是从我上面引用的网站中剪切和粘贴的:

列出的第一行是导致错误的行。

File file = service.files().insert(body, mediaContent).execute();
package DriveTest;
import com.google.api.client.googleapis.extensions.appengine.auth.oauth2.AppIdentityCredential;
import com.google.api.client.googleapis.services.CommonGoogleClientRequestInitializer;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.ParentReference;
import com.google.api.services.drive.model.Permission;

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;



    public class GDrive {

        private static GDrive instance=null;
        private static Drive drive=null;
        private static final String API_KEY = "ourkey";

        protected GDrive() {}

        public static GDrive getInstance() {
            if(instance==null) {
                instance=new GDrive();

            }
            return instance;
        }

        public void setDriveService() throws GeneralSecurityException,IOException, URISyntaxException {
            if(drive==null) {
                HttpTransport httpTransport = new NetHttpTransport();
                JsonFactory jsonFactory = new JacksonFactory();
                ArrayList<String> scopes=new ArrayList<String>();
                scopes.add(DriveScopes.DRIVE);
                AppIdentityCredential credential = new AppIdentityCredential.Builder(scopes).build();
                GoogleClientRequestInitializer keyInitializer = new CommonGoogleClientRequestInitializer(API_KEY);
                drive = new Drive.Builder(httpTransport, jsonFactory, null)
                        .setHttpRequestInitializer(credential)
                        .setGoogleClientRequestInitializer(keyInitializer)
                        .build();
            }
        }

        public void putFile(String filename) throws Exception {
            File theFile=this.insertFile(this.drive, "Report", "a report!","","application/vnd.ms-excel",filename);
            Permission thePermission=this.setShare(this.drive, theFile.getId(),"someuser@somedomain.com","user","reader");
        }

        private File insertFile(Drive service, String title, String description,
                                       String parentId, String mimeType, String filename) {
            // File's metadata.
            File body = new File();
            body.setTitle(title);
            body.setDescription(description);
            body.setMimeType(mimeType);

            // Set the parent folder.
            if (parentId != null && parentId.length() > 0) {
                body.setParents(
                        Arrays.asList(new ParentReference().setId(parentId)));
            }

            // File's content.
            java.io.File fileContent = new java.io.File(filename);
            FileContent mediaContent = new FileContent(mimeType, fileContent);
            try {
                File file = service.files().insert(body, mediaContent).execute();

                // Uncomment the following line to print the File ID.
                System.out.println("File ID: %s" + file.getId());

                return file;
            } catch (IOException e) {
                System.out.println("An error occured: " + e);
                return null;
            }
        }

        /**
         * Insert a new permission.
         *
         * @param service Drive API service instance.
         * @param fileId ID of the file to insert permission for.
         * @param value User or group e-mail address, domain name or {@code null}
        "default" type.
         * @param type The value "user", "group", "domain" or "default".
         * @param role The value "owner", "writer" or "reader".
         * @return The inserted permission if successful, {@code null} otherwise.
         */
        private Permission setShare(Drive service, String fileId,
                                    String value, String type, String role) throws Exception {

            Permission newPermission = new Permission();

            newPermission.setValue(value);
            newPermission.setType(type);
            newPermission.setRole(role);
            try {
                return service.permissions().insert(fileId, newPermission).execute();
            } catch (IOException e) {
                System.out.println("An error occurred: " + e);
            }
            return newPermission;
        }

    }

最后,我使用 Maven 来处理依赖关系:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>DriveTest</groupId>
  <artifactId>DriveTest</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>DriveTest</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
      <dependency>
          <groupId>com.google.api-client</groupId>
          <artifactId>google-api-client</artifactId>
          <version>1.17.0-rc</version>
      </dependency>
      <dependency>
          <groupId>com.google.api-client</groupId>
          <artifactId>google-api-client-appengine</artifactId>
          <version>1.17.0-rc</version>
      </dependency>
      <dependency>
          <groupId>com.google.apis</groupId>
          <artifactId>google-api-services-drive</artifactId>
          <version>v2-rev105-1.17.0-rc</version>
      </dependency>
      <dependency>
          <groupId>com.google.http-client</groupId>
          <artifactId>google-http-client-jackson</artifactId>
          <version>1.17.0-rc</version>
      </dependency>
      <dependency>
          <groupId>com.google.appengine</groupId>
          <artifactId>appengine-api-1.0-sdk</artifactId>
          <version>1.8.1</version>
      </dependency>
  </dependencies>
</project>

我确定我错过了一些东西,但不确定那是什么。

谢谢。

【问题讨论】:

    标签: java api google-app-engine maven google-drive-api


    【解决方案1】:

    好吧,我得到了这个工作,但答案并不完全令人满意。根据The Google SDK Documentation for Service Accounts,有两种方法可以与他们进行身份验证。

    首先是创建一个证书(在上面的链接中有详细说明),并使用该证书和服务电子邮件帐户地址。第二种是使用电子邮件帐户和 api 密钥。第二种方法是我使用的方法。

    文档警告:

    "注意:使用 API 密钥参数验证您的应用程序时 - 不使用 Client ID 和 Client Secret 方法 - Google Drive API 的所有应用程序特定功能都将被禁用 因为这种方法不太可信。例如每个文件范围的驱动器 并且无法使用 Application Data 文件夹。”

    但是,我找不到任何有用的信息来详细说明我收到提示此问题的错误的原因。

    所以,为了解决这个问题,我转而使用带有证书文件的电子邮件地址,它工作得很好。

    这是代码,直接指向链接:

    import com.google.api.client.googleapis.extensions.appengine.auth.oauth2.AppIdentityCredential;
    import com.google.api.client.googleapis.services.CommonGoogleClientRequestInitializer;
    import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
    import com.google.api.client.http.HttpTransport;
    import com.google.api.client.http.javanet.NetHttpTransport;
    import com.google.api.client.json.JsonFactory;
    import com.google.api.client.json.jackson.JacksonFactory;
    import com.google.api.services.drive.Drive;
    import com.google.api.services.drive.DriveScopes;
    ...
    
    /** The API Key of the project */
    private static final String API_KEY = "the_api_key_of_the_project";
    
    /**
     * Build and returns a Drive service object authorized with the
     * application's service accounts.
     *
     * @return Drive service object that is ready to make requests.
     */
    public static Drive getDriveService() throws GeneralSecurityException,
        IOException, URISyntaxException {
      HttpTransport httpTransport = new NetHttpTransport();
      JsonFactory jsonFactory = new JacksonFactory();
      AppIdentityCredential credential =
          new AppIdentityCredential.Builder(DriveScopes.DRIVE).build();
      GoogleClientRequestInitializer keyInitializer =
          new CommonGoogleClientRequestInitializer(API_KEY);
      Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
          .setHttpRequestInitializer(credential)
          .setGoogleClientRequestInitializer(keyInitializer)
          .build();
      return service;
    }
    

    将其替换为我的 setDriveService(),一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      相关资源
      最近更新 更多