Send Firebase Token from Android to Google Cloud Endpoints

这一生的挚爱 提交于 2019-12-30 13:38:30

问题


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.

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)

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:


You can use an HttpRequestInitializer to inject the token:

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


来源:https://stackoverflow.com/questions/41329587/send-firebase-token-from-android-to-google-cloud-endpoints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!