【问题标题】:Firestore import/export per API每个 API 的 Firestore 导入/导出
【发布时间】:2019-04-11 15:21:43
【问题描述】:

我正在寻找一种从 java 代码中以编程方式调用 firestore import/export 功能的方法。

到目前为止,我发现漂亮的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登录或获取支持这些方法的客户端库(请在应用引擎上运行一些语言:))

感谢阅读! 卡斯滕

【问题讨论】:

  • 这是 Node 中的一个示例:firebase.google.com/docs/firestore/solutions/schedule-export。对于 Java,您可以修改此 Cloud Datastore example
  • @JRLtechwriting 非常感谢!采用数据存储示例有效!我认为应用引擎帐户需要“Cloud Datastore Import Export Admin”和“Firebase Admin SDK Administrator Service Agent”但这些都是次要的细节。顺便说一句:如果您添加您的评论作为答案,我会接受它:)

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


【解决方案1】:

您可以将此 Cloud Datastore example 改编为 Cloud Firestore。在此处查看他们如何获取访问令牌:

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());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 2020-03-27
    • 1970-01-01
    • 2021-11-18
    • 2020-12-09
    • 2018-03-18
    • 2021-10-26
    相关资源
    最近更新 更多