GoogleSignIn

public final class GoogleSignIn extends Object

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

Public Method Summary

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
static void

Inherited Method Summary

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.

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.

public static GoogleSignInClient getClient (Context context, GoogleSignInOptions options)

Create a new instance of GoogleSignInClient

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

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

options

public static GoogleSignInClient getClient (Activity activity, GoogleSignInOptions options)

Create a new instance of GoogleSignInClient

Parameters
activity An Activity that will be used to manage the lifecycle of the GoogleSignInClient.
options 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

public static GoogleSignInAccount getLastSignedInAccount (Context context)

Gets the last account that the user signed in with.

Returns
  • 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 the Intent returned via onActivityResult(int, int, Intent) when sign in completed.
Returns

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 the account to be checked.
extension the extension to be checked.
Returns
  • 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 the account to be checked.
scopes the collection of scopes to be checked.
Returns
  • 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 the target activity that will receive the response.
requestCode code associated with the request. It will match the requestCode associated with the response returned via onActivityResult(int, int, Intent).
account the account for which the permissions will be requested. If null the user may have the option to choose.
scopes 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 the target activity that will receive the response.
requestCode code associated with the request. It will match the requestCode associated with the response returned via onActivityResult(int, int, Intent).
account the account for which the permissions will be requested. If null the user may have the option to choose.
extension the extension associated with a set of permissions to be requested.

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

Parameters
fragment the fragment to launch permission resolution Intent from.
requestCode
account
extension

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

Parameters
fragment the fragment to launch permission resolution Intent from.
requestCode
account
scopes