来自servlet的RemoteApi调用未在本地应用引擎应用

时间:2016-04-17 01:19:06

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

我正在尝试这个例子https://cloud.google.com/appengine/docs/java/tools/remoteapi 如果我将脚本作为java应用程序运行,一切正常,但是当我将它作为servlet执行时,它总是永远加载并且不会抛出任何错误。也适用于localhost。另外我注意到它发生在查询时,当我将其注释掉(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();
    }
    }
}

1 个答案:

答案 0 :(得分:1)

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