Android:Cloud End Point的Firebase身份验证不起作用 [英] Android: Firebase auth for Cloud End Point not working

查看:154
本文介绍了Android:Cloud End Point的Firebase身份验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照链接完成了服务器上的配置

 / users:
发表:
描述:<描述>
operationId:< OperationID>
产生:
- application / json
响应:
200:
描述:user List
模式:
$ ref :#/ definitions / echoMessage
参数:
- description:搜索标准
in:body
名称:message
required:true
schema:
$ ref:#/ definitions / echoMessage
security:
- firebase:[]



  firebase:
authorizationUrl:
流:隐式
类型:oauth2
x-google-issuer:https://securetoken.google.com/<Project-ID>
x-google-jwks_uri:https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com

在通过JWT标准之后,我开始知道在调用这个服务的时候,我们必须在Bearer中添加Authorization头,所以我添加了如下的头文件



授权:持票人

我最初尝试使用

  String token = FirebaseInstanceId.getInstance()。getToken(); 

但它给了错误,所以我尝试了,

  FirebaseUser firebaseUser = FirebaseAuth.getInstance()。getCurrentUser(); 
if(firebaseUser!= null){

firebaseUser.getIdToken(true)
.addOnSuccessListener(new OnSuccessListener< GetTokenResult>(){
@Override
)public void onSuccess(GetTokenResult getTokenResult){
String token = getTokenResult.getToken();
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString(Constants.PREFS_FCM_TOKEN,token );
editor.apply();
}
});





$ b

但是即使对于这两个代码,我也会收到错误401和invalid_token

解决方案

经过这么多天的努力,我能够解决这个问题。

我解决了这个问题,

 / users:
post:
description: <描述> 中
operationId:< OperationID>
产生:
- application / json
响应:
200:
描述:user List
模式:
$ ref :#/ definitions / echoMessage
参数:
- description:搜索标准
in:body
名称:message
required:true
schema:
$ ref:#/ definitions / echoMessage
security:
- firebase:[]



  firebase:
authorizationUrl:
流:隐式
类型:oauth2
x-google-issuer:https://securetoken.google.com/<Project-ID>
x-google-jwks_uri:https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com
x-google-audiences:<项目-ID>中//我添加了这个,这是主要的罪魁祸首。

如评论中提到的,我错过了


x-google-audiences:


在服务器配置中。



另一个说明使用哪个标记:我们必须使用第二种方法,我在问题中提到过,如下所示:

  FirebaseUser firebaseUser = FirebaseAuth.getInstance()。getCurrentUser(); 
if(firebaseUser!= null){

firebaseUser.getIdToken(true)
.addOnSuccessListener(new OnSuccessListener< GetTokenResult>(){
@Override
)public void onSuccess(GetTokenResult getTokenResult){
String token = getTokenResult.getToken();
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString(Constants.PREFS_FCM_TOKEN,token );
editor.apply();
}
});
}


I have followed the link and have done the configuration on the server as mentioned.

"/users":
post:
  description: "<Description>"
  operationId: "<OperationID>"
  produces:
  - "application/json"
  responses:
    200:
      description: "user List"
      schema:
        $ref: "#/definitions/echoMessage"
  parameters:
  - description: "Search Criteria"
    in: body
    name: message
    required: true
    schema:
      $ref: "#/definitions/echoMessage"
  security:
    - firebase: []

and

  firebase:
authorizationUrl: ""
flow: "implicit"
type: "oauth2"
x-google-issuer: "https://securetoken.google.com/<Project-ID>"
x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com"

And after going through JWT standards I came to know that while calling calling the service we have to add Authorization header with Bearer so I have added the header as follows,

Authorization: Bearer

I initially tried with

String token = FirebaseInstanceId.getInstance().getToken();

But it gave error so I tried with,

FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    if (firebaseUser != null) {

        firebaseUser.getIdToken(true)
                .addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
                    @Override
                    public void onSuccess(GetTokenResult getTokenResult) {
                        String token = getTokenResult.getToken();
                        SharedPreferences.Editor editor = mSharedPreferences.edit();
                        editor.putString(Constants.PREFS_FCM_TOKEN, token);
                        editor.apply();
                    }
                });
    }

But even with both codes I am getting error as 401 and invalid_token

解决方案

After so many days of struggle I am able to solve the issue.

I solved the issue by following this,

    "/users":
post:
  description: "<Description>"
  operationId: "<OperationID>"
  produces:
  - "application/json"
  responses:
    200:
      description: "user List"
      schema:
        $ref: "#/definitions/echoMessage"
  parameters:
  - description: "Search Criteria"
    in: body
    name: message
    required: true
    schema:
      $ref: "#/definitions/echoMessage"
  security:
    - firebase: []

and

firebase:
authorizationUrl: ""
flow: "implicit"
type: "oauth2"
x-google-issuer: "https://securetoken.google.com/<Project-ID>"
x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com"
x-google-audiences: "<Project-ID>" //I have added this, this was the main culprit.

as mentioned in the comment, I was missing

x-google-audiences: ""

in the server configuration.

And for another clarification for which token to use: We have to use the second approach that I have mentioned in the question, i.e, as below,

FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    if (firebaseUser != null) { 

        firebaseUser.getIdToken(true)
                .addOnSuccessListener(new OnSuccessListener<GetTokenResult>() { 
                    @Override 
                    public void onSuccess(GetTokenResult getTokenResult) {
                        String token = getTokenResult.getToken();
                        SharedPreferences.Editor editor = mSharedPreferences.edit();
                        editor.putString(Constants.PREFS_FCM_TOKEN, token);
                        editor.apply();
                    } 
                }); 
    }

这篇关于Android:Cloud End Point的Firebase身份验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