来自Java端点的Firebase管理员

时间:2023-01-06 23:09:59

I'm trying to access firebase realtime db via admin SDK from an endpoint in Java. Authentication seems to be successful, but query has no effect. No error occurs and listeners are not called. Firebase rule is set writable(assured with simulator).

我正在尝试通过来自Java端点的管理SDK访问firebase实时数据库。身份验证似乎是成功的,但查询无效。不会发生错误,也不会调用侦听器。 Firebase规则设置为可写(使用模拟器确保)。

Firebase db:

Firebase数据库:

    mydbid
      +---users
           +---user0
                +---email:default@gmail.com

Endpoint def:

端点定义:

    @Api(
    name = "firebaseTestEndpoint",
    version = "v1",
    description = "Firebase admin test endpoint.")

public class FirebaseTestService {
    private static final Logger log = 
    Logger.getLogger(FirebaseTestService.class.getName());
    static {
        log.setLevel(Level.INFO);
    }

@ApiMethod(httpMethod = HttpMethod.POST)
public void connectToFirebase(@Named("id") String id,
                              ServletContext servletContext) {

    Map<String, Object> authVars = new HashMap<String, Object>();
    authVars.put("uid", "admin");

    try {
        FirebaseOptions options = new FirebaseOptions.Builder()
                .setDatabaseUrl("https://mydbid.firebaseio.com")            
                .setCredential(FirebaseCredentials.fromCertificate(
                    servletContext.getResourceAsStream(
                        "/WEB-INF/firebase-credentials.json")))
                .setDatabaseAuthVariableOverride(authVars)
                .build();
        FirebaseApp app = FirebaseApp.initializeApp(options);
        DatabaseReference ref = 
    FirebaseDatabase.getInstance().getReference("/users/user0/email");
        ValueEventListener valueEventListener =
    ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot arg0) {
                log.info(arg0.getRef().getPath().toString() + 
                         " changed to " + arg0.getValue(String.class));
            }

            @Override
            public void onCancelled(DatabaseError arg0) {
                log.info("cancelled");
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@ApiMethod(httpMethod = HttpMethod.POST)
public void updateEmail(@Named("id") String id) {

    Map<String,Object> values = new HashMap<String,Object>();
    values.put("email", "updated@gmail.com");
    try {
        FirebaseDatabase.getInstance().getReference("/users/user0")
        .updateChildren(values, new DatabaseReference.CompletionListener() {
            public void onComplete(DatabaseError databaseError, 
                                   DatabaseReference databaseReference) {
                if (databaseError != null) {
                    throw databaseError.toException();
                }
                log.info("update completed.");
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

appengine-web.xml(related parts):

appengine-web.xml(相关部分):

<basic-scaling>
  <max-instances>2</max-instances>
</basic-scaling>

web.xml(related parts; a space inserted before *):

web.xml(相关部分;在*之前插入的空格):

<servlet>
  <servlet-name>EndpointsServlet</servlet-name>
  <servlet-class>com.google.api.server.spi.EndpointsServlet</servlet-class>
  <init-param>
    <param-name>services</param-name>
    <param-value>mypackage.FirebaseTestService</param-value>
  </init-param>
</servlet>
<servlet-mapping>
  <servlet-name>EndpointsServlet</servlet-name>
  <url-pattern>/_ah/api/ *</url-pattern>
</servlet-mapping>

<servlet>
  <servlet-name>SystemServiceServlet</servlet-name>
  <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
  <init-param>
    <param-name>services</param-name>
    <param-value>mypackage.FirebaseTestService</param-value>
  </init-param>
</servlet>
<servlet-mapping>
  <servlet-name>SystemServiceServlet</servlet-name>
  <url-pattern>/_ah/spi/ *</url-pattern>
</servlet-mapping>

Tested in APIs Explorer , connectToFirebase() call is followed by a request to _ah/background, but updateEmail() does not. What's wrong?

在API Explorer中测试,connectToFirebase()调用之后是对_ah / background的请求,但updateEmail()没有。怎么了?

1 个解决方案

#1


0  

To be able to use firebase listeners on AppEngine change basic scaling to manual scaling.

为了能够在AppEngine上使用firebase侦听器,可以将基本缩放更改为手动缩放。

<manual-scaling>
   <instances>1</instances>
</manual-scaling>

If you want to use basic or autoscaling of AppEngine instances change firebase listeners to REST calls.

如果要使用AppEngine实例的基本或自动缩放,请将firebase侦听器更改为REST调用。

#1


0  

To be able to use firebase listeners on AppEngine change basic scaling to manual scaling.

为了能够在AppEngine上使用firebase侦听器,可以将基本缩放更改为手动缩放。

<manual-scaling>
   <instances>1</instances>
</manual-scaling>

If you want to use basic or autoscaling of AppEngine instances change firebase listeners to REST calls.

如果要使用AppEngine实例的基本或自动缩放,请将firebase侦听器更改为REST调用。