0

我遵循link,並已完成上述服務器上的配置。Android:針對雲端點的Firebase身份驗證無法正常工作

"/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: [] 

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/[email protected]" 

並通過智威湯遜的標準去後,我才知道,同時呼籲調用服務,我們必須增加與承載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(); 
        } 
       }); 
    } 

但即使有兩個代碼,我得到錯誤爲401和INVALID_TOKEN

回答

0

後奮鬥了這麼多天,我能解決問題。

我按照這個解決的問題,

"/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: [] 

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/[email protected]" 
x-google-audiences: "<Project-ID>" //I have added this, this was the main culprit. 

在註釋中,我缺少

X-谷歌,觀衆: 「」

在服務器配置中。

而對於另一個澄清爲此令牌使用:我們使用的是我在這個問題提到的第二種方法,即,下面,

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(); 
        } 
       }); 
    } 
相關問題