/** * @param key Dropbox app key (see {@link #getKey}) * @param secret Dropbox app secret (see {@link #getSecret}) */ public DbxAppInfo(String key, String secret) { checkKeyArg(key); checkSecretArg(secret); this.key = key; this.secret = secret; this.host = DbxHost.DEFAULT; }
/** * If they secret's format looks correct, return {@code null}. Otherwise return * a string that describes what the problem is. * * <p> * NOTE: This function only performs some cursory checks on the format. Even if it * returns {@code null} (which means "no problem"), it doesn't mean that what * you passed in is an actual valid Dropbox API app key. * </p> */ public static /*@Nullable*/String getSecretFormatError(String key) { return getTokenPartError(key); }
final String APP_SECRET = "INSERT_APP_SECRET"; DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);
private DbxAuthFinish finish(String code, String redirectUri, final String state) throws DbxException { if (code == null) throw new NullPointerException("code"); Map<String, String> params = new HashMap<String, String>(); params.put("grant_type", "authorization_code"); params.put("code", code); params.put("locale", requestConfig.getUserLocale()); if (redirectUri != null) { params.put("redirect_uri", redirectUri); } List<HttpRequestor.Header> headers = new ArrayList<HttpRequestor.Header>(); DbxRequestUtil.addBasicAuthHeader(headers, appInfo.getKey(), appInfo.getSecret()); return DbxRequestUtil.doPostNoAuth( requestConfig, DbxRawClientV2.USER_AGENT_ID, appInfo.getHost().getApi(), "oauth2/token", DbxRequestUtil.toParamsArray(params), headers, new DbxRequestUtil.ResponseHandler<DbxAuthFinish>() { @Override public DbxAuthFinish handle(HttpRequestor.Response response) throws DbxException { if (response.getStatusCode() != 200) { throw DbxRequestUtil.unexpectedStatus(response); } return DbxRequestUtil.readJsonFromResponse(DbxAuthFinish.Reader, response) .withUrlState(state); } } ); }
private String buildOAuth1Header(DbxOAuth1AccessToken token) { StringBuilder buf = new StringBuilder(); buf.append("OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\""); buf.append(", oauth_consumer_key=\"").append(encode(this.appInfo.getKey())).append("\""); buf.append(", oauth_token=\"").append(encode(token.getKey())).append("\""); buf.append(", oauth_signature=\"").append(encode(this.appInfo.getSecret())).append("&").append(encode(token.getSecret())).append("\""); return buf.toString(); }
private String authorizeImpl(Request request) { Map<String, String> params = new HashMap<String, String>(); params.put("client_id", appInfo.getKey()); params.put("response_type", "code"); if (request.redirectUri != null) { params.put("redirect_uri", request.redirectUri); params.put("state", appendCsrfToken(request)); } if (request.requireRole != null) { params.put("require_role", request.requireRole); } if (request.forceReapprove != null) { params.put("force_reapprove", Boolean.toString(request.forceReapprove).toLowerCase()); } if (request.disableSignup != null) { params.put("disable_signup", Boolean.toString(request.disableSignup).toLowerCase()); } return DbxRequestUtil.buildUrlWithParams( requestConfig.getUserLocale(), appInfo.getHost().getWeb(), "oauth2/authorize", DbxRequestUtil.toParamsArray(params) ); }
@Override public String read(JsonParser parser) throws IOException, JsonReadException { try { String v = parser.getText(); String error = getKeyFormatError(v); if (error != null) { throw new JsonReadException("bad format for app secret: " + error, parser.getTokenLocation()); } parser.nextToken(); return v; } catch (JsonParseException ex) { throw JsonReadException.fromJackson(ex); } }
/** * Tell the Dropbox API server to disable an OAuth 1 access token. */ public void disableOAuth1AccessToken(DbxOAuth1AccessToken token) throws DbxException { if (token == null) throw new IllegalArgumentException("'token' can't be null"); DbxRequestUtil.doPostNoAuth( requestConfig, DbxClientV1.USER_AGENT_ID, appInfo.getHost().getApi(), "1/disable_access_token", null, getHeaders(token), new DbxRequestUtil.ResponseHandler<Void>() { @Override public Void handle(HttpRequestor.Response response) throws DbxException { if (response.getStatusCode() != 200) throw DbxRequestUtil.unexpectedStatus(response); return null; } } ); }
final String APP_SECRET = "supersecret"; // change with yours DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);
@Override public String read(JsonParser parser) throws IOException, JsonReadException { try { String v = parser.getText(); String error = getKeyFormatError(v); if (error != null) { throw new JsonReadException("bad format for app key: " + error, parser.getTokenLocation()); } parser.nextToken(); return v; } catch (JsonParseException ex) { throw JsonReadException.fromJackson(ex); } } };
/** * Given an existing active OAuth 1 access token, make a Dropbox API call to get a new OAuth 2 * access token that represents the same user and app. */ public String createOAuth2AccessToken(DbxOAuth1AccessToken token) throws DbxException { if (token == null) throw new IllegalArgumentException("'token' can't be null"); return DbxRequestUtil.doPostNoAuth( requestConfig, DbxClientV1.USER_AGENT_ID, appInfo.getHost().getApi(), "1/oauth2/token_from_oauth1", null, getHeaders(token), new DbxRequestUtil.ResponseHandler<String>() { @Override public String handle(HttpRequestor.Response response) throws DbxException { if (response.getStatusCode() != 200) throw DbxRequestUtil.unexpectedStatus(response); return DbxRequestUtil.readJsonFromResponse(ResponseReader, response); } } ); }
if (host == null) host = DbxHost.DEFAULT; return new DbxAppInfo(key, secret, host);
/** * @param key Dropbox app key (see {@link #getKey}) * @param secret Dropbox app secret (see {@link #getSecret}) * @param host Dropbox host configuration (see {@link #getHost}) */ public DbxAppInfo(String key, String secret, DbxHost host) { checkKeyArg(key); checkSecretArg(secret); this.key = key; this.secret = secret; this.host = host; }
/** * If they key's format looks correct, return {@code null}. Otherwise return * a string that describes what the problem is. * * <p> * NOTE: This function only performs some cursory checks on the format of the key. * Even if it returns {@code null} (which means "no problem"), it doesn't mean * that what you passed in is an actual valid Dropbox API app key. * </p> */ public static /*@Nullable*/String getKeyFormatError(String key) { return getTokenPartError(key); }
appInfo = new DbxAppInfo(APP_KEY, APP_SECRET); } catch (IllegalArgumentException ex) { JOptionPane.showMessageDialog(null,
public static void checkKeyArg(String key) { String error = getTokenPartError(key); if (error == null) return; throw new IllegalArgumentException("Bad 'key': " + error); }
public static void checkSecretArg(String secret) { String error = getTokenPartError(secret); if (error == null) return; throw new IllegalArgumentException("Bad 'secret': " + error); } }
@Override public String read(JsonParser parser) throws IOException, JsonReadException { try { String v = parser.getText(); String error = DbxAppInfo.getTokenPartError(v); if (error != null) { throw new JsonReadException(error, parser.getTokenLocation()); } parser.nextToken(); return v; } catch (JsonParseException ex) { throw JsonReadException.fromJackson(ex); } }