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

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

Introduction

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

Prototype

public GoogleAccountCredential setBackOff(BackOff backOff) 

Source Link

Document

Sets the back-off policy which is used when an I/O exception is thrown inside #getToken or null for none.

Usage

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.
 *///  w  w  w .jav a  2 s .  co  m
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);
}