Bạn có thể nhận GoogleSignInResult
qua 2 cách để xác thực.
i) Bằng cách nhập id email và mật khẩu vào màn hình đăng nhập google.
ii) Bằng cách chọn tài khoản từ tài khoản đã đăng nhập trong điện thoại.
tôi đã sử dụng phương pháp thứ 2 để nhận mã thông báo truy cập và xác thực người dùng.
để có thêm liên kết tham chiếu hỗ trợ được cung cấp bên dưới.
google sign in link 1
Stackoverflow - token refresh
google auth provider documentation
server side token verification docs
nếu mục tiêu duy nhất của bạn là, để nhận mã thông báo, do đó bạn cũng có thể thử this github source.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
// sử dụng tùy chọn đăng nhập để tạo bản sao máy khách api.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent,RC_SIGN_IN); }
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN){
GoogleSignInResult result =Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
} else {
// Signed out, show unauthenticated.
}
}
// được chứng thực
AuthCredential credential =
GoogleAuthProvider.getCredential(acct.getIdToken(), null);
FirebaseAuth.getInstance().getCurrentUser().reauthenticate(credential)
.addOnCompleteListener(new OnCompleteListener<Void>() {
// add your job here on authenticated
}
// if token is obsoleted then you can do this
credential.refreshToken();
accessToken = credential.getAccessToken();
Tôi không nhớ những gì tôi đã kết thúc làm (đó là 4 months ago), nhưng tôi nghĩ tôi cần một cái gì đó tĩnh mà tôi có thể sử dụng bất cứ khi nào tôi muốn và không chỉ khi đăng nhập (ví dụ: nếu người dùng muốn xóa tài khoản của họ). Dù sao, bởi vì tôi không làm việc này nữa, nếu ai đó tin rằng đây là câu trả lời đúng, tôi sẽ chọn nó như một câu trả lời. –