Skip to content
This repository has been archived by the owner on Apr 14, 2024. It is now read-only.

Commit

Permalink
dropbox (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
yschimke committed Feb 11, 2017
1 parent a727d33 commit 9552c8f
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 1 deletion.
@@ -0,0 +1,44 @@
package com.baulsupp.oksocial.services.dropbox;

import com.baulsupp.oksocial.authenticator.AuthUtil;
import com.baulsupp.oksocial.authenticator.SimpleWebServer;
import com.baulsupp.oksocial.authenticator.oauth2.Oauth2Token;
import com.baulsupp.oksocial.output.OutputHandler;
import java.io.IOException;
import java.util.Map;
import okhttp3.Credentials;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class DropboxAuthFlow {
public static Oauth2Token login(OkHttpClient client, OutputHandler outputHandler, String clientId,
String clientSecret) throws IOException {
try (SimpleWebServer<String> s = SimpleWebServer.forCode()) {

String loginUrl = "https://www.dropbox.com/1/oauth2/authorize"
+ "?client_id=" + clientId
+ "&response_type=code"
+ "&redirect_uri=" + s.getRedirectUri();

outputHandler.openLink(loginUrl);

String code = s.waitForCode();

String basic = Credentials.basic(clientId, clientSecret);
FormBody body = new FormBody.Builder().add("code", code)
.add("grant_type", "authorization_code")
.add("redirect_uri", s.getRedirectUri())
.build();
Request request =
new Request.Builder().url("https://api.dropboxapi.com/1/oauth2/token")
.post(body)
.header("Authorization", basic)
.build();

Map<String, Object> responseMap = AuthUtil.makeJsonMapRequest(client, request);

return new Oauth2Token((String) responseMap.get("access_token"));
}
}
}
@@ -0,0 +1,73 @@
package com.baulsupp.oksocial.services.dropbox;

import com.baulsupp.oksocial.authenticator.AuthInterceptor;
import com.baulsupp.oksocial.authenticator.JsonCredentialsValidator;
import com.baulsupp.oksocial.authenticator.ValidatedCredentials;
import com.baulsupp.oksocial.authenticator.oauth2.Oauth2ServiceDefinition;
import com.baulsupp.oksocial.authenticator.oauth2.Oauth2Token;
import com.baulsupp.oksocial.output.OutputHandler;
import com.baulsupp.oksocial.secrets.Secrets;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Future;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static com.baulsupp.oksocial.authenticator.JsonCredentialsValidator.fieldExtractor;

/**
* https://developer.dropbox.com/docs/authentication
*/
public class DropboxAuthInterceptor implements AuthInterceptor<Oauth2Token> {
@Override public Oauth2ServiceDefinition serviceDefinition() {
return new Oauth2ServiceDefinition("api.dropboxapi.com", "Dropbox API", "dropbox",
"https://www.dropbox.com/developers", "https://www.dropbox.com/developers/apps");
}

@Override public Response intercept(Interceptor.Chain chain, Oauth2Token credentials)
throws IOException {
Request request = chain.request();

String token = credentials.accessToken;

Request.Builder builder =
request.newBuilder().addHeader("Authorization", "Bearer " + token);
request = builder.build();

return chain.proceed(request);
}

@Override public Oauth2Token authorize(OkHttpClient client, OutputHandler outputHandler,
List<String> authArguments) throws IOException {
System.err.println("Authorising Dropbox API");

String clientId =
Secrets.prompt("Dropbox Client Id", "dropbox.clientId", "", false);
String clientSecret =
Secrets.prompt("Dropbox Client Secret", "dropbox.clientSecret", "", true);

return DropboxAuthFlow.login(client, outputHandler, clientId, clientSecret);
}

@Override public Future<Optional<ValidatedCredentials>> validate(OkHttpClient client,
Request.Builder requestBuilder, Oauth2Token credentials) throws IOException {
RequestBody body = FormBody.create(MediaType.parse("application/json"), "null");
return new JsonCredentialsValidator(
DropboxUtil.apiRequest("/2/users/get_current_account", requestBuilder)
.newBuilder()
.post(body)
.build(),
fieldExtractor("email")).validate(client);
}

@Override public Set<String> hosts() {
return DropboxUtil.API_HOSTS;
}
}
@@ -0,0 +1,18 @@
package com.baulsupp.oksocial.services.dropbox;

import com.google.common.collect.Sets;
import java.util.Collections;
import java.util.Set;
import okhttp3.Request;

public class DropboxUtil {
private DropboxUtil() {
}

public static final Set<String> API_HOSTS =
Collections.unmodifiableSet(Sets.newHashSet("api.dropboxapi.com", "content.dropboxapi.com"));

public static Request apiRequest(String s, Request.Builder requestBuilder) {
return requestBuilder.url("https://api.dropboxapi.com" + s).build();
}
}
@@ -1,3 +1,4 @@
com.baulsupp.oksocial.services.dropbox.DropboxAuthInterceptor
com.baulsupp.oksocial.services.facebook.FacebookAuthInterceptor
com.baulsupp.oksocial.services.fitbit.FitbitAuthInterceptor
com.baulsupp.oksocial.services.foursquare.FourSquareAuthInterceptor
Expand Down
89 changes: 89 additions & 0 deletions src/main/resources/urls/dropbox.txt
@@ -0,0 +1,89 @@
https://api.dropboxapi.com/2/auth/token/from_oauth1
https://api.dropboxapi.com/2/auth/token/revoke
https://api.dropboxapi.com/2/files/alpha/get_metadata
https://content.dropboxapi.com/2/files/alpha/upload
https://api.dropboxapi.com/2/files/copy_batch
https://api.dropboxapi.com/2/files/copy_batch/check
https://api.dropboxapi.com/2/files/copy_reference/get
https://api.dropboxapi.com/2/files/copy_reference/save
https://api.dropboxapi.com/2/files/copy
https://api.dropboxapi.com/2/files/create_folder
https://api.dropboxapi.com/2/files/delete_batch
https://api.dropboxapi.com/2/files/delete_batch/check
https://api.dropboxapi.com/2/files/delete
https://content.dropboxapi.com/2/files/download
https://api.dropboxapi.com/2/files/get_metadata
https://content.dropboxapi.com/2/files/get_preview
https://api.dropboxapi.com/2/files/get_temporary_link
https://content.dropboxapi.com/2/files/get_thumbnail
https://api.dropboxapi.com/2/files/list_folder
https://api.dropboxapi.com/2/files/list_folder/continue
https://api.dropboxapi.com/2/files/list_folder/get_latest_cursor
https://content.dropboxapi.com/2/files/list_folder/longpoll
https://api.dropboxapi.com/2/files/list_revisions
https://api.dropboxapi.com/2/files/move_batch
https://api.dropboxapi.com/2/files/move_batch/check
https://api.dropboxapi.com/2/files/move
https://api.dropboxapi.com/2/files/permanently_delete
https://api.dropboxapi.com/2/files/properties/add
https://api.dropboxapi.com/2/files/properties/overwrite
https://api.dropboxapi.com/2/files/properties/remove
https://api.dropboxapi.com/2/files/properties/template/get
https://api.dropboxapi.com/2/files/properties/template/list
https://api.dropboxapi.com/2/files/properties/update
https://api.dropboxapi.com/2/files/restore
https://api.dropboxapi.com/2/files/save_url
https://api.dropboxapi.com/2/files/save_url/check_job_status
https://api.dropboxapi.com/2/files/search
https://content.dropboxapi.com/2/files/upload_session/append_v2
https://content.dropboxapi.com/2/files/upload_session/append
https://api.dropboxapi.com/2/files/upload_session/finish_batch
https://api.dropboxapi.com/2/files/upload_session/finish_batch/check
https://content.dropboxapi.com/2/files/upload_session/finish
https://content.dropboxapi.com/2/files/upload_session/start
https://content.dropboxapi.com/2/files/upload
https://api.dropboxapi.com/2/sharing/add_file_member
https://api.dropboxapi.com/2/sharing/add_folder_member
https://api.dropboxapi.com/2/sharing/change_file_member_access
https://api.dropboxapi.com/2/sharing/check_job_status
https://api.dropboxapi.com/2/sharing/check_remove_member_job_status
https://api.dropboxapi.com/2/sharing/check_share_job_status
https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings
https://api.dropboxapi.com/2/sharing/create_shared_link
https://api.dropboxapi.com/2/sharing/get_file_metadata
https://api.dropboxapi.com/2/sharing/get_file_metadata/batch
https://api.dropboxapi.com/2/sharing/get_folder_metadata
https://content.dropboxapi.com/2/sharing/get_shared_link_file
https://api.dropboxapi.com/2/sharing/get_shared_link_metadata
https://api.dropboxapi.com/2/sharing/get_shared_links
https://api.dropboxapi.com/2/sharing/list_file_members
https://api.dropboxapi.com/2/sharing/list_file_members/batch
https://api.dropboxapi.com/2/sharing/list_file_members/continue
https://api.dropboxapi.com/2/sharing/list_folder_members
https://api.dropboxapi.com/2/sharing/list_folder_members/continue
https://api.dropboxapi.com/2/sharing/list_folders
https://api.dropboxapi.com/2/sharing/list_folders/continue
https://api.dropboxapi.com/2/sharing/list_mountable_folders
https://api.dropboxapi.com/2/sharing/list_mountable_folders/continue
https://api.dropboxapi.com/2/sharing/list_received_files
https://api.dropboxapi.com/2/sharing/list_received_files/continue
https://api.dropboxapi.com/2/sharing/list_shared_links
https://api.dropboxapi.com/2/sharing/modify_shared_link_settings
https://api.dropboxapi.com/2/sharing/mount_folder
https://api.dropboxapi.com/2/sharing/relinquish_file_membership
https://api.dropboxapi.com/2/sharing/relinquish_folder_membership
https://api.dropboxapi.com/2/sharing/remove_file_member_2
https://api.dropboxapi.com/2/sharing/remove_file_member
https://api.dropboxapi.com/2/sharing/remove_folder_member
https://api.dropboxapi.com/2/sharing/revoke_shared_link
https://api.dropboxapi.com/2/sharing/share_folder
https://api.dropboxapi.com/2/sharing/transfer_folder
https://api.dropboxapi.com/2/sharing/unmount_folder
https://api.dropboxapi.com/2/sharing/unshare_file
https://api.dropboxapi.com/2/sharing/unshare_folder
https://api.dropboxapi.com/2/sharing/update_folder_member
https://api.dropboxapi.com/2/sharing/update_folder_policy
https://api.dropboxapi.com/2/users/get_account_batch
https://api.dropboxapi.com/2/users/get_account
https://api.dropboxapi.com/2/users/get_current_account
https://api.dropboxapi.com/2/users/get_space_usage
2 changes: 1 addition & 1 deletion src/test/java/com/baulsupp/oksocial/TestMain.java
Expand Up @@ -2,6 +2,6 @@

public class TestMain {
public static void main(String[] args) throws Exception {
Main.main("--debug", "https://api.twitter.com/robots.txt");
Main.main("--show-credentials", "dropbox");
}
}

0 comments on commit 9552c8f

Please sign in to comment.