I implement Google login using GoogleAccountCredential
method.
Init at onCreate
method
mCredential = GoogleAccountCredential.usingOAuth2(
getApplicationContext(), Arrays.asList(SCOPES))
.setBackOff(new ExponentialBackOff());
When button tap action
public void getResultsFromApi() throws IOException {
if (!isGooglePlayServicesAvailable()) {
acquireGooglePlayServices();
} else if (mCredential.getSelectedAccountName() == null) {
chooseAccount();
} else if (!isDeviceOnline()) {
Utils.showAlert(this, getResources().getString(R.string.no_internet));
} else {
new MakeRequestTask(mCredential).execute();
}
}
And I got successfully logged in.
How can I logout using GoogleAccountCredential
.
This is for youtube access. So, I have to use GoogleAccountCredential
with OAuth2.
UPDATE ONE 12 JULY 2018
@Override
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_GOOGLE_PLAY_SERVICES:
if (resultCode != RESULT_OK) {
Utils.showAlert(this, getResources().getString(R.string.youtube_alert_for_install_playstore));
} else {
try {
//Some code
} catch (IOException e) {
e.printStackTrace();
}
}
break;
case REQUEST_ACCOUNT_PICKER:
if (resultCode == RESULT_OK && data != null &&
data.getExtras() != null) {
String accountName =
data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null) {
SharedPreferences settings =
getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(PREF_ACCOUNT_NAME, accountName);
editor.apply();
mCredential.setSelectedAccountName(accountName);
try {
//Some code
} catch (IOException e) {
e.printStackTrace();
}
}
}
break;
case REQUEST_AUTHORIZATION:
if (resultCode == RESULT_OK) {
try {
//Some code
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
}
Tried to clear SharedPreferences
for key PREF_ACCOUNT_NAME
no use