Java Code Examples for com.google.android.gms.auth.api.signin.GoogleSignIn#getClient()

The following examples show how to use com.google.android.gms.auth.api.signin.GoogleSignIn#getClient() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GameSyncService.java    From Passbook with Apache License 2.0 9 votes vote down vote up
@Override
public SyncService initialize(Activity context) {
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
            .requestScopes(Drive.SCOPE_APPFOLDER)
            //.requestScopes(Games.SCOPE_GAMES)
            .build();
    mGoogleSignClient = GoogleSignIn.getClient(context, gso);
    mSignInAccount = GoogleSignIn.getLastSignedInAccount(context);
    return this;
}
 
Example 2
Source File: GoogleAccountsProvider.java    From science-journal with Apache License 2.0 9 votes vote down vote up
public GoogleAccountsProvider(Context context) {
  super(context);
  this.context = context;
  setShowSignInActivityIfNotSignedIn(false);
  GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
      .requestEmail()
      .build();
  googleSignInClient = GoogleSignIn.getClient(context, gso);

  GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(context);
  if (googleSignInAccount != null) {
    signInCurrentAccount(googleSignInAccount);
  }
}
 
Example 3
Source File: PlayGames.java    From shortyz with GNU General Public License v3.0 6 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if(thisView != null){
        return thisView;
    }

    this.mGoogleSignInClient = GoogleSignIn.getClient(this.getContext(),
            new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN).build());
    View v = inflater.inflate(R.layout.slide_playgames, container, false);
    initView(v);
    text = (TextView) v.findViewById(R.id.slideText);
    (this.signInButton = v.findViewById(R.id.sign_in_button)).setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            // start the sign-in flow
            signInSilently();
        }

    });
    if(isSignedIn()){
        done();
    } else {
        this.signInButton.setVisibility(View.VISIBLE);
    }
    return v;
}
 
Example 4
Source File: RNGoogleSigninModule.java    From google-signin with MIT License 6 votes vote down vote up
@ReactMethod
public void configure(
        final ReadableMap config,
        final Promise promise
) {
    final ReadableArray scopes = config.hasKey("scopes") ? config.getArray("scopes") : Arguments.createArray();
    final String webClientId = config.hasKey("webClientId") ? config.getString("webClientId") : null;
    final boolean offlineAccess = config.hasKey("offlineAccess") && config.getBoolean("offlineAccess");
    final boolean forceCodeForRefreshToken = config.hasKey("forceCodeForRefreshToken") && config.getBoolean("forceCodeForRefreshToken");
    final String accountName = config.hasKey("accountName") ? config.getString("accountName") : null;
    final String hostedDomain = config.hasKey("hostedDomain") ? config.getString("hostedDomain") : null;

    GoogleSignInOptions options = getSignInOptions(createScopesArray(scopes), webClientId, offlineAccess, forceCodeForRefreshToken, accountName, hostedDomain);
    _apiClient = GoogleSignIn.getClient(getReactApplicationContext(), options);
    promise.resolve(null);
}
 
Example 5
Source File: SignInActivity.java    From codelab-friendlychat-android with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_in);

    // Assign fields
    mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);

    // Set click listeners
    mSignInButton.setOnClickListener(this);

    // Configure Google Sign In
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();
    mSignInClient = GoogleSignIn.getClient(this, gso);

    // Initialize FirebaseAuth
}
 
Example 6
Source File: LoadingActivity.java    From PretendYoureXyzzyAndroid with GNU General Public License v3.0 6 votes vote down vote up
private void signInSilently() {
    GoogleSignInOptions signInOptions = GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN;
    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
    if (GoogleSignIn.hasPermissions(account, signInOptions.getScopeArray())) {
        googleSignedIn(account);
    } else {
        GoogleSignInClient signInClient = GoogleSignIn.getClient(this, signInOptions);
        signInClient.silentSignIn().addOnCompleteListener(this, task -> {
            if (task.isSuccessful()) {
                googleSignedIn(task.getResult());
            } else {
                if (Prefs.getBoolean(PK.SHOULD_PROMPT_GOOGLE_PLAY, true)) {
                    Intent intent = signInClient.getSignInIntent();
                    startActivityForResult(intent, GOOGLE_SIGN_IN_CODE);
                }
            }
        });
    }
}
 
Example 7
Source File: GoogleAuthTest.java    From gdx-fireapp with Apache License 2.0 6 votes vote down vote up
@Test
    public void revokeAccess_ok() {
        // Given
        GoogleAuth googleAuth = new GoogleAuth();
        Task task = Mockito.mock(Task.class);
        final Task task2 = Mockito.mock(Task.class);
        Mockito.when(task2.isSuccessful()).thenReturn(true);
        Mockito.when(googleSignInClient.revokeAccess()).thenReturn(task);
        Mockito.when(task.addOnCompleteListener(Mockito.any(OnCompleteListener.class))).thenAnswer(new Answer<Object>() {
            @Override
            public Object answer(InvocationOnMock invocation) {
                ((OnCompleteListener) invocation.getArgument(0)).onComplete(task2);
                return null;
            }
        });

        // When
        FuturePromise promise = (FuturePromise) spy(googleAuth.revokeAccess().subscribe());

        // Then
        PowerMockito.verifyStatic(GoogleSignIn.class, VerificationModeFactory.times(1));
        GoogleSignIn.getClient((Activity) Mockito.refEq(Gdx.app), Mockito.any(GoogleSignInOptions.class));
        Mockito.verify(googleSignInClient, VerificationModeFactory.times(1)).revokeAccess();
//        Mockito.verify(promise, VerificationModeFactory.times(1)).doComplete(Mockito.isNull());
    }
 
