Android:Cloud End Point的Firebase身份验证无效

时间:2022-03-04 04:29:11

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,

在完成JWT标准之后,我开始知道在调用服务时我们必须添加带有Bearer的Authorization标头,所以我添加了如下标题,

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

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

1 个解决方案

#1


0  

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: ""

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

#1


0  

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: ""

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