我正在将Firebase身份验证集成到我的Android应用中。成功登录后,Android应用程序将调用在Google App Engine上运行的一些Google Cloud端点。
因此,我需要将从身份验证收到的Firebase令牌传递给Google Cloud端点。我使用以下代码来调用端点(source)
MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
myApiService = builder.build();
myApiService.sayHi(name).execute();
那么如何将令牌转发到我的后端呢?
答案 0 :(得分:1)
您可以使用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();