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

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

Introduction

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

Prototype

public static GoogleAccountCredential usingOAuth2(Context context, Collection<String> scopes) 

Source Link

Document

Constructs a new instance using OAuth 2.0 scopes.

Usage

From source file:com.github.beardlybread.orgestrator.io.GoogleDriveApi.java

License:Open Source License

/** Set up credential and Google Play Services, and choose Google account.
 *///  www  .  ja  v  a  2s  .com
public void initialize() {
    if (!this.initialized) {
        if (this.credential == null) {
            this.credential = GoogleAccountCredential
                    .usingOAuth2(getContext().getApplicationContext(), Arrays.asList(GoogleDriveApi.SCOPE))
                    .setBackOff(new ExponentialBackOff());
        }
        if (!this.isGooglePlayServicesAvailable()) {
            this.acquireGooglePlayServices();
        } else if (this.credential.getSelectedAccountName() == null) {
            this.chooseAccount();
        } else if (this.deviceIsOffline()) {
            this.showErrorDialog(new Exception(GoogleDriveApi.ERROR_OFFLINE));
        } else {
            this.initialized = true;
        }
    }
}

From source file:com.github.socialc0de.gsw.android.MainActivity.java

License:Apache License

/**
 * Retrieves the previously used account name from the application preferences and checks if the
 * credential object can be set to this account.
 *///w  ww.j av  a  2 s . c o  m
private boolean isSignedIn() {
    credential = GoogleAccountCredential.usingOAuth2(this,
            Collections.singleton(DonateScopes.USERINFO_EMAIL + " " + DonateScopes.PLUS_LOGIN));
    SharedPreferences settings = getSharedPreferences("refugees", 0);
    String accountName = settings.getString(ACCOUNT_NAME_SETTING_NAME, null);
    credential.setSelectedAccountName(accountName);

    return credential.getSelectedAccount() != null;
}

From source file:com.google.adsensequickstart.api.ApiController.java

License:Apache License

public ApiController(Activity activity) {

    this.activity = activity;

    // API credentials
    credential = GoogleAccountCredential.usingOAuth2(activity.getApplicationContext(),
            Collections.singleton(AdSenseScopes.ADSENSE));
    SharedPreferences settings = activity.getPreferences(Context.MODE_PRIVATE);
    credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));

    // Set up AdSense Management API client.
    adsenseService = new AdSense.Builder(httpTransport, jsonFactory, credential)
            .setApplicationName("AdSense Quickstart for Android").build();

}

From source file:com.google.android.apps.watchme.MainActivity.java

License:Apache License

@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ensureFetcher();//w  w  w .  j av  a2s  . c  o m

    credential = GoogleAccountCredential.usingOAuth2(getApplicationContext(), Arrays.asList(Utils.SCOPES));
    // set exponential backoff policy
    credential.setBackOff(new ExponentialBackOff());

    if (savedInstanceState != null) {
        mChosenAccountName = savedInstanceState.getString(ACCOUNT_KEY);
    } else {
        loadAccount();
    }

    credential.setSelectedAccountName(mChosenAccountName);

    mEventsListFragment = (EventsListFragment) getFragmentManager().findFragmentById(R.id.list_fragment);
}

From source file:com.google.cloud.genomics.android.CredentialActivity.java

License:Apache License

private static GoogleAccountCredential getCredential(Context context) {
    GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context,
            Collections.singleton(GenomicsScopes.GENOMICS));
    credential.setSelectedAccountName(SettingsActivity.getAccountName(context));
    return credential;
}

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.drive.samples.crossclientoauth2.MainActivity.java

License:Open Source License

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    mExchangeCodeEditText = (EditText) findViewById(R.id.editTextExchangeCode);
    mIdTokenEditText = (EditText) findViewById(R.id.editTextIdToken);

    // initiate a credential object with drive and plus.login scopes
    // cross identity is only available for tokens retrieved with plus.login
    mCredential = GoogleAccountCredential.usingOAuth2(this, null);

    // user needs to select an account, start account picker
    startActivityForResult(mCredential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}

From source file:com.google.samples.apps.iosched.sync.userdata.http.HTTPUserDataSyncHelper.java

License:Open Source License

/**
 * Private {@code HTTPUserDataSyncHelper} constructor.
 * @param context Context of the application
 *//*from   w  w w.j ava 2 s .  c  om*/
public HTTPUserDataSyncHelper(Context context, String accountName) {
    super(context, accountName);
    mCredentials = GoogleAccountCredential.usingOAuth2(mContext,
            java.util.Arrays.asList(DriveScopes.DRIVE_APPDATA));
    mCredentials.setSelectedAccountName(mAccountName);
}

From source file:com.google.samples.mobiledoctranslate.MainActivity.java

License:Open Source License

/**
 * Attempts to initialize credentials and service object (prior to a call
 * to the API); uses the account provided by the calling app. This
 * requires the GET_ACCOUNTS permission to be explicitly granted by the
 * user; this will be requested here if it is not already granted. The
 * AfterPermissionGranted annotation indicates that this function will be
 * rerun automatically whenever the GET_ACCOUNTS permission is granted.
 *///from  w  ww .ja  va  2  s .c  o  m
@AfterPermissionGranted(REQUEST_PERMISSION_GET_ACCOUNTS)
private void createCredentialsAndService() {
    if (EasyPermissions.hasPermissions(MainActivity.this, Manifest.permission.GET_ACCOUNTS)) {
        mCredential = GoogleAccountCredential.usingOAuth2(getApplicationContext(), Arrays.asList(SCOPES))
                .setBackOff(new ExponentialBackOff()).setSelectedAccountName(mAccount.name);
        mService = new com.google.api.services.script.Script.Builder(mTransport, mJsonFactory,
                setHttpTimeout(mCredential)).setApplicationName(getString(R.string.app_name)).build();
        updateButtonEnableStatus();

        // Callback to retry the API call with valid service/credentials
        callAppsScriptTask(mLastFunctionCalled);
    } else {
        // Request the GET_ACCOUNTS permission via a user dialog
        EasyPermissions.requestPermissions(MainActivity.this, getString(R.string.get_accounts_rationale),
                REQUEST_PERMISSION_GET_ACCOUNTS, Manifest.permission.GET_ACCOUNTS);
    }
}

From source file:com.google.yydw.MainActivity.java

License:Apache License

@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    super.onCreate(savedInstanceState);

    mUploadsListFragment = new UploadsListFragment(getApplicationContext());

    // Check to see if the proper keys and playlist IDs have been set up
    if (!isCorrectlyConfigured()) {
        setContentView(R.layout.developer_setup_required);
        showMissingConfigurations();/*w  ww  .  j av  a2  s  . c  o  m*/
    } else {
        setContentView(R.layout.activity_main);

        ensureLoader();

        credential = GoogleAccountCredential.usingOAuth2(getApplicationContext(), Arrays.asList(Auth.SCOPES));
        // set exponential backoff policy
        credential.setBackOff(new ExponentialBackOff());

        if (savedInstanceState != null) {
            mChosenAccountName = savedInstanceState.getString(ACCOUNT_KEY);
        } else {
            loadAccount();
        }

        credential.setSelectedAccountName(mChosenAccountName);

        mUploadsListFragment = (UploadsListFragment) getFragmentManager().findFragmentById(R.id.list_fragment);

    }
}