我正在調用Firebase令牌,然後使用該令牌從服務器獲取令牌。從覆蓋回調中返回一個值
我想userSignIn()
從我的服務器返回令牌。
有誰知道我該如何將代幣退回userSignIn()
?
@Override
public String userSignIn(String email, String password, String authType) throws Exception {
login(email, password, authType, new OnLoginResponseCallback() {
@Override
public String onLoginResponse(boolean success, String token) {
**return token;** // how do I return this to userSignIn???
}
});
}
public interface OnLoginResponseCallback {
public String onLoginResponse(boolean success, String token);
}
public void login(String email, String password, String authType, final OnLoginResponseCallback callback) throws Exception {
getFirebaseToken(email, password, new OnFirebaseTokenResponseCallback() {
@Override
public String onFirebaseTokenResponse(boolean success, String token) {
getAuthToken(token, null, new OnAuthTokenResponseCallback(){
@Override
public String onAuthTokenResponse(boolean success, JSONObject response){
try {
String access_token = response.getString("access_token");
callback.onLoginResponse(true, access_token);
}
catch (JSONException ex) {
}
}
});
}
});
}
public interface OnFirebaseTokenResponseCallback {
public String onFirebaseTokenResponse(boolean success, String token);
}
public void getFirebaseToken(String email, String password, final OnFirebaseTokenResponseCallback callback) {
FirebaseAuth auth = FirebaseAuth.getInstance();
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
} else {
AuthResult result = task.getResult();
FirebaseUser user = result.getUser();
user.getToken(false).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
@Override
public void onComplete(@NonNull Task<GetTokenResult> task) {
if (task.isSuccessful()) {
try {
String token = task.getResult().getToken();
callback.onFirebaseTokenResponse(true, token);
}
catch (Exception ex) {
}
} else {
}
}
});
}
}
});
}
public interface OnAuthTokenResponseCallback {
public String onAuthTokenResponse(boolean success, JSONObject response);
}
public void getAuthToken(String token, String refreshToken, final OnAuthTokenResponseCallback callback) throws JSONException {
RequestParams params = new RequestParams();
if (refreshToken != null)
{
params.add("grant_type", "refresh_token");
params.add("refresh_token", refreshToken);
}
else if (token != null)
{
params.add("grant_type", "urn:ietf:params:oauth:grant-type:firebase_token");
params.add("assertion", token);
}
else if (refreshToken == null && token == null)
{
params.add("grant_type", "password");
params.add("username", "");
params.add("password", "");
}
AuthClient.post("connect/token", params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, JSONObject response) {
try {
callback.onAuthTokenResponse(true, response);
} catch (Exception ex) {
}
}
@Override
public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, Throwable throwable, JSONObject response) {
callback.onAuthTokenResponse(false, new JSONObject());
}
});
}
UPDATE:
感謝。我去掉了多餘的方法和呼叫登錄這樣的:
.login(userName, userPass, mAuthTokenType, new OnLoginResponseCallback() {
@Override
public void onLoginResponse(boolean success, String token) {
data.putString(AccountManager.KEY_ACCOUNT_NAME, userName);
data.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType);
data.putString(AccountManager.KEY_AUTHTOKEN, token);
data.putString(PARAM_USER_PASS, userPass);
}
});
,我認爲它的工作原理,但還沒有來得及充分測試它。我不確定的一件事是我試圖用「token」中的值修改「data」,然而「data」是一個最終的Bundle,所以我不確定它是否有效。稍後再測試。謝謝。
「任何人都知道我可以返回令牌 'userSignIn'?」 - 你沒有。從'onLoginResponse()'方法啓動任何需要'token'的代碼。 –
你的接口方法不需要返回數據。他們都應該是無效的 –