本文整理汇总了Java中com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken方法的典型用法代码示例。如果您正苦于以下问题:Java GoogleAccountCredential.getToken方法的具体用法?Java GoogleAccountCredential.getToken怎么用?Java GoogleAccountCredential.getToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential
的用法示例。
在下文中一共展示了GoogleAccountCredential.getToken方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: create
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential; //导入方法依赖的package包/类
public static Drive create(Context context, String googleDriveAccount) throws IOException, GoogleAuthException, ImportExportException {
if (googleDriveAccount == null) {
throw new ImportExportException(R.string.google_drive_account_required);
}
try {
List<String> scope = new ArrayList<String>();
scope.add(DriveScopes.DRIVE_FILE);
if (MyPreferences.isGoogleDriveFullReadonly(context)) {
scope.add(DriveScopes.DRIVE_READONLY);
}
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, scope);
credential.setSelectedAccountName(googleDriveAccount);
credential.getToken();
return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
} catch (UserRecoverableAuthException e) {
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent authorizationIntent = e.getIntent();
authorizationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(
Intent.FLAG_FROM_BACKGROUND);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
authorizationIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setTicker(context.getString(R.string.google_drive_permission_requested))
.setContentTitle(context.getString(R.string.google_drive_permission_requested))
.setContentText(context.getString(R.string.google_drive_permission_requested_for_account, googleDriveAccount))
.setContentIntent(pendingIntent).setAutoCancel(true).build();
notificationManager.notify(0, notification);
throw new ImportExportException(R.string.google_drive_permission_required);
}
}
示例2: create
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential; //导入方法依赖的package包/类
public static Drive create(Context context) throws IOException, GoogleAuthException, ImportExportException {
String googleDriveAccount = MyPreferences.getGoogleDriveAccount(context);
if (googleDriveAccount == null) {
throw new ImportExportException(R.string.google_drive_account_required);
}
try {
List<String> scope = new ArrayList<String>();
scope.add(DriveScopes.DRIVE_FILE);
if (MyPreferences.isGoogleDriveFullReadonly(context)) {
scope.add(DriveScopes.DRIVE_READONLY);
}
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, scope);
credential.setSelectedAccountName(googleDriveAccount);
credential.getToken();
return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
} catch (UserRecoverableAuthException e) {
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent authorizationIntent = e.getIntent();
authorizationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(
Intent.FLAG_FROM_BACKGROUND);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
authorizationIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setTicker(context.getString(R.string.google_drive_permission_requested))
.setContentTitle(context.getString(R.string.google_drive_permission_requested))
.setContentText(context.getString(R.string.google_drive_permission_requested_for_account, googleDriveAccount))
.setContentIntent(pendingIntent).setAutoCancel(true).build();
notificationManager.notify(0, notification);
throw new ImportExportException(R.string.google_drive_permission_required);
}
}
示例3: clearToken
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential; //导入方法依赖的package包/类
public void clearToken(GoogleAccountCredential credential) throws IOException {
try {
String token = credential.getToken();
Timber.d("Invalidating %s", token);
GoogleAuthUtil.clearToken(context, token);
GoogleAuthUtil.getTokenWithNotification(context, credential.getSelectedAccount(), "oauth2:" + TasksScopes.TASKS, null);
} catch (GoogleAuthException e) {
Timber.e(e, e.getMessage());
throw new IOException(e);
}
}
示例4: getGoogleAccountCredential
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential; //导入方法依赖的package包/类
/**
* Gets the google account credential.
*
* @param context the context
* @param accountName the account name
* @param scope the scope
*/
public static GoogleAccountCredential getGoogleAccountCredential(
Context context, String accountName, String scope) throws IOException, GoogleAuthException {
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, scope);
credential.setSelectedAccountName(accountName);
credential.getToken();
return credential;
}
示例5: create
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential; //导入方法依赖的package包/类
public static Drive create(Context context, String googleDriveAccount) throws IOException, GoogleAuthException, ImportExportException {
if (googleDriveAccount == null) {
throw new ImportExportException(R.string.google_drive_account_select_error);
}
try {
List<String> scope = new ArrayList<String>();
scope.add(DriveScopes.DRIVE_FILE);
if (MyPreferences.isGoogleDriveFullReadonly(context)) {
scope.add(DriveScopes.DRIVE_READONLY);
}
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, scope);
credential.setSelectedAccountName(googleDriveAccount);
credential.getToken();
return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
} catch (UserRecoverableAuthException e) {
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent authorizationIntent = e.getIntent();
authorizationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(
Intent.FLAG_FROM_BACKGROUND);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
authorizationIntent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setTicker(context.getString(R.string.google_drive_permission_requested))
.setContentTitle(context.getString(R.string.google_drive_permission_requested))
.setContentText(context.getString(R.string.google_drive_permission_requested_for_account) +" " + googleDriveAccount)
.setContentIntent(pendingIntent).setAutoCancel(true).build();
notificationManager.notify(0, notification);
throw new ImportExportException(R.string.google_drive_permission_required);
}
}
示例6: doInBackground
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential; //导入方法依赖的package包/类
@Override
protected Boolean doInBackground(String... emailAccounts) {
Log.i(LOG_TAG, "Background task started.");
if (!AppConstants.checkGooglePlayServicesAvailable(MainActivity.this)) {
return false;
}
String emailAccount = emailAccounts[0];
// Ensure only one task is running at a time.
mAuthTask = this;
// Ensure an email was selected.
if (Strings.isNullOrEmpty(emailAccount)) {
publishProgress(R.string.toast_no_google_account_selected);
// Failure.
return false;
}
if (DEBUG) {
Log.d(LOG_TAG, "Attempting to get AuthToken for account: " + mEmailAccount);
}
try {
// If the application has the appropriate access then a token will be retrieved, otherwise
// an error will be thrown.
GoogleAccountCredential credential = GoogleAccountCredential.usingAudience(
MainActivity.this, AppConstants.AUDIENCE);
credential.setSelectedAccountName(emailAccount);
String accessToken = credential.getToken();
if (DEBUG) {
Log.d(LOG_TAG, "AccessToken retrieved");
}
// Success.
return true;
} catch (GoogleAuthException unrecoverableException) {
Log.e(LOG_TAG, "Exception checking OAuth2 authentication.", unrecoverableException);
publishProgress(R.string.toast_exception_checking_authorization);
// Failure.
return false;
} catch (IOException ioException) {
Log.e(LOG_TAG, "Exception checking OAuth2 authentication.", ioException);
publishProgress(R.string.toast_exception_checking_authorization);
// Failure or cancel request.
return false;
}
}
示例7: getToken
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential; //导入方法依赖的package包/类
/**
* Gets the OAuth2 token.
*
* @param context the context
* @param accountName the account name
* @param scope the scope
*/
public static String getToken(Context context, String accountName, String scope)
throws IOException, GoogleAuthException {
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, scope);
credential.setSelectedAccountName(accountName);
return credential.getToken();
}