Example usage for com.google.api.client.googleapis.extensions.android.gms.auth GoogleAccountCredential setSelectedAccountName

List of usage examples for com.google.api.client.googleapis.extensions.android.gms.auth GoogleAccountCredential setSelectedAccountName

Introduction

In this page you can find the example usage for com.google.api.client.googleapis.extensions.android.gms.auth GoogleAccountCredential setSelectedAccountName.

Prototype

public final GoogleAccountCredential setSelectedAccountName(String accountName) 

Source Link

Document

Sets the selected Google account name (e-mail address) -- for example "johndoe@gmail.com" -- or null for none.

Usage

From source file:com.google.devrel.samples.compute.android.AppUtils.java

License:Open Source License

public static Compute getComputeServiceObject(Context context, String emailAddress) {
    List scopes = Lists.newArrayList(ComputeScopes.COMPUTE, ComputeScopes.DEVSTORAGE_READ_ONLY);

    // Utilize the Android credential type. This will give you problems if you haven't
    // registered the android application within the developer console (see README file).
    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, scopes);
    // Tell the credential which Google account(email) to use.
    credential.setSelectedAccountName(emailAddress);

    // Create Google Compute Engine API query object.
    return new Compute.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
            .setApplicationName(AppConstants.COMPUTE_ENGINE_ANDROID_SAMPLE_APP_NAME).build();
}

From source file:com.google.devrel.samples.helloendpoints.MainActivity.java

License:Open Source License

/**
 * This method is invoked when the "Get Authenticated Greeting" button is clicked. See
 * activity_main.xml for the dynamic reference to this method.
 *//*w ww  . j  a  va 2 s .c o m*/
public void onClickGetAuthenticatedGreeting(View unused) {
    if (!isSignedIn()) {
        Toast.makeText(this, "You must sign in for this action.", Toast.LENGTH_LONG).show();
        return;
    }

    // Use of an anonymous class is done for sample code simplicity. {@code AsyncTasks} should be
    // static-inner or top-level classes to prevent memory leak issues.
    // @see http://goo.gl/fN1fuE @26:00 for an great explanation.
    AsyncTask<Void, Void, HelloGreeting> getAuthedGreetingAndDisplay = new AsyncTask<Void, Void, HelloGreeting>() {
        @Override
        protected HelloGreeting doInBackground(Void... unused) {
            if (!isSignedIn()) {
                return null;
            }
            ;

            if (!AppConstants.checkGooglePlayServicesAvailable(MainActivity.this)) {
                return null;
            }

            // Create a Google credential since this is an authenticated request to the API.
            GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(MainActivity.this,
                    AppConstants.AUDIENCE);
            credential.setSelectedAccountName(mEmailAccount);

            // Retrieve service handle using credential since this is an authenticated call.
            Helloworld apiServiceHandle = AppConstants.getApiServiceHandle(credential);

            try {
                Authed getAuthedGreetingCommand = apiServiceHandle.greetings().authed();
                HelloGreeting greeting = getAuthedGreetingCommand.execute();
                return greeting;
            } catch (IOException e) {
                Log.e(LOG_TAG, "Exception during API call", e);
            }
            return null;
        }

        @Override
        protected void onPostExecute(HelloGreeting greeting) {
            if (greeting != null) {
                displayGreetings(greeting);
            } else {
                Log.e(LOG_TAG, "No greetings were returned by the API.");
            }
        }
    };

    getAuthedGreetingAndDisplay.execute((Void) null);
}

From source file:com.google.devrel.samples.memedroid.app.Constants.java

License:Apache License

/**
 * Retrieve a GoogleAccountCredential used to authorise requests made to the cloud endpoints
 * backend./*from   w  w  w  .j  a  v a2s .  co  m*/
 *
 * @param context application context
 * @param accountName the account selected
 * @return
 */
