public final class

GoogleSignIn

extends Object
java.lang.Object
   ↳ com.google.android.gms.auth.api.signin.GoogleSignIn

Class Overview

Entry point for the Google Sign In API. See GoogleSignInClient.

Summary

Public Methods
static GoogleSignInAccount getAccountForExtension(Context context, GoogleSignInOptionsExtension extension)
Gets a GoogleSignInAccount object to use with other authenticated APIs.
static GoogleSignInAccount getAccountForScopes(Context context, Scope scope, Scope... scopes)
Gets a GoogleSignInAccount object to use with other authenticated APIs.
static GoogleSignInClient getClient(Context context, GoogleSignInOptions options)
Create a new instance of GoogleSignInClient
static GoogleSignInClient getClient(Activity activity, GoogleSignInOptions options)
Create a new instance of GoogleSignInClient
static GoogleSignInAccount getLastSignedInAccount(Context context)
Gets the last account that the user signed in with.
static Task<GoogleSignInAccount> getSignedInAccountFromIntent(Intent data)
Returns a GoogleSignInAccount present in the result data for the associated Activity started via getSignInIntent().
static boolean hasPermissions(GoogleSignInAccount account, GoogleSignInOptionsExtension extension)
Determines if the given account has been granted permission to all scopes associated with the given extension.
static boolean hasPermissions(GoogleSignInAccount account, Scope... scopes)
Determines if the given account has been granted permission to all given scopes.
static void requestPermissions(Activity activity, int requestCode, GoogleSignInAccount account, Scope... scopes)
Requests a collection of permissions to be granted to the given account.
static void requestPermissions(Activity activity, int requestCode, GoogleSignInAccount account, GoogleSignInOptionsExtension extension)
Requests a collection of permissions associated with the given extension to be granted to the given account.
static void requestPermissions(Fragment fragment, int requestCode, GoogleSignInAccount account, GoogleSignInOptionsExtension extension)
static void requestPermissions(Fragment fragment, int requestCode, GoogleSignInAccount account, Scope... scopes)
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public static GoogleSignInAccount getAccountForExtension (Context context, GoogleSignInOptionsExtension extension)

Gets a GoogleSignInAccount object to use with other authenticated APIs. Please specify the additional configurations required by the authenticated API, e.g. FitnessOptions indicating what data types you'd like to access.

Parameters
context Context
extension GoogleSignInOptionsExtension
Returns
GoogleSignInAccount

public static GoogleSignInAccount getAccountForScopes (Context context, Scope scope, Scope... scopes)

Gets a GoogleSignInAccount object to use with other authenticated APIs. Please specify the scope(s) required by the authenticated API.

Parameters
context Context
scope Scope
scopes Scope
Returns
GoogleSignInAccount

public static GoogleSignInClient getClient (Context context, GoogleSignInOptions options)

Create a new instance of GoogleSignInClient

Parameters
context Context: A Context used to provide information about the application's environment.

See also getClient(Activity, GoogleSignInOptions) for GoogleSignInOptions configuration.

options GoogleSignInOptions
Returns
GoogleSignInClient

public static GoogleSignInClient getClient (Activity activity, GoogleSignInOptions options)

Create a new instance of GoogleSignInClient

Parameters
activity Activity: An Activity that will be used to manage the lifecycle of the GoogleSignInClient.
options GoogleSignInOptions: A GoogleSignInOptions used to configure the GoogleSignInClient. It is recommended to build out a GoogleSignInOptions starting with new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)}, configuring either ID token or Server Auth Code options if you have a server. Later, in-context incrementally auth to additional scopes for other Google services access.
Returns
GoogleSignInClient A instance of GoogleSignInClient

public static GoogleSignInAccount getLastSignedInAccount (Context context)

Gets the last account that the user signed in with.

Parameters
context Context
Returns
GoogleSignInAccount GoogleSignInAccount from last known successful sign-in. If user has never signed in before or has signed out / revoked access, null is returned.

public static Task<GoogleSignInAccount> getSignedInAccountFromIntent (Intent data)

Returns a GoogleSignInAccount present in the result data for the associated Activity started via getSignInIntent().

A sample usage:

 try {
   // As documented, we return a completed Task in this case and it's safe to directly call
   // getResult(Class) here (without need to worry about IllegalStateException).
   GoogleSignIn.getSignedInAccountFromIntent(intent).getResult(ApiException.class);
 } catch (ApiException apiException) {
   Log.wtf(TAG, "Unexpected error parsing sign-in result");
 }
 

Parameters
data Intent: the Intent returned via onActivityResult(int, int, Intent) when sign in completed.
Returns
Task<GoogleSignInAccount> A completed Task containing a GoogleSignInAccount object.

See also:

public static boolean hasPermissions (GoogleSignInAccount account, GoogleSignInOptionsExtension extension)

Determines if the given account has been granted permission to all scopes associated with the given extension.

See requestPermissions(Activity, int, GoogleSignInAccount, Scope) for sample code.

