java - 每个 API 的 Firestore 导入/导出

标签 java firebase google-app-engine google-cloud-firestore google-client-login

我正在寻找一种方法来调用 firestore import/export通过 Java 代码以编程方式实现功能。

到目前为止我发现很好的firestore client library尚不支持导入/导出调用。但级别较低rest/grpc api已经支持他们了。使用java library我尝试了以下方法:

Firestore firestoreApi = new Firestore
    .Builder(UrlFetchTransport.getDefaultInstance(), new GsonFactory(), null)
    .setApplicationName(SystemProperty.applicationId.get())
    .build();

GoogleFirestoreAdminV1beta2ImportDocumentsRequest importRequest = new GoogleFirestoreAdminV1beta2ImportDocumentsRequest();
importRequest.setInputUriPrefix(String.format("gs://{}/{}/", BUCKET, image));

GoogleLongrunningOperation operation = firestoreApi
    .projects()
    .databases()
    .importDocuments("projects/" + SystemProperty.applicationId.get() + "/databases/(default)", importRequest)
    .execute();

遗憾的是,在应用引擎中运行时,缺少权限:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401
{
  "code": 401,
  "errors": [
    {
      "domain": "global",
      "location": "Authorization",
      "locationType": "header",
      "message": "Login Required.",
      "reason": "required"
    }
  ],
  "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
  "status": "UNAUTHENTICATED"

我无法获取official way登录才能工作,因为 firestore 构建器没有接受 AppEngineCredentials 实例的方法。

我已经检查了 python 客户端库,它似乎也不支持这些方法(尚)。 有谁知道我如何使用旧的 REST API 登录或获取支持这些方法的客户端库(请在应用程序引擎上运行某种语言:))

感谢您的阅读! 卡斯滕

最佳答案

您可以调整此 Cloud Datastore example对于 Cloud Firestore。在这里查看他们如何获取访问 token :

import com.google.appengine.api.appidentity.AppIdentityService;
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;

// Get an access token to authorize export request
      ArrayList<String> scopes = new ArrayList<String>();
      scopes.add("https://www.googleapis.com/auth/datastore");
      final AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
      final AppIdentityService.GetAccessTokenResult accessToken =
          AppIdentityServiceFactory.getAppIdentityService().getAccessToken(scopes);
      connection.addRequestProperty("Authorization", "Bearer " + accessToken.getAccessToken());

关于java - 每个 API 的 Firestore 导入/导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55636014/

相关文章:

java - 在线获取 UTC 时间的可靠方法?

Firebase Analytics 不正确的活跃用户数

java - Firebase Android 中的 ListView 制作

google-app-engine - 在 Google 电子表格中标记重复条目

java - 我在 intellij IDEA 中看不到 Jaxb 工具

运行 Solr 需要 Java 1.7 或更高版本,但已安装 1.8

java - 关于泛型方法的问题

firebase - 需要启用 App Engine 才能使用 Cloud Firestore

java - 通过反射动态给Guice添加类

python - 让 gcloud 在 Cygwin Windows 中工作