Download File
Download Project
Settings
Line Wrap
Themes
default
ambiance
bespin
dracula
eclipse
material
mbo
mdn-like
neat
solarized dark
ttcn
zenburn
IdTokenActivity.java
package com.google.samples.quickstart.signin; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.auth.api.signin.GoogleSignIn; import com.google.android.gms.auth.api.signin.GoogleSignInAccount; import com.google.android.gms.auth.api.signin.GoogleSignInClient; import com.google.android.gms.auth.api.signin.GoogleSignInOptions; import com.google.android.gms.common.api.ApiException; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; /** * Demonstrates retrieving an ID token for the current Google user. */ public class IdTokenActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "IdTokenActivity"; private static final int RC_GET_TOKEN = 9002; private GoogleSignInClient mGoogleSignInClient; private TextView mIdTokenTextView; private Button mRefreshButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Views mIdTokenTextView = findViewById(R.id.detail); mRefreshButton = findViewById(R.id.button_optional_action); mRefreshButton.setText(R.string.refresh_token); // Button click listeners findViewById(R.id.sign_in_button).setOnClickListener(this); findViewById(R.id.sign_out_button).setOnClickListener(this); findViewById(R.id.disconnect_button).setOnClickListener(this); mRefreshButton.setOnClickListener(this); // For sample only: make sure there is a valid server client ID. validateServerClientID(); // [START configure_signin] // Request only the user's ID token, which can be used to identify the // user securely to your backend. This will contain the user's basic // profile (name, profile picture URL, etc) so you should not need to // make an additional call to personalize your application. GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.server_client_id)) .requestEmail() .build(); // [END configure_signin] // Build GoogleAPIClient with the Google Sign-In API and the above options. mGoogleSignInClient = GoogleSignIn.getClient(this, gso); } private void getIdToken() { // Show an account picker to let the user choose a Google account from the device. // If the GoogleSignInOptions only asks for IDToken and/or profile and/or email then no // consent screen will be shown here. Intent signInIntent = mGoogleSignInClient.getSignInIntent(); startActivityForResult(signInIntent, RC_GET_TOKEN); } private void refreshIdToken() { // Attempt to silently refresh the GoogleSignInAccount. If the GoogleSignInAccount // already has a valid token this method may complete immediately. // // If the user has not previously signed in on this device or the sign-in has expired, // this asynchronous branch will attempt to sign in the user silently and get a valid // ID token. Cross-device single sign on will occur in this branch. mGoogleSignInClient.silentSignIn() .addOnCompleteListener(this, new OnCompleteListener
() { @Override public void onComplete(@NonNull Task
task) { handleSignInResult(task); } }); } // [START handle_sign_in_result] private void handleSignInResult(@NonNull Task
completedTask) { try { GoogleSignInAccount account = completedTask.getResult(ApiException.class); String idToken = account.getIdToken(); // TODO(developer): send ID Token to server and validate updateUI(account); } catch (ApiException e) { Log.w(TAG, "handleSignInResult:error", e); updateUI(null); } } // [END handle_sign_in_result] private void signOut() { mGoogleSignInClient.signOut().addOnCompleteListener(this, new OnCompleteListener
() { @Override public void onComplete(@NonNull Task
task) { updateUI(null); } }); } private void revokeAccess() { mGoogleSignInClient.revokeAccess().addOnCompleteListener(this, new OnCompleteListener
() { @Override public void onComplete(@NonNull Task
task) { updateUI(null); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RC_GET_TOKEN) { // [START get_id_token] // This task is always completed immediately, there is no need to attach an // asynchronous listener. Task
task = GoogleSignIn.getSignedInAccountFromIntent(data); handleSignInResult(task); // [END get_id_token] } } private void updateUI(@Nullable GoogleSignInAccount account) { if (account != null) { ((TextView) findViewById(R.id.status)).setText(R.string.signed_in); String idToken = account.getIdToken(); mIdTokenTextView.setText(getString(R.string.id_token_fmt, idToken)); findViewById(R.id.sign_in_button).setVisibility(View.GONE); findViewById(R.id.sign_out_and_disconnect).setVisibility(View.VISIBLE); mRefreshButton.setVisibility(View.VISIBLE); } else { ((TextView) findViewById(R.id.status)).setText(R.string.signed_out); mIdTokenTextView.setText(getString(R.string.id_token_fmt, "null")); findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE); findViewById(R.id.sign_out_and_disconnect).setVisibility(View.GONE); mRefreshButton.setVisibility(View.GONE); } } /** * Validates that there is a reasonable server client ID in strings.xml, this is only needed * to make sure users of this sample follow the README. */ private void validateServerClientID() { String serverClientId = getString(R.string.server_client_id); String suffix = ".apps.googleusercontent.com"; if (!serverClientId.trim().endsWith(suffix)) { String message = "Invalid server client ID in strings.xml, must end with " + suffix; Log.w(TAG, message); Toast.makeText(this, message, Toast.LENGTH_LONG).show(); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.sign_in_button: getIdToken(); break; case R.id.sign_out_button: signOut(); break; case R.id.disconnect_button: revokeAccess(); break; case R.id.button_optional_action: refreshIdToken(); break; } } }