Search in sources :

Example 6 with DbxRequestConfig

use of com.dropbox.core.DbxRequestConfig in project dropbox-sdk-java by dropbox.

the class DropboxClientFactory method init.

public static void init(String accessToken) {
    if (sDbxClient == null) {
        DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder("examples-v2-demo").withHttpRequestor(new OkHttp3Requestor(OkHttp3Requestor.defaultOkHttpClient())).build();
        sDbxClient = new DbxClientV2(requestConfig, accessToken);
    }
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) OkHttp3Requestor(com.dropbox.core.http.OkHttp3Requestor)

Example 7 with DbxRequestConfig

use of com.dropbox.core.DbxRequestConfig in project syndesis-qe by syndesisio.

the class DropBoxUtils method getClient.

private DbxClientV2 getClient() throws DbxException {
    if (this.client == null) {
        Optional<Account> optional = AccountsDirectory.getInstance().getAccount("QE Dropbox");
        if (optional.isPresent()) {
            DbxRequestConfig config = new DbxRequestConfig(optional.get().getProperty("clientIdentifier"));
            this.client = new DbxClientV2(config, optional.get().getProperty("accessToken"));
        } else {
            log.error("Unable to create DropBox client - credentials not found.");
            throw new IllegalArgumentException("DropBox credentials were not found.");
        }
        log.debug("DropBox client created, logged as: " + client.users().getCurrentAccount());
    } else {
        log.debug("DropBox client was already created, returning existing instance");
    }
    return this.client;
}
Also used : Account(io.syndesis.qe.accounts.Account) DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig)

Example 8 with DbxRequestConfig

use of com.dropbox.core.DbxRequestConfig in project dropbox-sdk-java by dropbox.

the class Main method main.

public static void main(String[] args) throws IOException {
    // Only display important log messages.
    Logger.getLogger("").setLevel(Level.WARNING);
    if (args.length != 2) {
        System.out.println("Usage: COMMAND <app-info-file> <auth-file-output>");
        System.out.println("");
        System.out.println("<app-info-file>: a JSON file with information about your API app.  Example:");
        System.out.println("");
        System.out.println("  {");
        System.out.println("    \"key\": \"Your Dropbox API app key...\",");
        System.out.println("    \"secret\": \"Your Dropbox API app secret...\"");
        System.out.println("  }");
        System.out.println("");
        System.out.println("  Get an API app key by registering with Dropbox:");
        System.out.println("    https://dropbox.com/developers/apps");
        System.out.println("");
        System.out.println("<auth-file-output>: If authorization is successful, the resulting API");
        System.out.println("  access token will be saved to this file, which can then be used with");
        System.out.println("  other example programs, such as the one in \"examples/account-info\".");
        System.out.println("");
        System.exit(1);
        return;
    }
    String argAppInfoFile = args[0];
    String argAuthFileOutput = args[1];
    // Read app info file (contains app key and app secret)
    DbxAppInfo appInfo;
    try {
        appInfo = DbxAppInfo.Reader.readFromFile(argAppInfoFile);
    } catch (JsonReader.FileLoadException ex) {
        System.err.println("Error reading <app-info-file>: " + ex.getMessage());
        System.exit(1);
        return;
    }
    // Run through Dropbox API authorization process
    DbxRequestConfig requestConfig = new DbxRequestConfig("examples-authorize");
    DbxWebAuth webAuth = new DbxWebAuth(requestConfig, appInfo);
    DbxWebAuth.Request webAuthRequest = DbxWebAuth.newRequestBuilder().withNoRedirect().build();
    String authorizeUrl = webAuth.authorize(webAuthRequest);
    System.out.println("1. Go to " + authorizeUrl);
    System.out.println("2. Click \"Allow\" (you might have to log in first).");
    System.out.println("3. Copy the authorization code.");
    System.out.print("Enter the authorization code here: ");
    String code = new BufferedReader(new InputStreamReader(System.in)).readLine();
    if (code == null) {
        System.exit(1);
        return;
    }
    code = code.trim();
    DbxAuthFinish authFinish;
    try {
        authFinish = webAuth.finishFromCode(code);
    } catch (DbxException ex) {
        System.err.println("Error in DbxWebAuth.authorize: " + ex.getMessage());
        System.exit(1);
        return;
    }
    System.out.println("Authorization complete.");
    System.out.println("- User ID: " + authFinish.getUserId());
    System.out.println("- Account ID: " + authFinish.getAccountId());
    System.out.println("- Access Token: " + authFinish.getAccessToken());
    // Save auth information to output file.
    DbxAuthInfo authInfo = new DbxAuthInfo(authFinish.getAccessToken(), appInfo.getHost());
    File output = new File(argAuthFileOutput);
    try {
        DbxAuthInfo.Writer.writeToFile(authInfo, output);
        System.out.println("Saved authorization information to \"" + output.getCanonicalPath() + "\".");
    } catch (IOException ex) {
        System.err.println("Error saving to <auth-file-out>: " + ex.getMessage());
        System.err.println("Dumping to stderr instead:");
        DbxAuthInfo.Writer.writeToStream(authInfo, System.err);
        System.exit(1);
        return;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) DbxAuthInfo(com.dropbox.core.DbxAuthInfo) DbxAppInfo(com.dropbox.core.DbxAppInfo) BufferedReader(java.io.BufferedReader) JsonReader(com.dropbox.core.json.JsonReader) DbxAuthFinish(com.dropbox.core.DbxAuthFinish) File(java.io.File) DbxException(com.dropbox.core.DbxException) DbxWebAuth(com.dropbox.core.DbxWebAuth)

Example 9 with DbxRequestConfig

use of com.dropbox.core.DbxRequestConfig in project dropbox-sdk-java by dropbox.

the class Main method createClient.

/**
 * Create a new Dropbox client using the given authentication
 * information and HTTP client config.
 *
 * @param auth Authentication information
 * @param config HTTP request configuration
 *
 * @return new Dropbox V2 client
 */
private static DbxClientV2 createClient(DbxAuthInfo auth, StandardHttpRequestor.Config config) {
    String clientUserAgentId = "examples-longpoll";
    StandardHttpRequestor requestor = new StandardHttpRequestor(config);
    DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder(clientUserAgentId).withHttpRequestor(requestor).build();
    return new DbxClientV2(requestConfig, auth.getAccessToken(), auth.getHost());
}
Also used : StandardHttpRequestor(com.dropbox.core.http.StandardHttpRequestor) DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig)