public static GoogleAccountCredential getCredential(Context context, String accountName) {
    GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(context,
            "server:client_id:" + Constants.SERVER_CLIENTID);
    // Small workaround to avoid setting an account that doesn't exist, so we can test.
    if (!TEST_EMAIL_ADDRESS.equals(accountName)) {
        credential.setSelectedAccountName(accountName);
    }
    return credential;
}

From source file:com.google.training.cpd200.conference.android.utils.ConferenceUtils.java

License:Open Source License

/**
 * Build and returns an instance of {@link com.appspot.cpd200_extras.conference.Conference}
 *
 * @param context/*from  w  w  w  .j  a  va2 s.com*/
 * @param email
 * @return
 */
public static com.appspot.cpd200_extras.conference.Conference buildServiceHandler(Context context,
        String email) {
    GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(context, AppConstants.AUDIENCE);
    credential.setSelectedAccountName(email);

    com.appspot.cpd200_extras.conference.Conference.Builder builder = new com.appspot.cpd200_extras.conference.Conference.Builder(
            AppConstants.HTTP_TRANSPORT, AppConstants.JSON_FACTORY, credential);
    builder.setApplicationName("conference-central-server");
    return builder.build();
}

From source file:com.jefftharris.passwdsafe.sync.gdrive.GDriveProvider.java

License:Open Source License

/**
 * Retrieve a authorized service object to send requests to the Google
 * Drive API. On failure to retrieve an access token, a notification is
 * sent to the user requesting that authorization be granted for the
 * {@code https://www.googleapis.com/auth/drive} scope.
 *
 * @return An authorized service object and its auth token.
 *///from w w  w.j  a v  a 2 s  . c om
private static Pair<Drive, String> getDriveService(Account acct, Context ctx) {
    Drive drive = null;
    String token = null;
    try {
        GoogleAccountCredential credential = getAcctCredential(ctx);
        credential.setBackOff(new ExponentialBackOff());
        credential.setSelectedAccountName(acct.name);

        token = GoogleAuthUtil.getTokenWithNotification(ctx, acct, credential.getScope(), null,
                PasswdSafeContract.AUTHORITY, null);

        Drive.Builder builder = new Drive.Builder(AndroidHttp.newCompatibleTransport(),
                JacksonFactory.getDefaultInstance(), credential);
        builder.setApplicationName(ctx.getString(R.string.app_name));
        drive = builder.build();
    } catch (UserRecoverableNotifiedException e) {
        // User notified
        PasswdSafeUtil.dbginfo(TAG, e, "User notified auth exception");
        try {
            GoogleAuthUtil.clearToken(ctx, null);
        } catch (Exception ioe) {
            Log.e(TAG, "getDriveService clear failure", e);
        }
    } catch (GoogleAuthException e) {
        // Unrecoverable
        Log.e(TAG, "Unrecoverable auth exception", e);
    } catch (IOException e) {
        // Transient
        PasswdSafeUtil.dbginfo(TAG, e, "Transient error");
    } catch (Exception e) {
        Log.e(TAG, "Token exception", e);
    }
    return new Pair<>(drive, token);
}

From source file:com.piusvelte.cloudset.android.ActionsIntentService.java

License:Open Source License

private void publish(String action, List<Extra> extras) {
    SharedPreferences sp = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);
    String accountName = sp.getString(CloudSetMain.PREFERENCE_ACCOUNT_NAME, null);
    Long deviceId = sp.getLong(CloudSetMain.PREFERENCE_DEVICE_ID, CloudSetMain.INVALID_DEVICE_ID);

    if (accountName != null && !deviceId.equals(CloudSetMain.INVALID_DEVICE_ID)) {
        Action publication = new Action();
        publication.setPublisher(deviceId);
        publication.setName(action);//from   w w  w.  ja v  a 2  s .  c  o  m
        publication.setExtras(extras);

        GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(this,
                "server:client_id:" + getString(R.string.android_audience));
        credential.setSelectedAccountName(accountName);

        Actionendpoint.Builder endpointBuilder = new Actionendpoint.Builder(
                AndroidHttp.newCompatibleTransport(), new JacksonFactory(), credential)
                        .setApplicationName(getString(R.string.app_name));
        endpoint = CloudEndpointUtils.updateBuilder(endpointBuilder).build();

        (new AsyncTask<Action, Void, Void>() {

            @Override
            protected Void doInBackground(Action... publications) {
                try {
                    endpoint.actionEndpoint().publish(publications[0]).execute();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                sWakeLock.release();
            }

        }).execute(publication);
    } else {
        sWakeLock.release();
    }
}

From source file:com.piusvelte.cloudset.android.DevicesLoader.java

License:Open Source License

private void initEndpoint(String account) {
    Context globalContext = getContext();
    GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(
            globalContext.getApplicationContext(),
            "server:client_id:" + globalContext.getString(R.string.android_audience));
    credential.setSelectedAccountName(account);
    Deviceendpoint.Builder endpointBuilder = new Deviceendpoint.Builder(AndroidHttp.newCompatibleTransport(),
            new JacksonFactory(), credential).setApplicationName(globalContext.getString(R.string.app_name));
    deviceendpoint = CloudEndpointUtils.updateBuilder(endpointBuilder).build();
}

From source file:com.piusvelte.cloudset.android.GCMIntentService.java

License:Open Source License

private Deviceendpoint getEndpoint(Context context) {
    if (endpoint == null) {
        String accountName = null;
        SharedPreferences sp = context.getSharedPreferences(context.getString(R.string.app_name), MODE_PRIVATE);

        if (sp.contains(CloudSetMain.PREFERENCE_ACCOUNT_NAME)) {
            accountName = sp.getString(CloudSetMain.PREFERENCE_ACCOUNT_NAME, null);
        }/* w ww.  j  av a  2  s. c  o m*/

        GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(context,
                "server:client_id:" + context.getString(R.string.android_audience));
        credential.setSelectedAccountName(accountName);

        Deviceendpoint.Builder endpointBuilder = new Deviceendpoint.Builder(
                AndroidHttp.newCompatibleTransport(), new JacksonFactory(), credential)
                        .setApplicationName(getString(R.string.app_name));
        endpoint = CloudEndpointUtils.updateBuilder(endpointBuilder).build();
    }

    return endpoint;
}

From source file:com.taesua.admeet.admeet.ConferenceUtils.java

License:Open Source License

/**
 * Build and returns an instance of {@link com.appspot.ad_meet.conference.Conference}
 *
 * @param context//from   w  w  w. j a  v a  2 s.  c o  m
 * @param email
 * @return
 */

public static com.appspot.ad_meet.conference.Conference buildServiceHandler(Context context, String email) {
    GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(context, Ids.AUDIENCE);
    credential.setSelectedAccountName(email);

    com.appspot.ad_meet.conference.Conference.Builder builder = new com.appspot.ad_meet.conference.Conference.Builder(
            Ids.HTTP_TRANSPORT, Ids.JSON_FACTORY, credential);
    builder.setApplicationName("AdMeet");
    return builder.build();
}

From source file:com.udacity.devrel.training.conference.android.utils.ConferenceUtils.java

License:Open Source License

/**
 * Build and returns an instance of {@link com.appspot.your_app_id.conference.Conference}
 *
 * @param context//  ww w.ja  v a 2 s.  co  m
 * @param email
 * @return
 */
public static com.appspot.your_app_id.conference.Conference buildServiceHandler(Context context, String email) {
    GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(context, AppConstants.AUDIENCE);
    credential.setSelectedAccountName(email);

    com.appspot.your_app_id.conference.Conference.Builder builder = new com.appspot.your_app_id.conference.Conference.Builder(
            AppConstants.HTTP_TRANSPORT, AppConstants.JSON_FACTORY, credential);
    builder.setApplicationName("conference-central-server");
    return builder.build();
}