Java Code Examples for com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential#setSelectedAccount()

The following examples show how to use com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential#setSelectedAccount() . 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: AndroidGoogleDrive.java    From QtAndroidTools with MIT License 9 votes vote down vote up
public boolean authenticate(String AppName, String ScopeName)
{
    final GoogleSignInAccount SignInAccount = GoogleSignIn.getLastSignedInAccount(mActivityInstance);

    if(SignInAccount != null)
    {
        GoogleAccountCredential AccountCredential;
        Drive.Builder DriveBuilder;

        AccountCredential = GoogleAccountCredential.usingOAuth2(mActivityInstance, Collections.singleton(ScopeName));
        AccountCredential.setSelectedAccount(SignInAccount.getAccount());

        DriveBuilder = new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), AccountCredential);
        DriveBuilder.setApplicationName(AppName);
        mDriveService = DriveBuilder.build();

        return true;
    }

    Log.d(TAG, "You have to signin by select account before use this call!");
    return false;
}
 
Example 2
Source File: BaseMainDbActivity.java    From dbsync with Apache License 2.0 6 votes vote down vote up
private void handleSignInONRecnnect() {
    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);

    if  (account != null ){
        Log.d(TAG, "Signed in as " + account.getEmail());

        // Use the authenticated account to sign in to the Drive service.
        GoogleAccountCredential credential =
                GoogleAccountCredential.usingOAuth2(
                        this, Collections.singleton(DriveScopes.DRIVE_FILE));
        credential.setSelectedAccount(account.getAccount());

        googleDriveService =
                new com.google.api.services.drive.Drive.Builder(
                        AndroidHttp.newCompatibleTransport(),
                        new GsonFactory(),
                        credential)
                        .setApplicationName("Drive API DBSync")
                        .build();
    }
}
 
Example 3
Source File: RestApiActivity.java    From google-services with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Person> doInBackground(Account... accounts) {
    if (mActivityRef.get() == null) {
        return null;
    }

    Context context = mActivityRef.get().getApplicationContext();
    try {
        GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
                context,
                Collections.singleton(CONTACTS_SCOPE));
        credential.setSelectedAccount(accounts[0]);

        PeopleService service = new PeopleService.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName("Google Sign In Quickstart")
                .build();

        ListConnectionsResponse connectionsResponse = service
                .people()
                .connections()
                .list("people/me")
                .setFields("names,emailAddresses")
                .execute();

        return connectionsResponse.getConnections();

    } catch (UserRecoverableAuthIOException recoverableException) {
        if (mActivityRef.get() != null) {
            mActivityRef.get().onRecoverableAuthException(recoverableException);
        }
    } catch (IOException e) {
        Log.w(TAG, "getContacts:exception", e);
    }

    return null;
}