java - 来自 servlet 的 RemoteApi 调用未在本地应用程序引擎应用程序中加载

标签 java google-app-engine google-cloud-datastore

我正在尝试这个例子https://cloud.google.com/appengine/docs/java/tools/remoteapi 如果我将脚本作为 java 应用程序运行,一切都会正常工作,但是当我将其作为 servlet 运行时,它总是会永远加载并且不会抛出任何错误。在本地主机上也可以正常工作。我还注意到在进行查询时会发生这种情况,当我将其注释掉(datastore.put)时,servlet 会立即加载。

import java.io.IOException;
import javax.servlet.http.*;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.tools.remoteapi.RemoteApiInstaller;
import com.google.appengine.tools.remoteapi.RemoteApiOptions;

@SuppressWarnings("serial")
public class Gae_java_Servlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

    RemoteApiOptions options = new RemoteApiOptions()
        .server("java-dot-project.appspot.com", 443)
        .useApplicationDefaultCredential();
    RemoteApiInstaller installer = new RemoteApiInstaller();
    installer.install(options);
    try {
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        System.out.println("Key of new entity is " +
                datastore.put(new Entity("Hello Remote API!")));
    } finally {
        installer.uninstall();
    }
    }
}

最佳答案

我想通了,需要使用 RemoteApiOptions().useServiceAccountCredential("service email", "p12key") 而不是 useApplicationDefaultCredential()

关于java - 来自 servlet 的 RemoteApi 调用未在本地应用程序引擎应用程序中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36671668/

相关文章:

java - 如何从外部包资源导入 CSS 文件

java - 扩展泛型类时出现 ClassCastException

java - 对对象类型(如用户)进行全局计数,最佳做法?

python-3.x - 值错误 : Unknown protobuf attr type <class 'dict' > when tried to put a nested dict/entity

java - 将字符串日期更改为另一个字符串日期

javascript - 通过 Java JAX-RS REST 服务发送带有 JS 的 HTML

google-app-engine - 是否可以在 GAE Flexible 自定义运行时中安装 Google Filestore

java - 单元测试 Google App Engine 1.6.4 中出现非法 blobKey 错误

google-app-engine - 数据存储管理员以什么格式备份实体?

java - Google Cloud Datastore - 不平等返回 "no matching index found"