繁体   English   中英

无法连接到Google App Engine Remote API

[英]Cannot connect to Google App Engine Remote API

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

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

我已经了解了指南并更改了web.xml,部署了代码,安装了云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();
        }
    }
}

任何人都知道如何解决这个问题?

更新:

现在证书似乎已修复。 但是现在stacktrace返回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)

根据这篇文章,它发生在服务器没有在web.xml中获得远程api但我已经这样做了。 我需要在服务器端的其他任何东西?

好吧,让我们分解错误代码。 错误代码的第一行: 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()参数指向正确的Web服务器和端口。

现在回到错误代码,第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了解更多信息

创建一个新的Credential,给它正确的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"); 

在这里阅读更多

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM