我的大学项目之一是使用Android Studio创建移动应用程序,并且该应用程序必须使用REST API,Google登录和身份验证。我的应用程序将访问用户的私有Google图书库。为此,我创建了一个Google登录以验证用户身份。
Scope SCOPE_BOOKS =
new Scope("https://www.googleapis.com/auth/books");
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.requestProfile()
.requestScopes(SCOPE_BOOKS)
.build();
mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
.enableAutoManage(this, this )
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addScope(SCOPE_BOOKS)
.build();
一切正常。我什至会收到一个对话框,要求用户允许我的应用程序访问其Google图书。从现在开始我要去哪里?如何获得访问令牌?等
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = firebaseAuth.getCurrentUser();
mStatusTextView.setText(getString(R.string.signed_in_fmt, user.getDisplayName()));
Glide.with(MainActivity.this).load(user.getPhotoUrl()).into(mBannerImageView);
AddToLocalStorage("userData", "DisplayName", user.getDisplayName());
AddToLocalStorage("userData", "Email", user.getEmail());
AddToLocalStorage("userData", "Avatar", user.getPhotoUrl().toString());
AddToLocalStorage("userData", "UserId", user.getUid());
Toast.makeText(MainActivity.this,
"Welcome, " + user.getDisplayName(), Toast.LENGTH_SHORT).show();
updateUI(true);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
//Toast.make(findViewById(R.id.main_layout), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
updateUI(false);
}
hideProgressDialog();
}
});
}
如何创建回调以授予访问令牌?我已经从一开始就知道Google的oAuth是一件麻烦事,尤其是尝试访问用户的私人数据时。有人可以挥舞魔杖来帮助同学吗?