Firebase的Retrofit2身份验证密钥

时间:2017-01-26 16:08:39

标签: android firebase retrofit2

我需要使用改造来使用firebase json进行身份验证。我使用代码https://futurestud.io/tutorials/retrofit-2-manage-request-headers-in-okhttp-interceptor

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();  
httpClient.addInterceptor(new Interceptor() {  
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
    Request original = chain.request();

    // Request customization: add request headers
    Request.Builder requestBuilder = original.newBuilder()
            .header("Authorization", "auth-value"); // <-- this is the important line

    Request request = requestBuilder.build();
    return chain.proceed(request);
}
});

OkHttpClient client = httpClient.build(); 

当然可以。我可以使用firebase控制台的auth-value键吗?

1 个答案:

答案 0 :(得分:0)

根据官方火力基地doc

  

要检索访问令牌,您需要使用服务帐户。请参阅使用Google服务帐户的指南。您可以从Firebase控制台的“服务帐户”部分在Firebase项目中创建服务帐户凭据。

GoogleCredential googleCred = GoogleCredential.fromStream(new FileInputStream("service_account.json"));
GoogleCredential scoped = googleCred.createScoped(
    Arrays.asList(
      "https://www.googleapis.com/auth/firebase.database",
      "https://www.googleapis.com/auth/userinfo.email"
    )
);
scoped.refreshToken();
String token = scoped.getAccessToken();

使用访问令牌

  

数据库REST API接受查询字符串或标题Authorization:Bearer上的access_token =以使用服务帐户验证请求。