Parameters
account GoogleSignInAccount: the account to be checked.
extension GoogleSignInOptionsExtension: the extension to be checked.
Returns
boolean true if the given account has been granted permission to all scopes associated with the given extension.

public static boolean hasPermissions (GoogleSignInAccount account, Scope... scopes)

Determines if the given account has been granted permission to all given scopes.

See requestPermissions(Activity, int, GoogleSignInAccount, GoogleSignInOptionsExtension) for sample code.

Parameters
account GoogleSignInAccount: the account to be checked.
scopes Scope: the collection of scopes to be checked.
Returns
boolean true if the given account has been granted permission to all given scopes.

public static void requestPermissions (Activity activity, int requestCode, GoogleSignInAccount account, Scope... scopes)

Requests a collection of permissions to be granted to the given account. If the account does not have the requested permissions the user will be presented with a UI for accepting them. Once the user has accepted or rejected a response will returned via onActivityResult(int, int, Intent).

A sample usage:


 // Check for your incrementally authed features:
 if (!GoogleSignIn.hasPermissions(
   GoogleSignIn.getLastSignedInAccount(this), Drive.SCOPE_APPFOLDER)) {
   requestPermission(Drive.SCOPE_APPFOLDER, RC_REQUEST_DRIVE_AND_CONTINUE_FILE_CREATION);
 } else {
   createDriveFile();
 }

 void createDriveFile() {
    Drive.getDriveResourceClient(this, GoogleSignIn.getLastSignedInAccount(this))
        .createFile(appFolderRoot, changeSet, newContents);
    ...
 }

 private void requestPermission(Scope scope, String requestCode) {
   GoogleSignIn.requestPermissions(
       this,
       requestCode,
       GoogleSignIn.getLastSignedInAccount(),
       scope);
 }
 // ...
 @Override
 void onActivityResult(int requestCode, int resultCode, Intent intent) {
   if (resultCode == Activity.RESULT_OK) {
     if (RC_REQUEST_DRIVE_AND_CONTINUE_FILE_CREATION == requestCode) {
       createDriveFile();
     }
   }
 }
 

Parameters
activity Activity: the target activity that will receive the response.
requestCode int: code associated with the request. It will match the requestCode associated with the response returned via onActivityResult(int, int, Intent).
account GoogleSignInAccount: the account for which the permissions will be requested. If null the user may have the option to choose.
scopes Scope: the extra collection of scopes to be requested.

public static void requestPermissions (Activity activity, int requestCode, GoogleSignInAccount account, GoogleSignInOptionsExtension extension)

Requests a collection of permissions associated with the given extension to be granted to the given account. If the account does not have the requested permissions the user will be presented with a UI for accepting them. Once the user has accepted or rejected a response will returned via onActivityResult(int, int, Intent).

See also requestPermissions(Activity, int, GoogleSignInAccount, Scope)

A sample usage:


 // Check for your incrementally authed features:
 FitnessOptions fitnessOptions = FitnessOptions.builder()
     .addDataType(DataType.TYPE_STEP_COUNT_CUMULATIVE, FitnessOptions.ACCESS_READ)
     .build();

 if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions)) {
    GoogleSignIn.requestPermissions(
       this,
       RC_REQUEST_STEP_COUNT_AND_CONTINUE_SUBSCRIPTION,
       GoogleSignIn.getLastSignedInAccount(this),
       fitnessOptions);
 } else {
   startSubscription();
 }

 void startSubscription() {
    Fitness.getRecordingClient(this, GoogleSignIn.getLastSignedInAccount())
        .subscribe(DataType.TYPE_STEP_COUNT_CUMULATIVE);
    ...
 }

 @Override
 void onActivityResult(int requestCode, int resultCode, Intent intent) {
   if (resultCode == Activity.RESULT_OK) {
     if (RC_REQUEST_STEP_COUNT_AND_CONTINUE_SUBSCRIPTION == requestCode) {
       startSubscription();
     }
   }
 }
 

Parameters
activity Activity: the target activity that will receive the response.
requestCode int: code associated with the request. It will match the requestCode associated with the response returned via onActivityResult(int, int, Intent).
account GoogleSignInAccount: the account for which the permissions will be requested. If null the user may have the option to choose.
extension GoogleSignInOptionsExtension: the extension associated with a set of permissions to be requested.

public static void requestPermissions (Fragment fragment, int requestCode, GoogleSignInAccount account, GoogleSignInOptionsExtension extension)

See requestPermissions(Activity, int, GoogleSignInAccount, GoogleSignInOptionsExtension).

Parameters
fragment Fragment: the fragment to launch permission resolution Intent from.
requestCode int
account GoogleSignInAccount
extension GoogleSignInOptionsExtension

public static void requestPermissions (Fragment fragment, int requestCode, GoogleSignInAccount account, Scope... scopes)

See requestPermissions(Activity, int, GoogleSignInAccount, Scope)

Parameters
fragment Fragment: the fragment to launch permission resolution Intent from.
requestCode int
account GoogleSignInAccount
scopes Scope