Search in sources :

Example 11 with DbxCredential

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

the class DbxClientV2Test method testOnlineWontRefresh.

@Test
public void testOnlineWontRefresh() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withHttpRequestor(mockRequestor).build();
    DbxCredential credential = new DbxCredential("accesstoken");
    DbxClientV2 client = new DbxClientV2(config, credential);
    FileMetadata expected = constructFileMetadate();
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createTokenExpiredResponse());
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    try {
        client.files().getMetadata(expected.getId());
    } catch (InvalidAccessTokenException ex) {
        verify(mockRequestor, times(1)).startPost(anyString(), anyHeaders());
        assertThat(credential.getAccessToken()).isEqualTo("accesstoken");
        AuthError authError = DbxRequestUtil.readJsonFromErrorMessage(AuthError.Serializer.INSTANCE, ex.getMessage(), ex.getRequestId());
        assertThat(authError).isEqualTo(AuthError.EXPIRED_ACCESS_TOKEN);
        return;
    }
    fail("API v2 call should throw exception");
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) AuthError(com.dropbox.core.v2.auth.AuthError) FileMetadata(com.dropbox.core.v2.files.FileMetadata) DbxCredential(com.dropbox.core.oauth.DbxCredential) Test(org.testng.annotations.Test)

Example 12 with DbxCredential

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

the class DbxClientV2Test method testRefreshAndRetryAfterTokenExpired.

@Test
public void testRefreshAndRetryAfterTokenExpired() throws Exception {
    HttpRequestor mockRequestor = mock(HttpRequestor.class);
    DbxRequestConfig config = createRequestConfig().withHttpRequestor(mockRequestor).build();
    long now = System.currentTimeMillis();
    DbxCredential credential = new DbxCredential("accesstoken", now + 2 * DbxCredential.EXPIRE_MARGIN, "refresh_token", "appkey", "app_secret");
    DbxClientV2 client = new DbxClientV2(config, credential);
    FileMetadata expected = constructFileMetadate();
    HttpRequestor.Uploader mockUploader = mockUploader();
    when(mockUploader.finish()).thenReturn(createTokenExpiredResponse()).thenReturn(createSuccessRefreshResponse("new_token", 14400L)).thenReturn(createSuccessResponse(serialize(expected)));
    when(mockRequestor.startPost(anyString(), anyHeaders())).thenReturn(mockUploader);
    Metadata actual = client.files().getMetadata(expected.getId());
    verify(mockRequestor, times(3)).startPost(anyString(), anyHeaders());
    assertThat(credential.getAccessToken()).isEqualTo("new_token");
    assertThat(actual.getName()).isEqualTo(expected.getName());
    assertWithMessage(actual.getClass().toString()).that(actual instanceof FileMetadata).isTrue();
    assertThat(((FileMetadata) actual).getId()).isEqualTo(expected.getId());
}
Also used : HttpRequestor(com.dropbox.core.http.HttpRequestor) FileMetadata(com.dropbox.core.v2.files.FileMetadata) FileMetadata(com.dropbox.core.v2.files.FileMetadata) Metadata(com.dropbox.core.v2.files.Metadata) DbxCredential(com.dropbox.core.oauth.DbxCredential) Test(org.testng.annotations.Test)

Example 13 with DbxCredential

use of com.dropbox.core.oauth.DbxCredential 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 != 1) {
        System.out.println("");
        System.out.println("Usage: COMMAND <auth-file>");
        System.out.println("");
        System.out.println(" <auth-file>: An \"auth file\" that contains the information necessary to make");
        System.out.println("    an authorized Dropbox API request.  Generate this file using the");
        System.out.println("    \"authorize\" example program.");
        System.out.println("");
        System.exit(1);
        return;
    }
    String argAuthFile = args[0];
    // Use DbxCredential instead of DbxAuthInfo.
    DbxCredential credential;
    try {
        credential = DbxCredential.Reader.readFromFile(argAuthFile);
    } catch (JsonReader.FileLoadException ex) {
        System.err.println("Error loading <auth-file>: " + ex.getMessage());
        System.exit(1);
        return;
    }
    // Create a DbxClientV2, which is what you use to make API calls.
    DbxRequestConfig requestConfig = new DbxRequestConfig("examples-account-info");
    // Use DbxCredential to create dbx client.
    DbxClientV2 dbxClient = new DbxClientV2(requestConfig, credential);
    // Make the /account/info API call.
    FullAccount dbxAccountInfo;
    try {
        dbxAccountInfo = dbxClient.users().getCurrentAccount();
    } catch (DbxException ex) {
        System.err.println("Error making API call: " + ex.getMessage());
        System.exit(1);
        return;
    }
    System.out.print(dbxAccountInfo.toStringMultiline());
}
Also used : DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) DbxCredential(com.dropbox.core.oauth.DbxCredential) JsonReader(com.dropbox.core.json.JsonReader) FullAccount(com.dropbox.core.v2.users.FullAccount) DbxException(com.dropbox.core.DbxException)

Aggregations

DbxCredential (com.dropbox.core.oauth.DbxCredential)13 HttpRequestor (com.dropbox.core.http.HttpRequestor)9 FileMetadata (com.dropbox.core.v2.files.FileMetadata)9 Test (org.testng.annotations.Test)9 Metadata (com.dropbox.core.v2.files.Metadata)6 JsonReader (com.dropbox.core.json.JsonReader)2 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 DbxAppInfo (com.dropbox.core.DbxAppInfo)1 DbxAuthFinish (com.dropbox.core.DbxAuthFinish)1 DbxException (com.dropbox.core.DbxException)1 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)1 PkceAuthorize (com.dropbox.core.examples.authorize.PkceAuthorize)1 ScopeAuthorize (com.dropbox.core.examples.authorize.ScopeAuthorize)1 ShortLiveTokenAuthorize (com.dropbox.core.examples.authorize.ShortLiveTokenAuthorize)1 JsonReadException (com.dropbox.core.json.JsonReadException)1 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)1 AuthError (com.dropbox.core.v2.auth.AuthError)1 FullAccount (com.dropbox.core.v2.users.FullAccount)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1