该API包'的memcache“或称之为”获取()'未找到 [英] The API package 'memcache' or call 'Get()' was not found

查看:285
本文介绍了该API包'的memcache“或称之为”获取()'未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过可在以下网站上找到了谷歌云端硬盘API的例子工作:的使用服务帐户插入文件设置权限。正如你可能已经猜到,我刚开始接触的谷歌的API。因此,我相信答案是在那里,但我对这个话题的知识还不足够广泛的国米preT的结果。

I am working through the Google Drive API examples that can be found at the following sites: Using Service Accounts, Inserting a File, and Setting Permissions. As you might guess, I'm just getting started with the Google APIs. As such, I'm sure the answer is out there but my knowledge on this topic is not yet broad enough to interpret the findings.

总之,使用谷歌的例子code,我已经成功地连接到驱动为我服务帐户。我不能,但是,插入文件。我收到以下错误:

Anyhow, using Google's example code, I have successfully connected to Drive for my service account. I cannot, however, insert a file. I receive the following error:


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

code是pretty多剪切和粘贴从我上面提到的网站:

Code is pretty much cut-and-paste from the sites I referenced above:

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

The first line listed is the line that causes the error.

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来处理依赖关系:

Finally, I am using Maven to handle dependencies:

<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>

我相信,我错过了一些东西,但不知道那是什么。

I am sure I have missed something, but not sure what that is.

感谢。

推荐答案

好吧,我得到这个工作,但得到的答复是不能完全令人满意。据的谷歌SDK文档服务帐户,有两种方式去与他们进行身份验证。

Well, I got this to work but the answer was not wholly satisfying. According to The Google SDK Documentation for Service Accounts, there are two ways to go about authenticating with them.

首先是创建一个证书(在上面的链路详述),并使用该和服务电子邮件​​帐户地址。二是使用的电子邮件帐户和API密钥。这第二种方法是我用的人。

The first is to create a certificate (detailed in the link above), and use that and the service email account address. The second is to use the email account and the api key. That second method is the one I was using.

文档警告说:

请注意:当使用API​​密钥验证参数,您的应用程序
   - 而不是使用客户端ID和客户端秘法 - 谷歌的API驱动所有应用的特殊功能将被禁用
  因为这种方法是低信任度。例如每个文件的范围驱动器
  和应用程序数据的文件夹,不能使用。

"Note: When authenticating your application using an API Key parameter - instead of using the Client ID and Client Secret method - all application-specific features of the Google Drive API will be disabled as this method is less trusted. For instance the Drive per file scope and the Application Data folder cannot be used."

不过,我能找到详细说明为什么我收到促使这个问题的错误的有用信息。

However, I could find no useful information that detailed why I was receiving the error that prompted this question.

因此​​,要解决这个问题,我切换到使用带有证书文件的电子邮件地址和它的工作就好了。

So, to solve the problem, I switched to using the email address with the certificate file and it worked just fine.

这里的code,直链接:

Here's the code, straight for the link:

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(),一切伟大的工作。

Substituted this in to my setDriveService() and everything worked great.

这篇关于该API包'的memcache“或称之为”获取()'未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