Java 无法连接到Google应用程序引擎远程API

Java 无法连接到Google应用程序引擎远程API,java,google-app-engine,Java,Google App Engine,我正试图通过以下指南从本地计算机访问我的数据: 我遵循指南,更改了web.xml,部署了代码,安装了cloud sdk,运行了“gcloud init”命令并选择了项目,但在运行示例代码时,我遇到了一个异常: Exception in thread "main" java.lang.RuntimeException: Failed to acquire Google Application Default credential. at com.google.appengine.tool

我正试图通过以下指南从本地计算机访问我的数据:

我遵循指南,更改了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();
        }
    }
}
有人知道如何解决这个问题吗

更新:

现在凭证似乎已修复。但是,现在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时,就会发生这种情况,但我已经这样做了。服务器端还有什么需要的吗?

好吧,让我们分析一下错误代码。错误代码的第一行:
未能获取Google应用程序默认凭据。
告诉您什么不正常(无法获取GAD凭据)

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

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

现在回到错误代码,第4行直接告诉您发生错误的原因:a
java.io.IOException
。它说这是因为
如果在谷歌应用引擎、谷歌计算引擎或谷歌云壳上运行,它们是可用的。
我猜你没有这些。因此,错误代码告诉您需要做什么以及到哪里寻求帮助。您需要安装Google App Engine、Google Compute Engine或Google Cloud Shell

要么安装其中一个/所有这些,要么必须定义指向定义凭据的文件的环境变量GOOGLE\u APPLICATION\u CREDENTIALS


从错误代码的第4行开始:要了解更多信息,我们来分析错误代码。错误代码的第一行:
未能获取Google应用程序默认凭据。
告诉您什么不正常(无法获取GAD凭据)

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

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

现在回到错误代码,第4行直接告诉您发生错误的原因:a
java.io.IOException
。它说这是因为
如果在谷歌应用引擎、谷歌计算引擎或谷歌云壳上运行,它们是可用的。
我猜你没有这些。因此,错误代码告诉您需要做什么以及到哪里寻求帮助。您需要安装Google App Engine、Google Compute Engine或Google Cloud Shell

要么安装其中一个/所有这些,要么必须定义指向定义凭据的文件的环境变量GOOGLE\u APPLICATION\u CREDENTIALS


从错误代码的第4行开始:有关更多信息,请创建一个新凭据,授予它IAM访问权,下载json,并在使用api之前使用它。:)

确保已启用远程API
远程API Servlet
RemoteApiServlet
com.google.apphosting.utils.remoteapi.RemoteApiServlet
1.
RemoteApiServlet
/远程应用编程接口

RemoteApiOptions raoptions=new RemoteApiOptions().server(“my module.dot my project.appspot.com”,443).useServiceAccountCredential(“我的服务帐户id”,“my-p12-file.p12”);

阅读更多内容

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

确保已启用远程API
远程API Servlet
RemoteApiServlet
com.google.apphosting.utils.remoteapi.RemoteApiServlet
1.
RemoteApiServlet
/远程应用编程接口

RemoteApiOptions raoptions=new RemoteApiOptions().server(“my module.dot my project.appspot.com”,443).useServiceAccountCredential(“我的服务帐户id”,“my-p12-file.p12”);

阅读更多

关于如何使用json的提示?像是教程还是一些示例代码?我能找到的唯一示例使用gcloud库,而不是appengine.RemoteApiOptions raoptions=new RemoteApiOptions().server(“my module.dot my project.appspot.com”,443)。useServiceAccountCredential(“我的服务帐户id”,“my-p12-file.p12”);谢谢-这解决了凭据问题。现在转到302:-)如果你认为这有帮助,那么投票表决如何:)关于如何使用json的任何提示?像是教程还是一些示例代码?我能找到的唯一示例使用gcloud库,而不是appengine.RemoteApiOptions raoptions=new RemoteApiOptions().server(“my module.dot my project.appspot.com”,443)。useServiceAccountCredential(“我的服务帐户id”,“my-p12-file.p12”);谢谢-这解决了凭据问题。现在转到302:-)如果你认为这有帮助,那么投票表决怎么样:)
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");