Example 8
Source File: PlayGamesPlugin.java    From play_games with MIT License 6 votes vote down vote up
private void signOut() {
    GoogleSignInOptions.Builder builder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
    GoogleSignInOptions opts = builder.build();
    GoogleSignInClient signInClient = GoogleSignIn.getClient(registrar.activity(), opts);
    signInClient.signOut().addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            currentAccount = null;
            Map<String, Object> successMap = new HashMap<>();
            successMap.put("type", "SUCCESS");
            result(successMap);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            error("ERROR_SIGN_OUT", e);
            Log.i(TAG, "Failed to signout", e);
        }
    });

}
 
Example 9
Source File: GoogleLoginInstance.java    From ShareLoginPayUtil with Apache License 2.0 6 votes vote down vote up
public GoogleLoginInstance(Activity activity, LoginListener listener, boolean fetchUserInfo) {
    super(activity, listener, fetchUserInfo);
    mClientId = ShareManager.CONFIG.getGoogleClientId();
    mClientSecret = ShareManager.CONFIG.getGoogleClientSecret();
    mClient = new OkHttpClient.Builder().build();

    GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(mClientId)
            .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER), new Scope(Scopes.OPEN_ID), new Scope(Scopes.PLUS_ME))
            .requestServerAuthCode(mClientId)
            .requestProfile()
            .requestEmail()
            .build();

    mGoogleSignInClient = GoogleSignIn.getClient(activity, signInOptions);
}
 
Example 10
Source File: AuthUtil.java    From SEAL-Demo with MIT License 6 votes vote down vote up
/**
 * Allows to logout from Google account
 *
 * @param context the activity context
 */
private static void logoutGoogleProvider(final Context context) {
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(context.getString(R.string.googleClientId))
            .requestProfile()
            .build();
    // Build a GoogleSignInClient with the options specified by gso.
    GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(context, gso);
    // Check for existing Google Sign In account, if the user is already signed in
    // the GoogleSignInAccount will be non-null.
    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(context);
    if (account != null) {
        mGoogleSignInClient.signOut()
                .addOnCompleteListener(((Activity) context), new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        ApplicationState.setIsKeysCreated(false);
                        // delete all user info
                        deleteUserTokenAndInfo(context);
                        // load sign in
                        Intent myIntent = new Intent(context, SignInActivity.class);
                        ((Activity) context).startActivityForResult(myIntent, SIGN_IN_ACTIVITY_REQUEST_CODE);
                    }
                });
    }
}
 
Example 11
Source File: GoogleAuthTest.java    From gdx-fireapp with Apache License 2.0 6 votes vote down vote up
@Test
    public void revokeAccess_fail() {
        // Given
        GoogleAuth googleAuth = new GoogleAuth();
        Task task = Mockito.mock(Task.class);
        final Task task2 = Mockito.mock(Task.class);
        Mockito.when(task2.isSuccessful()).thenReturn(false);
        Mockito.when(googleSignInClient.revokeAccess()).thenReturn(task);
        Mockito.when(task.addOnCompleteListener(Mockito.any(OnCompleteListener.class))).thenAnswer(new Answer<Object>() {
            @Override
            public Object answer(InvocationOnMock invocation) {
                ((OnCompleteListener) invocation.getArgument(0)).onComplete(task2);
                return null;
            }
        });

        // When
        FuturePromise promise = (FuturePromise) spy(googleAuth.revokeAccess().silentFail().subscribe());

        // Then
        PowerMockito.verifyStatic(GoogleSignIn.class, VerificationModeFactory.times(1));
        GoogleSignIn.getClient((Activity) Mockito.refEq(Gdx.app), Mockito.any(GoogleSignInOptions.class));
        Mockito.verify(googleSignInClient, VerificationModeFactory.times(1)).revokeAccess();
//        Mockito.verify(promise, VerificationModeFactory.times(1)).doFail(Mockito.nullable(Exception.class));
    }
 
