List of usage examples for android.accounts AccountManager blockingGetAuthToken
public String blockingGetAuthToken(Account account, String authTokenType, boolean notifyAuthFailure) throws OperationCanceledException, IOException, AuthenticatorException
From source file:ir.keloud.android.lib.common.accounts.AccountUtils.java
/** * /*w ww .j a va 2s .c o m*/ * @return * @throws IOException * @throws AuthenticatorException * @throws OperationCanceledException */ public static KeloudCredentials getCredentialsForAccount(Context context, Account account) throws OperationCanceledException, AuthenticatorException, IOException { KeloudCredentials credentials = null; AccountManager am = AccountManager.get(context); boolean isOauth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; boolean isSamlSso = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; if (isOauth2) { String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), false); credentials = KeloudCredentialsFactory.newBearerCredentials(accessToken); } else if (isSamlSso) { String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type), false); credentials = KeloudCredentialsFactory.newSamlSsoCredentials(accessToken); } else { String username = account.name.substring(0, account.name.lastIndexOf('@')); String password = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypePass(account.type), false); credentials = KeloudCredentialsFactory.newBasicCredentials(username, password); } return credentials; }
From source file:com.owncloud.android.lib.common.accounts.AccountUtils.java
/** * /*from w w w .java 2s .co m*/ * @return * @throws IOException * @throws AuthenticatorException * @throws OperationCanceledException */ public static OwnCloudCredentials getCredentialsForAccount(Context context, Account account) throws OperationCanceledException, AuthenticatorException, IOException { OwnCloudCredentials credentials = null; AccountManager am = AccountManager.get(context); boolean isOauth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; boolean isSamlSso = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; if (isOauth2) { String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), false); credentials = OwnCloudCredentialsFactory.newBearerCredentials(accessToken); } else if (isSamlSso) { String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type), false); credentials = OwnCloudCredentialsFactory.newSamlSsoCredentials(accessToken); } else { String username = account.name.substring(0, account.name.lastIndexOf('@')); String password = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypePass(account.type), false); credentials = OwnCloudCredentialsFactory.newBasicCredentials(username, password); } return credentials; }
From source file:com.owncloud.android.network.OwnCloudClientUtils.java
/** * Creates a WebdavClient setup for an ownCloud account * /*from ww w . j a v a2 s . c om*/ * Do not call this method from the main thread. * * @param account The ownCloud account * @param appContext Android application context * @return A WebdavClient object ready to be used * @throws AuthenticatorException If the authenticator failed to get the authorization token for the account. * @throws OperationCanceledException If the authenticator operation was cancelled while getting the authorization token for the account. * @throws IOException If there was some I/O error while getting the authorization token for the account. * @throws AccountNotFoundException If 'account' is unknown for the AccountManager */ public static WebdavClient createOwnCloudClient(Account account, Context appContext) throws OperationCanceledException, AuthenticatorException, IOException, AccountNotFoundException { //Log_OC.d(TAG, "Creating WebdavClient associated to " + account.name); Uri uri = Uri.parse(AccountUtils.constructFullURLForAccount(appContext, account)); AccountManager am = AccountManager.get(appContext); boolean isOauth2 = am.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_OAUTH2) != null; // TODO avoid calling to getUserData here boolean isSamlSso = am.getUserData(account, AccountAuthenticator.KEY_SUPPORTS_SAML_WEB_SSO) != null; WebdavClient client = createOwnCloudClient(uri, appContext, !isSamlSso); if (isOauth2) { String accessToken = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_ACCESS_TOKEN, false); client.setBearerCredentials(accessToken); // TODO not assume that the access token is a bearer token } else if (isSamlSso) { // TODO avoid a call to getUserData here String accessToken = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_SAML_WEB_SSO_SESSION_COOKIE, false); client.setSsoSessionCookie(accessToken); } else { String username = account.name.substring(0, account.name.lastIndexOf('@')); //String password = am.getPassword(account); String password = am.blockingGetAuthToken(account, AccountAuthenticator.AUTH_TOKEN_TYPE_PASSWORD, false); client.setBasicCredentials(username, password); } return client; }
From source file:com.cerema.cloud2.lib.common.accounts.AccountUtils.java
/** * /*from w ww . j a v a2 s . c o m*/ * @return * @throws IOException * @throws AuthenticatorException * @throws OperationCanceledException */ public static OwnCloudCredentials getCredentialsForAccount(Context context, Account account) throws OperationCanceledException, AuthenticatorException, IOException { OwnCloudCredentials credentials = null; AccountManager am = AccountManager.get(context); boolean isOauth2 = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_OAUTH2) != null; boolean isSamlSso = am.getUserData(account, AccountUtils.Constants.KEY_SUPPORTS_SAML_WEB_SSO) != null; String username = account.name.substring(0, account.name.lastIndexOf('@')); if (isOauth2) { String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeAccessToken(account.type), false); credentials = OwnCloudCredentialsFactory.newBearerCredentials(accessToken); } else if (isSamlSso) { String accessToken = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypeSamlSessionCookie(account.type), false); credentials = OwnCloudCredentialsFactory.newSamlSsoCredentials(username, accessToken); } else { String password = am.blockingGetAuthToken(account, AccountTypeUtils.getAuthTokenTypePass(account.type), false); credentials = OwnCloudCredentialsFactory.newBasicCredentials(username, password); } return credentials; }
From source file:com.deliciousdroid.client.DeliciousApi.java
/** * Performs an api call to Delicious's http based api methods. * /* w w w . j a v a 2s.c o m*/ * @param url URL of the api method to call. * @param params Extra parameters included in the api call, as specified by different methods. * @param account The account being synced. * @param context The current application context. * @return A String containing the response from the server. * @throws IOException If a server error was encountered. * @throws AuthenticationException If an authentication error was encountered. */ private static InputStream DeliciousApiCall(String url, TreeMap<String, String> params, Account account, Context context) throws IOException, AuthenticationException { final AccountManager am = AccountManager.get(context); if (account == null) throw new AuthenticationException(); final String username = account.name; String authtoken = null; try { authtoken = am.blockingGetAuthToken(account, Constants.AUTHTOKEN_TYPE, false); } catch (OperationCanceledException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (AuthenticatorException e) { // TODO Auto-generated catch block e.printStackTrace(); } Uri.Builder builder = new Uri.Builder(); builder.scheme(SCHEME); builder.authority(DELICIOUS_AUTHORITY); builder.appendEncodedPath(url); for (String key : params.keySet()) { builder.appendQueryParameter(key, params.get(key)); } String apiCallUrl = builder.build().toString(); Log.d("apiCallUrl", apiCallUrl); final HttpGet post = new HttpGet(apiCallUrl); post.setHeader("User-Agent", "DeliciousDroid"); post.setHeader("Accept-Encoding", "gzip"); DefaultHttpClient client = (DefaultHttpClient) HttpClientFactory.getThreadSafeClient(); CredentialsProvider provider = client.getCredentialsProvider(); Credentials credentials = new UsernamePasswordCredentials(username, authtoken); provider.setCredentials(SCOPE, credentials); client.addRequestInterceptor(new PreemptiveAuthInterceptor(), 0); final HttpResponse resp = client.execute(post); final int statusCode = resp.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { final HttpEntity entity = resp.getEntity(); InputStream instream = entity.getContent(); final Header encoding = entity.getContentEncoding(); if (encoding != null && encoding.getValue().equalsIgnoreCase("gzip")) { instream = new GZIPInputStream(instream); } return instream; } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) { throw new AuthenticationException(); } else { throw new IOException(); } }
From source file:com.ocp.picasa.PicasaApi.java
public static AuthAccount[] getAuthenticatedAccounts(Context context) { AccountManager accountManager = AccountManager.get(context); Account[] accounts = getAccounts(context); if (accounts == null) accounts = new Account[0]; int numAccounts = accounts.length; ArrayList<AuthAccount> authAccounts = new ArrayList<AuthAccount>(numAccounts); for (int i = 0; i != numAccounts; ++i) { Account account = accounts[i];//from w ww . j a v a 2 s . c o m String authToken; try { // Get the token without user interaction. authToken = accountManager.blockingGetAuthToken(account, PicasaService.SERVICE_NAME, true); // TODO: Remove this once the build is signed by Google, since // we will always have permission. // This code requests permission from the user explicitly. if (context instanceof Activity) { Bundle bundle = accountManager .getAuthToken(account, PicasaService.SERVICE_NAME, null, (Activity) context, null, null) .getResult(); authToken = bundle.getString("authtoken"); PicasaService.requestSync(context, PicasaService.TYPE_USERS_ALBUMS, -1); } // Add the account information to the list of accounts. if (authToken != null) { String username = canonicalizeUsername(account.name); authAccounts.add(new AuthAccount(username, authToken, account)); } } catch (OperationCanceledException e) { } catch (IOException e) { } catch (AuthenticatorException e) { } catch (Exception e) { ; } } AuthAccount[] authArray = new AuthAccount[authAccounts.size()]; authAccounts.toArray(authArray); return authArray; }
From source file:com.pindroid.client.PinboardApi.java
/** * Performs an api call to Pinboard's http based api methods. * /* www . ja v a2s . co m*/ * @param url URL of the api method to call. * @param params Extra parameters included in the api call, as specified by different methods. * @param account The account being synced. * @param context The current application context. * @return A String containing the response from the server. * @throws IOException If a server error was encountered. * @throws AuthenticationException If an authentication error was encountered. * @throws TooManyRequestsException * @throws PinboardException */ private static InputStream PinboardApiCall(String url, TreeMap<String, String> params, Account account, Context context) throws IOException, AuthenticationException, TooManyRequestsException, PinboardException { final AccountManager am = AccountManager.get(context); if (account == null) throw new AuthenticationException(); final String username = account.name; String authtoken = "00000000000000000000"; // need to provide a sane default value, since a token that is too short causes a 500 error instead of 401 try { String tempAuthtoken = am.blockingGetAuthToken(account, Constants.AUTHTOKEN_TYPE, true); if (tempAuthtoken != null) authtoken = tempAuthtoken; } catch (Exception e) { e.printStackTrace(); throw new AuthenticationException("Error getting auth token"); } params.put("auth_token", username + ":" + authtoken); final Uri.Builder builder = new Uri.Builder(); builder.scheme(SCHEME); builder.authority(PINBOARD_AUTHORITY); builder.appendEncodedPath(url); for (String key : params.keySet()) { builder.appendQueryParameter(key, params.get(key)); } String apiCallUrl = builder.build().toString(); Log.d("apiCallUrl", apiCallUrl); final HttpGet post = new HttpGet(apiCallUrl); post.setHeader("User-Agent", "PinDroid"); post.setHeader("Accept-Encoding", "gzip"); final DefaultHttpClient client = (DefaultHttpClient) HttpClientFactory.getThreadSafeClient(); final HttpResponse resp = client.execute(post); final int statusCode = resp.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { final HttpEntity entity = resp.getEntity(); InputStream instream = entity.getContent(); final Header encoding = entity.getContentEncoding(); if (encoding != null && encoding.getValue().equalsIgnoreCase("gzip")) { instream = new GZIPInputStream(instream); } return instream; } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) { am.invalidateAuthToken(Constants.AUTHTOKEN_TYPE, authtoken); try { authtoken = am.blockingGetAuthToken(account, Constants.AUTHTOKEN_TYPE, true); } catch (Exception e) { e.printStackTrace(); throw new AuthenticationException("Invalid auth token"); } throw new AuthenticationException(); } else if (statusCode == Constants.HTTP_STATUS_TOO_MANY_REQUESTS) { throw new TooManyRequestsException(300); } else if (statusCode == HttpStatus.SC_REQUEST_URI_TOO_LONG) { throw new PinboardException(); } else { throw new IOException(); } }
From source file:com.nononsenseapps.notepad.sync.googleapi.GoogleAPITalker.java
private static String getAuthToken(AccountManager accountManager, Account account, String authTokenType, boolean notifyAuthFailure) { Log.d(TAG, "getAuthToken"); String authToken = ""; try {/*from w ww. j a v a2 s . c o m*/ // Might be invalid in the cache authToken = accountManager.blockingGetAuthToken(account, authTokenType, notifyAuthFailure); accountManager.invalidateAuthToken("com.google", authToken); authToken = accountManager.blockingGetAuthToken(account, authTokenType, notifyAuthFailure); } catch (OperationCanceledException ignored) { } catch (AuthenticatorException ignored) { } catch (IOException ignored) { } return authToken; }
From source file:com.deliciousdroid.syncadapter.ContactSyncAdapter.java
@Override public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {//from ww w . j a v a 2s . co m List<User> users; List<Status> statuses; try { final AccountManager am = AccountManager.get(mContext); authtoken = am.blockingGetAuthToken(account, Constants.AUTHTOKEN_TYPE, false); // fetch updates from the sample service over the cloud users = DeliciousFeed.fetchFriendUpdates(account); // update platform contacts. Log.d(TAG, "Calling contactManager's sync contacts"); ContactManager.syncContacts(mContext, account.name, users); // fetch and update status messages for all the synced users. if (Build.VERSION.SDK_INT >= 15) { ContactManager.insertStreamStatuses(mContext, account.name); } else { statuses = DeliciousFeed.fetchFriendStatuses(account); ContactManager.insertStatuses(mContext, account.name, statuses); } } catch (final IOException e) { Log.e(TAG, "IOException", e); syncResult.stats.numIoExceptions++; } catch (final AuthenticationException e) { mAccountManager.invalidateAuthToken(Constants.ACCOUNT_TYPE, authtoken); syncResult.stats.numAuthExceptions++; Log.e(TAG, "AuthenticationException", e); } catch (final ParseException e) { syncResult.stats.numParseExceptions++; Log.e(TAG, "ParseException", e); } catch (final JSONException e) { syncResult.stats.numParseExceptions++; Log.e(TAG, "JSONException", e); } catch (final FeedForbiddenException e) { Log.e(TAG, "FeedForbiddenException"); } catch (final AuthenticatorException e) { Log.e(TAG, "AuthenticatorException"); } catch (final OperationCanceledException e) { Log.e(TAG, "OperationCanceledException"); } }
From source file:org.klnusbaum.udj.ArtistsLoader.java
private ArtistsResult attemptLoad(boolean attemptReauth) { AccountManager am = AccountManager.get(getContext()); String authToken = ""; try {/*w ww . jav a 2 s. c om*/ authToken = am.blockingGetAuthToken(account, "", true); } catch (IOException e) { //TODO this might actually be an auth error return new ArtistsResult(null, ArtistsError.AUTHENTICATION_ERROR); } catch (AuthenticatorException e) { return new ArtistsResult(null, ArtistsError.AUTHENTICATION_ERROR); } catch (OperationCanceledException e) { return new ArtistsResult(null, ArtistsError.AUTHENTICATION_ERROR); } try { String playerId = am.getUserData(account, Constants.LAST_PLAYER_ID_DATA); return getArtists(playerId, authToken); } catch (JSONException e) { return new ArtistsResult(null, ArtistsError.SERVER_ERROR); } catch (ParseException e) { return new ArtistsResult(null, ArtistsError.SERVER_ERROR); } catch (IOException e) { return new ArtistsResult(null, ArtistsError.SERVER_ERROR); } catch (AuthenticationException e) { if (attemptReauth) { Log.d(TAG, "soft auth failure"); am.invalidateAuthToken(Constants.ACCOUNT_TYPE, authToken); return attemptLoad(false); } else { Log.d(TAG, "hard auth failure"); return new ArtistsResult(null, ArtistsError.AUTHENTICATION_ERROR); } } catch (PlayerInactiveException e) { return new ArtistsResult(null, ArtistsError.PLAYER_INACTIVE_ERROR); } catch (NoLongerInPlayerException e) { return new ArtistsResult(null, ArtistsError.NO_LONGER_IN_PLAYER_ERROR); } catch (KickedException e) { return new ArtistsResult(null, ArtistsError.KICKED_ERROR); } }