Download File
Download Project
Settings
Line Wrap
Themes
default
ambiance
bespin
dracula
eclipse
material
mbo
mdn-like
neat
solarized dark
ttcn
zenburn
ServerAuthCodeActivity.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.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.Scopes; import com.google.android.gms.common.api.ApiException; import com.google.android.gms.common.api.Scope; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; /** * Demonstrates retrieving an offline access one-time code for the current Google user, which * can be exchanged by your server for an access token and refresh token. */ public class ServerAuthCodeActivity extends AppCompatActivity implements View.OnClickListener { public static final String TAG = "ServerAuthCodeActivity"; private static final int RC_GET_AUTH_CODE = 9003; private GoogleSignInClient mGoogleSignInClient; private TextView mAuthCodeTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Views mAuthCodeTextView = findViewById(R.id.detail); // 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); // For sample only: make sure there is a valid server client ID. validateServerClientID(); // [START configure_signin] // Configure sign-in to request offline access to the user's ID, basic // profile, and Google Drive. The first time you request a code you will // be able to exchange it for an access token and refresh token, which // you should store. In subsequent calls, the code will only result in // an access token. By asking for profile access (through // DEFAULT_SIGN_IN) you will also get an ID Token as a result of the // code exchange. String serverClientId = getString(R.string.server_client_id); GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER)) .requestServerAuthCode(serverClientId) .requestEmail() .build(); // [END configure_signin] mGoogleSignInClient = GoogleSignIn.getClient(this, gso); } private void getAuthCode() { // Start the retrieval process for a server auth code. If requested, ask for a refresh // token. Otherwise, only get an access token if a refresh token has been previously // retrieved. Getting a new access token for an existing grant does not require // user consent. Intent signInIntent = mGoogleSignInClient.getSignInIntent(); startActivityForResult(signInIntent, RC_GET_AUTH_CODE); } 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_AUTH_CODE) { // [START get_auth_code] Task
task = GoogleSignIn.getSignedInAccountFromIntent(data); try { GoogleSignInAccount account = task.getResult(ApiException.class); String authCode = account.getServerAuthCode(); // Show signed-un UI updateUI(account); // TODO(developer): send code to server and exchange for access/refresh/ID tokens } catch (ApiException e) { Log.w(TAG, "Sign-in failed", e); updateUI(null); } // [END get_auth_code] } } private void updateUI(@Nullable GoogleSignInAccount account) { if (account != null) { ((TextView) findViewById(R.id.status)).setText(R.string.signed_in); findViewById(R.id.sign_in_button).setVisibility(View.GONE); findViewById(R.id.sign_out_and_disconnect).setVisibility(View.VISIBLE); String authCode = account.getServerAuthCode(); mAuthCodeTextView.setText(getString(R.string.auth_code_fmt, authCode)); } else { ((TextView) findViewById(R.id.status)).setText(R.string.signed_out); mAuthCodeTextView.setText(getString(R.string.auth_code_fmt, "null")); findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE); findViewById(R.id.sign_out_and_disconnect).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: getAuthCode(); break; case R.id.sign_out_button: signOut(); break; case R.id.disconnect_button: revokeAccess(); break; } } }