Example 10 with DbxRequestConfig

use of com.dropbox.core.DbxRequestConfig in project dropbox-sdk-java by dropbox.

the class DbxClientV2Test method testRetrySuccess.

@Test
public void testRetrySuccess() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withAutoRetryEnabled(3).withHttpRequestor(mockRequestor).build();
    DbxClientV2 client = new DbxClientV2(config, "fakeAccessToken");
    FileMetadata expected = constructFileMetadate();
    // 503 twice, then return result
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createEmptyResponse(503)).thenReturn(createEmptyResponse(503)).thenReturn(createSuccessResponse(serialize(expected)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    Metadata actual = client.files().getMetadata(expected.getId());
    // should have only been called 3 times: initial call + 2 retries
    verify(mockRequestor, times(3)).startPost(anyString(), anyHeaders());
    assertEquals(actual.getName(), expected.getName());
    assertTrue(actual instanceof FileMetadata, actual.getClass().toString());
    assertEquals(((FileMetadata) actual).getId(), expected.getId());
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) FileMetadata(com.dropbox.core.v2.files.FileMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) Test(org.testng.annotations.Test)

Aggregations

DbxRequestConfig (com.dropbox.core.DbxRequestConfig)26 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)12 HttpRequestor (com.dropbox.core.http.HttpRequestor)11 Test (org.testng.annotations.Test)11 DbxAuthInfo (com.dropbox.core.DbxAuthInfo)5 DbxException (com.dropbox.core.DbxException)5 JsonReader (com.dropbox.core.json.JsonReader)5 FileMetadata (com.dropbox.core.v2.files.FileMetadata)4 Metadata (com.dropbox.core.v2.files.Metadata)3 DbxAppInfo (com.dropbox.core.DbxAppInfo)2 FullAccount (com.dropbox.core.v2.users.FullAccount)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 DbxAuthFinish (com.dropbox.core.DbxAuthFinish)1 DbxClient (com.dropbox.core.DbxClient)1 DbxOAuth1AccessToken (com.dropbox.core.DbxOAuth1AccessToken)1 DbxOAuth1Upgrader (com.dropbox.core.DbxOAuth1Upgrader)1 DbxWebAuth (com.dropbox.core.DbxWebAuth)1 OkHttp3Requestor (com.dropbox.core.http.OkHttp3Requestor)1