我試圖做從我的本地機器訪問我的數據連接到谷歌應用程序引擎遠程API:不能按照本指南操作
https://cloud.google.com/appengine/docs/java/tools/remoteapi
我已經folowed引導和改變的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();
}
}
}
任何人有一個想法如何解決這個問題?
UPDATE:
現在憑證似乎是固定的。然而現在的堆棧跟蹤返回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,但我已經做到了它發生。還有什麼我需要在服務器端?
如何使用JSON任何提示?像一個教程或一些示例代碼?我能找到的唯一樣本使用gcloud庫而不是appengine。 –
RemoteApiOptions raoptions = new RemoteApiOptions() .server(「my-module-dot-my-project.appspot.com」,443) .useServiceAccountCredential(「my-service-account-id」,「my-p12-file的.p12" ); – varun
http://varunsweblog.appspot。com/posts/how-to-use-google-appengine-remote-api-for-java – varun