Example 12
Source File: PlayGameServices.java    From PGSGP with MIT License 6 votes vote down vote up
private void initializePlayGameServices(boolean enableSaveGamesFunctionality) {
    GoogleSignInOptions signInOptions = null;
     
    if (enableSaveGamesFunctionality) {
        GoogleSignInOptions.Builder signInOptionsBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
        signInOptionsBuilder.requestScopes(Drive.SCOPE_APPFOLDER).requestId();
        signInOptions = signInOptionsBuilder.build();
    } else {
        signInOptions = GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN;
    }
     
    godotCallbacksUtils = new GodotCallbacksUtils();
    connectionController = new ConnectionController(appActivity, signInOptions, godotCallbacksUtils);
    signInController = new SignInController(appActivity, godotCallbacksUtils, connectionController);
    achievementsController = new AchievementsController(appActivity, connectionController, godotCallbacksUtils);
    leaderboardsController = new LeaderboardsController(appActivity, godotCallbacksUtils, connectionController);
    eventsController = new EventsController(appActivity, connectionController, godotCallbacksUtils);
    playerStatsController = new PlayerStatsController(appActivity, connectionController, godotCallbacksUtils);
    savedGamesController = new SavedGamesController(appActivity, godotCallbacksUtils, connectionController);

    googleSignInClient = GoogleSignIn.getClient(appActivity, signInOptions);
}
 
Example 13
Source File: RemoteBackup.java    From Database-Backup-Restore with Apache License 2.0 5 votes vote down vote up
private GoogleSignInClient buildGoogleSignInClient() {
    GoogleSignInOptions signInOptions =
            new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestScopes(Drive.SCOPE_FILE)
                    .build();
    return GoogleSignIn.getClient(activity, signInOptions);
}
 
Example 14
Source File: DriveSyncService.java    From Passbook with Apache License 2.0 5 votes vote down vote up
@Override
public SyncService initialize(Activity context) {
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestScopes(Drive.SCOPE_APPFOLDER).build();
    mGoogleSignClient = GoogleSignIn.getClient(context, gso);
    mSignInAccount = GoogleSignIn.getLastSignedInAccount(context);
    return this;
}
 
Example 15
Source File: ServerAuthCodeActivity.java    From google-services with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 16
Source File: MainActivity.java    From drive-android-quickstart with Apache License 2.0 5 votes vote down vote up
/** Build a Google SignIn client. */
private GoogleSignInClient buildGoogleSignInClient() {
  GoogleSignInOptions signInOptions =
      new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
          .requestScopes(Drive.SCOPE_FILE)
          .build();
  return GoogleSignIn.getClient(this, signInOptions);
}
 
Example 17
Source File: MainActivity.java    From android-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Starts the sign in process to get the current user and authorize access to Drive.
 */
private void signIn() {
    GoogleSignInOptions signInOptions =
            new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestScopes(Drive.SCOPE_FILE)
                    .build();
    GoogleSignInClient googleSignInClient = GoogleSignIn.getClient(this, signInOptions);
    startActivityForResult(googleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN);
}
 
Example 18
Source File: GoogleSignInActivity.java    From quickstart-android with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBinding = ActivityGoogleBinding.inflate(getLayoutInflater());
    setContentView(mBinding.getRoot());
    setProgressBar(mBinding.progressBar);

    // Button listeners
    mBinding.signInButton.setOnClickListener(this);
    mBinding.signOutButton.setOnClickListener(this);
    mBinding.disconnectButton.setOnClickListener(this);

    // [START config_signin]
    // Configure Google Sign In
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();
    // [END config_signin]

    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    // [START initialize_auth]
    // Initialize Firebase Auth
    mAuth = FirebaseAuth.getInstance();
    // [END initialize_auth]
}
 
Example 19
Source File: GoogleProviderHandler.java    From capacitor-firebase-auth with MIT License 5 votes vote down vote up
@Override
public void init(CapacitorFirebaseAuth plugin) {
    this.plugin = plugin;

    String[] permissions = Config.getArray(CONFIG_KEY_PREFIX + "permissions.google", new String[0]);

    GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
    int result = googleAPI.isGooglePlayServicesAvailable(this.plugin.getContext());
    if (result == ConnectionResult.SUCCESS) {
        Log.d(GOOGLE_TAG, "Google Api is Available.");
    } else {
        Log.w(GOOGLE_TAG, "Google Api is not Available.");
    }

    GoogleSignInOptions.Builder gsBuilder = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(this.plugin.getContext().getString(R.string.default_web_client_id))
            .requestEmail();

    for (String permission : permissions) {
        try {
            gsBuilder.requestScopes(new Scope(permission));
        } catch (Exception e) {
            Log.w(GOOGLE_TAG, String.format("Failure requesting the scope at index %s", permission));
        }
    }

    GoogleSignInOptions gso = gsBuilder.build();
    this.mGoogleSignInClient = GoogleSignIn.getClient(this.plugin.getActivity(), gso);
}
 
Example 20
Source File: GoogleAuthTest.java    From gdx-fireapp with Apache License 2.0 5 votes vote down vote up
@Test
public void signIn() {
    // Given
    // mock GoogleSignInOptions?
    GoogleAuth googleAuth = new GoogleAuth();

    // When
    googleAuth.signIn().subscribe();

    // Then
    Mockito.verify(((AndroidApplication) Gdx.app), VerificationModeFactory.times(1)).startActivityForResult(Mockito.nullable(Intent.class), Mockito.anyInt());
    PowerMockito.verifyStatic(GoogleSignIn.class, VerificationModeFactory.times(1));
    GoogleSignIn.getClient((Activity) Mockito.refEq(Gdx.app), Mockito.any(GoogleSignInOptions.class));
}