是否可以仅在用 Java 编写的 Android 应用程序中通过凭证管理器 [1] 接收
serverAuthCode
?
我们的后端只需要这些信息,因此它可能会向 Google 发出请求,以接收有关尝试登录的用户的信息。
我当前的实现是:
CredentialManager manager = CredentialManager.create(getApplicationContext());
GetGoogleIdOption googleIdOption = new GetGoogleIdOption.Builder()
.setFilterByAuthorizedAccounts(true) // only existing google accounts, no "create new google account" screen
.setAutoSelectEnabled(true)
.setServerClientId("web_client_id")
.build();
GetCredentialRequest request = new GetCredentialRequest.Builder()
.addCredentialOption(googleIdOption)
.build();
CredentialManagerCallback<GetCredentialResponse, GetCredentialException> credentialManagerCallback = new CredentialManagerCallback<GetCredentialResponse, GetCredentialException>() {
@Override
public void onResult(GetCredentialResponse result) {
// result here has all the data of the user, like email, name
// wanted: only serverAuthToken to send over to the backend which is then makeing the google api request
}
@Override
public void onError(@NonNull GetCredentialException error) {
// handle error
}
};
manager.getCredentialAsync(
_activity_instance,
request,
null,
Executors.newSingleThreadExecutor(),
credentialManagerCallback
);
[1] https://developer.android.com/identity/sign-in/credential-manager
不,这是不可能的。仅当您使用Authorization APIs时,您才能收到授权码;凭证管理器的目的是身份验证而不是授权。