我已按照link进行了操作,并按照提及的那样在服务器上完成了配置。
"/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/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();
}
});
}
但即使使用这两个代码,我也会收到错误401和invalid_token
答案 0 :(得分: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/securetoken@system.gserviceaccount.com"
x-google-audiences: "<Project-ID>" //I have added this, this was the main culprit.
如评论中所述,我不见了
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();
}
});
}