/** * Obtain a new instance of DbxClient and store it in configuration. */ public void createClient() { DbxRequestConfig config = new DbxRequestConfig(clientIdentifier, Locale.getDefault().toString()); this.client = new DbxClientV2(config, accessToken); }
private static File downloadRemoteFile(String revision, String remotePath, String localPath) { DbxClientV2 client = getDbxClient(); FileOutputStream outputStream = null; File outputFile = new File(localPath); try { outputStream = new FileOutputStream(outputFile); client.files().download(remotePath, revision).download(outputStream); } catch (DbxException | IOException ex) { Exceptions.printStackTrace(ex); } finally { IOUtils.closeQuietly(outputStream); } return outputFile; }
private void verifyCredentials(ResultBuilder builder, Map<String, Object> parameters) { String token = (String) parameters.get("accessToken"); String clientId = (String) parameters.get("clientIdentifier"); try { // Create Dropbox client DbxRequestConfig config = new DbxRequestConfig(clientId, Locale.getDefault().toString()); DbxClientV2 client = new DbxClientV2(config, token); client.users().getCurrentAccount(); } catch (DbxException e) { builder.error(ResultErrorBuilder .withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION, "Invalid client identifier and/or access token") .parameterKey("accessToken").parameterKey("clientIdentifier").build()); } }
private FileMetadata dropBoxSave(String uploadPath, InputStream inputStream)throws Exception{ DbxRequestConfig config = new DbxRequestConfig(APP_IDENTIFIER); DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN); return client.files().uploadBuilder(uploadPath).uploadAndFinish(inputStream); }
private void displayCloudStatus() { String message = "Working locally."; String displayName; // Check Dropbox connection DbxClientV2 client = DbxUtil.getDbxClient(); if (client != null) { try { displayName = client.users().getCurrentAccount().getName().getDisplayName(); message = "Connected to Dropbox account as " + displayName + "."; Cloud.getInstance().setStatus(Cloud.Status.DBX_CONNECTED, " (" + displayName + ")"); } catch (DbxException ex) { // simply stay working locally. Cloud.getInstance().setStatus(Cloud.Status.DISCONNECTED); } } LOGGER.log(message); }
public void deAuth() { MyPreferences.removeDropboxKeys(context); if (dropboxClient != null) { try { dropboxClient.auth().tokenRevoke(); } catch (DbxException e) { Log.e("Financisto", "Unable to unlink Dropbox", e); } } }
@Override public void writeTo(OutputStream outputStream) { try { String fileStr = SEPARATOR + imageDir + SEPARATOR + id + SEPARATOR + fileName; DbxRequestConfig config = new DbxRequestConfig(APP_IDENTIFIER); DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN); client.files().download(fileStr).download(outputStream); } catch (Exception e) { logger.error(e.getMessage()); throw new ResourceNotFoundException("Image Not Found : " + id + "/" + fileName); } } };
String additional = ""; if(client != null) { additional = " (" + client.users().getCurrentAccount().getName().getDisplayName() + ")";
client.auth().tokenRevoke();
@Override protected Integer doInBackground(Void... params) { try { ListFolderResult filderContents = DropboxClientFactory.getClient().files().listFolder(""); dropBoxEntries = filderContents.getEntries(); return 0; } catch (InvalidAccessTokenException e) { Log.e(TAG, "InvalidAccessTokenException downloading database", e); return ERROR_DROPBOX_INVALID_TOKEN; } catch (DbxException e) { Log.e(TAG, "DbxException downloading database", e); return ERROR_DROPBOX; } }
/** * Returns a new {@link DbxClientV2} that performs requests against Dropbox API * user endpoints relative to a namespace without including the namespace as * part of the path variable for every request. * (<a href="https://www.dropbox.com/developers/reference/namespace-guide#pathrootmodes">https://www.dropbox.com/developers/reference/namespace-guide#pathrootmodes</a>). * * <p> This method performs no validation of the namespace ID. </p> * * @param pathRoot the path root for this client, never {@code null}. * * @return Dropbox client that issues requests with Dropbox-API-Path-Root header. * * @throws IllegalArgumentException If {@code pathRoot} is {@code null} */ public DbxClientV2 withPathRoot(PathRoot pathRoot) { if (pathRoot == null) { throw new IllegalArgumentException("'pathRoot' should not be null"); } return new DbxClientV2(_client.withPathRoot(pathRoot)); }
private void verifyCredentials(ResultBuilder builder, Map<String, Object> parameters) { String token = (String) parameters.get("accessToken"); String clientId = (String) parameters.get("clientIdentifier"); try { // Create Dropbox client DbxRequestConfig config = new DbxRequestConfig(clientId, Locale.getDefault().toString()); DbxClientV2 client = new DbxClientV2(config, token); client.users().getCurrentAccount(); client = null; } catch (Exception e) { builder.error(ResultErrorBuilder .withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION, "Invalid client identifier and/or access token") .parameterKey("accessToken").parameterKey("clientIdentifier").build()); } }
private FileMetadata putSingleFile(File inputFile, String dropboxPath, DropboxUploadMode mode) throws Exception { FileInputStream inputStream = new FileInputStream(inputFile); FileMetadata uploadedFile; try { WriteMode uploadMode; if (mode == DropboxUploadMode.force) { uploadMode = WriteMode.OVERWRITE; } else { uploadMode = WriteMode.ADD; } uploadedFile = client.files().uploadBuilder(dropboxPath).withMode(uploadMode).uploadAndFinish(inputStream, inputFile.length()); return uploadedFile; } finally { IOHelper.close(inputStream); } }
public static DbxClientV2 getDbxClient() { String accessToken = (String) ApplicationSettings.Setting.DROPBOX_TOKEN.getValue(); if (accessToken == null) { JOptionPane.showMessageDialog(null, "The authentication token has not been set.\n" + "Please connect the application to your Dropbox account first!", "Dropbox authentication token not found", JOptionPane.ERROR_MESSAGE ); return null; } return new DbxClientV2(getDbxConfig(), accessToken); }
private Map<String, Object> downloadFilesInFolder(String path) throws DropboxException { try { ListFolderResult folderResult = client.files().listFolder(path.equals("/") ? "" : path); Map<String, Object> returnMap = new LinkedHashMap<>(); for (Metadata entry : folderResult.getEntries()) { returnMap.put(entry.getPathDisplay(), downloadSingleFile(entry.getPathDisplay()).getValue()); } return returnMap; } catch (ListFolderErrorException e) { try { DbxDownloader<FileMetadata> listing = client.files().download(path); if (listing == null) { return Collections.emptyMap(); } else { LOG.debug("downloading a single file..."); Map.Entry<String, Object> entry = downloadSingleFile(path); return Collections.singletonMap(entry.getKey(), entry.getValue()); } } catch (DbxException dbxException) { throw new DropboxException(dbxException); } } catch (DbxException e) { throw new DropboxException(e); } }
@Override public ResponseEntity<StreamingResponseBody> readFile(String fileLocation, String imageDir, String id, String fileName) { StreamingResponseBody streamingResponseBody = new StreamingResponseBody() { @Override public void writeTo(OutputStream outputStream) { try { String fileStr = SEPARATOR + imageDir + SEPARATOR + id + SEPARATOR + fileName; DbxRequestConfig config = new DbxRequestConfig(APP_IDENTIFIER); DbxClientV2 client = new DbxClientV2(config, ACCESS_TOKEN); client.files().download(fileStr).download(outputStream); } catch (Exception e) { logger.error(e.getMessage()); throw new ResourceNotFoundException("Image Not Found : " + id + "/" + fileName); } } }; HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_TYPE, "image/*"); return new ResponseEntity<StreamingResponseBody>(streamingResponseBody, headers, HttpStatus.OK); }
outputStream = new FileOutputStream(file); DropboxClientFactory.getClient() .files() .download("/" + fileName[0]) .download(outputStream);
/** * Returns a {@link DbxClientV2} that performs requests against Dropbox API * user endpoints as the given team member. * * <p> This method performs no validation of the team member ID. </p> * * @param memberId Team member ID of member in this client's team, never * {@code null}. * * @return Dropbox client that issues requests to user endpoints as the * given team member * * @throws IllegalArgumentException If {@code memberId} is {@code null} */ public DbxClientV2 asMember(String memberId) { if (memberId == null) { throw new IllegalArgumentException("'memberId' should not be null"); } DbxRawClientV2 asMemberClient = new DbxTeamRawClientV2( _client.getRequestConfig(), _client.getHost(), accessToken, _client.getUserId(), memberId, null, null ); return new DbxClientV2(asMemberClient); }
/** * Rename a remote path with the new path location. * * @param remotePath the existing remote path to be renamed * @param newRemotePath the new remote path substituting the old one * @return a result object with the result of the move operation. * @throws DropboxException */ public DropboxMoveResult move(String remotePath, String newRemotePath) throws DropboxException { try { client.files().moveV2(remotePath, newRemotePath); return new DropboxMoveResult(remotePath, newRemotePath); } catch (DbxException e) { throw new DropboxException(remotePath + " does not exist or cannot obtain metadata", e); } }
/** * Returns a {@link DbxClientV2} that performs requests against Dropbox API * user endpoints as the given team admin. * * <p> This method performs no validation of the team admin ID. </p> * * @param adminId Team member ID of the admin in client's team, never * {@code null}. * * @return Dropbox client that issues requests to user endpoints as the * given team Admin. * * @throws IllegalArgumentException If {@code adminId} is {@code null} */ public DbxClientV2 asAdmin(String adminId) { if (adminId == null) { throw new IllegalArgumentException("'adminId' should not be null"); } DbxRawClientV2 asAdminClient = new DbxTeamRawClientV2( _client.getRequestConfig(), _client.getHost(), accessToken, _client.getUserId(), null, adminId, null ); return new DbxClientV2(asAdminClient); }