Android: Firebase auth for Cloud End Point not working

試著忘記壹切 提交于 2019-12-12 04:21:31

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/45104090/android-firebase-auth-for-cloud-end-point-not-working

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