将Firebase令牌从Android发送到Google Cloud端点

时间:2022-12-17 23:10:33

I am integrating Firebase Authentication into my Android App. After successful login, the Android App will call some Google Cloud Endpoints running on Google App Engine.

我正在将Firebase身份验证集成到我的Android应用中。成功登录后,Android应用程序将调用在Google App Engine上运行的某些Google Cloud端点。

So I need to pass the Firebase Token I received from the Authentication to the Google Cloud Endpoints. I am using the following code to call the endpoints (source)

因此,我需要将从身份验证收到的Firebase令牌传递给Google Cloud端点。我使用以下代码来调用端点(源代码)

MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
                new AndroidJsonFactory(), null)
myApiService = builder.build();

myApiService.sayHi(name).execute();

So how can I forward the token to my backend?

那么如何将令牌转发到我的后端呢?

1 个解决方案

#1


1  

You can use an HttpRequestInitializer to inject the token:

您可以使用HttpRequestInitializer注入令牌:

HttpRequestInitializer requestInitializer = new HttpRequestInitializer() {
  @Override
  public void initialize(HttpRequest request) throws IOException {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setAuthorization("Bearer " + getFirebaseToken());
    request.setHeaders(httpHeaders);
  }
};

MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
            new AndroidJsonFactory(), requestInitializer)
myApiService = builder.build();

#1


1  

You can use an HttpRequestInitializer to inject the token:

您可以使用HttpRequestInitializer注入令牌:

HttpRequestInitializer requestInitializer = new HttpRequestInitializer() {
  @Override
  public void initialize(HttpRequest request) throws IOException {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setAuthorization("Bearer " + getFirebaseToken());
    request.setHeaders(httpHeaders);
  }
};

MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
            new AndroidJsonFactory(), requestInitializer)
myApiService = builder.build();