【问题标题】:Cannot connect to Google App Engine Remote API无法连接到 Google App Engine 远程 API
【发布时间】:2016-09-30 11:13:17
【问题描述】:

我正在尝试按照本指南从本地计算机访问我的数据:

https://cloud.google.com/appengine/docs/java/tools/remoteapi

我已经按照指南更改了 web.xml,部署了代码,安装了 cloud sdk,运行“gcloud init”命令并选择了项目,但是在运行示例代码时出现异常:

Exception in thread "main" java.lang.RuntimeException: Failed to acquire Google Application Default credential.
    at com.google.appengine.tools.remoteapi.RemoteApiOptions.useApplicationDefaultCredential(RemoteApiOptions.java:169)
    at androidlost_remote.RemoteTest.main(RemoteTest.java:14)
Caused by: java.io.IOException: The Application Default Credentials are not available. They are available if running on Google App Engine, Google Compute Engine, or Google Cloud Shell. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
    at com.google.appengine.repackaged.com.google.api.client.googleapis.auth.oauth2.DefaultCredentialProvider.getDefaultCredential(DefaultCredentialProvider.java:98)
    at com.google.appengine.repackaged.com.google.api.client.googleapis.auth.oauth2.GoogleCredential.getApplicationDefault(GoogleCredential.java:213)
    at com.google.appengine.repackaged.com.google.api.client.googleapis.auth.oauth2.GoogleCredential.getApplicationDefault(GoogleCredential.java:191)
    at com.google.appengine.tools.remoteapi.RemoteApiOptions.useApplicationDefaultCredential(RemoteApiOptions.java:164)
    ... 1 more

这是我的代码:

import java.io.IOException;

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;

public class RemoteTest {

    public static void main(String[] args) throws IOException {
        RemoteApiOptions options = new RemoteApiOptions().server("myappid-hr.appspot.com", 443).useApplicationDefaultCredential();
        RemoteApiInstaller installer = new RemoteApiInstaller();
        installer.install(options);
        try {
            DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
            System.out.println("Key of new entity is " + ds.put(new Entity("Hello Remote API!")));
        } finally {
            installer.uninstall();
        }
    }
}

有人知道如何解决这个问题吗?

更新:

现在凭证似乎已修复。但是现在堆栈跟踪返回 302:

Exception in thread "main" com.google.appengine.repackaged.com.google.api.client.http.HttpResponseException: 302 Found
    at com.google.appengine.repackaged.com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1070)
    at com.google.appengine.tools.remoteapi.OAuthClient.get(OAuthClient.java:64)
    at com.google.appengine.tools.remoteapi.RemoteApiInstaller.getAppIdFromServer(RemoteApiInstaller.java:413)
    at com.google.appengine.tools.remoteapi.RemoteApiInstaller.loginImpl(RemoteApiInstaller.java:376)
    at com.google.appengine.tools.remoteapi.RemoteApiInstaller.login(RemoteApiInstaller.java:337)
    at com.google.appengine.tools.remoteapi.RemoteApiInstaller.install(RemoteApiInstaller.java:173)
    at androidlost_remote.RemoteTest3.main(RemoteTest3.java:16)

根据post,当服务器没有在 web.xml 中获得远程 api 但我已经做到了时,就会发生这种情况。在服务器端我还需要什么?

【问题讨论】:

    标签: java google-app-engine


    【解决方案1】:

    创建一个新的凭据,授予它正确的 IAM 访问权限,下载 json 并最终在使用 api 之前使用它。 :)

    Make sure remote API is enabled
    
    <servlet>
    <display-name>Remote API Servlet</display-name>
    <servlet-name>RemoteApiServlet</servlet-name>
    <servlet-class>com.google.apphosting.utils.remoteapi.RemoteApiServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>RemoteApiServlet</servlet-name>
    <url-pattern>/remote_api</url-pattern>
    </servlet-mapping>
    
    RemoteApiOptions raoptions = new RemoteApiOptions() .server("my-module-dot-my-project.appspot.com", 443) .useServiceAccountCredential("my-service-account-id", "my-p12-file.p12"); 
    

    阅读更多here

    【讨论】:

    • 关于如何使用 json 的任何提示?喜欢教程或一些示例代码?我能找到的唯一示例使用 gcloud 库而不是 appengine。
    • RemoteApiOptions raoptions = new RemoteApiOptions() .server("my-module-dot-my-project.appspot.com", 443) .useServiceAccountCredential("my-service-account-id", "我的-p12-file.p12");
    • 谢谢 - 解决了凭据问题。现在到 302 :-)
    • 如果您认为这有帮助,那么请投票:)
    【解决方案2】:

    好吧,让我们分解错误代码。错误代码的第一行:Failed to acquire Google Application Default credential. 告诉您什么不正常(无法获得 GAD 凭证)。

    下一行注释是:at androidlost_remote.RemoteTest.main(RemoteTest.java:14)。这告诉您错误是在 remoteTest 类的第 14 行引起的。

    第 14 行是installer.install(options);。这指向您的 RemoteApiOptions options 变量。确保.options() 参数指向正确的网络服务器和端口。

    现在回到错误代码,第 4 行直接告诉你它发生的原因:java.io.IOException。它说这是因为They are available if running on Google App Engine, Google Compute Engine, or Google Cloud Shell. 我猜你没有所有这些。所以错误代码告诉你你需要做什么以及去哪里寻求帮助。您需要安装 Google App Engine、Google Compute Engine 或 Google Cloud Shell。

    您要么安装其中一个/全部,环境变量 GOOGLE_APPLICATION_CREDENTIALS 必须定义为指向定义凭据的文件

    从错误代码的第 4 行:https://developers.google.com/accounts/docs/application-default-credentials 了解更多信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 2017-08-28
      • 1970-01-01
      • 1970-01-01
      • 2019-11-27
      相关资源
      最近更新 更多