简体   繁体   English

Firebase的Retrofit2身份验证密钥

[英]Retrofit2 authentication key to Firebase

I need to use the retrofit for consuming firebase json with authentication. 我需要使用改进来使用firebase json进行身份验证。 I use code by https://futurestud.io/tutorials/retrofit-2-manage-request-headers-in-okhttp-interceptor 我使用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(); 

For sure. 当然。 What auth-value key from firebase console do i use please ? 我可以使用firebase控制台的auth-value键吗?

As per official firebase doc : 根据官方firebase 文档

To retrieve an access token you need to use a service account. 要检索访问令牌,您需要使用服务帐户。 Please see the guide for using Google Service Accounts. 请参阅使用Google服务帐户的指南。 You can create a service account credential in your Firebase project from the Service Accounts section of the Firebase console. 您可以从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();

Using the access token 使用访问令牌

The Database REST API accepts access_token= on the query string or header Authorization: Bearer to authenticate a request with a service account. 数据库REST API接受查询字符串或标题Authorization:Bearer上的access_token =以使用服务帐户验证请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM