|
| 1 | +package com.dropbox.core.examples.upgrade_oauth1_token; |
| 2 | + |
| 3 | +import com.dropbox.core.*; |
| 4 | +import com.dropbox.core.json.JsonReader; |
| 5 | + |
| 6 | +import java.io.*; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Locale; |
| 9 | +import java.util.logging.Level; |
| 10 | +import java.util.logging.Logger; |
| 11 | + |
| 12 | +/** |
| 13 | + * An example command-line application that converts the given OAuth 1 access |
| 14 | + * token to an OAuth 2 access token and then disables the original OAuth 1 access |
| 15 | + * token. |
| 16 | + */ |
| 17 | +public class Main |
| 18 | +{ |
| 19 | + public static void main(String[] args) |
| 20 | + throws IOException |
| 21 | + { |
| 22 | + // Only display important log messages. |
| 23 | + Logger.getLogger("").setLevel(Level.INFO); |
| 24 | + |
| 25 | + Config cfg = parseArgs(args); |
| 26 | + if (cfg == null) { |
| 27 | + System.exit(1); return; |
| 28 | + } |
| 29 | + |
| 30 | + // Read app info file (contains app key and app secret) |
| 31 | + DbxAppInfo appInfo; |
| 32 | + try { |
| 33 | + appInfo = DbxAppInfo.Reader.readFromFile(cfg.appInfoFile); |
| 34 | + } |
| 35 | + catch (JsonReader.FileLoadException ex) { |
| 36 | + System.err.println("Error reading <app-info-file>: " + ex.getMessage()); |
| 37 | + System.exit(1); return; |
| 38 | + } |
| 39 | + |
| 40 | + DbxOAuth1AccessToken oauth1AccessToken = |
| 41 | + new DbxOAuth1AccessToken(cfg.accessTokenKey, cfg.accessTokenSecret); |
| 42 | + |
| 43 | + // Get an OAuth 2 access token. |
| 44 | + String userLocale = Locale.getDefault().toString(); |
| 45 | + DbxRequestConfig requestConfig = new DbxRequestConfig("examples-authorize", userLocale); |
| 46 | + DbxOAuth1Upgrader upgrader = new DbxOAuth1Upgrader(requestConfig, appInfo); |
| 47 | + |
| 48 | + String oauth2AccessToken ; |
| 49 | + try { |
| 50 | + oauth2AccessToken = upgrader.createOAuth2AccessToken(oauth1AccessToken); |
| 51 | + } |
| 52 | + catch (DbxException ex) { |
| 53 | + System.err.println("Error getting OAuth 2 access token: " + ex.getMessage()); |
| 54 | + System.exit(1); return; |
| 55 | + } |
| 56 | + |
| 57 | + System.out.println("OAuth 2 access token obtained."); |
| 58 | + DbxAuthInfo authInfo = new DbxAuthInfo(oauth2AccessToken, appInfo.host); |
| 59 | + DbxAuthInfo.Writer.writeToStream(authInfo, System.out); |
| 60 | + System.out.println(); |
| 61 | + |
| 62 | + // Disable the OAuth 1 access token. |
| 63 | + if (cfg.disable) { |
| 64 | + try { |
| 65 | + upgrader.disableOAuth1AccessToken(oauth1AccessToken); |
| 66 | + } |
| 67 | + catch (DbxException ex) { |
| 68 | + System.err.println("Error disabling OAuth 1 access token: " + ex.getMessage()); |
| 69 | + System.exit(1); return; |
| 70 | + } |
| 71 | + System.out.println("OAuth 1 access token disabled."); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private static void printHelp(PrintStream out) |
| 76 | + { |
| 77 | + out.println("Usage: COMMAND <app-info-file> <oa1-access-token-key> <oa1-access-token-secret>"); |
| 78 | + out.println(""); |
| 79 | + out.println("<app-info-file>: a JSON file with information about your API app. Example:"); |
| 80 | + out.println(""); |
| 81 | + out.println(" {"); |
| 82 | + out.println(" \"key\": \"Your Dropbox API app key...\","); |
| 83 | + out.println(" \"secret\": \"Your Dropbox API app secret...\""); |
| 84 | + out.println(" }"); |
| 85 | + out.println(""); |
| 86 | + out.println(" Get an API app key by registering with Dropbox:"); |
| 87 | + out.println(" https://dropbox.com/developers/apps"); |
| 88 | + out.println(""); |
| 89 | + out.println("<oa1-access-token-key>: The OAuth 1 access token key."); |
| 90 | + out.println(""); |
| 91 | + out.println("<oa1-access-token-secret>: The OAuth 1 access token secret."); |
| 92 | + out.println(""); |
| 93 | + out.println("Options:"); |
| 94 | + out.println(""); |
| 95 | + out.println(" --disable: After we get an OAuth 2 access token, tell the server to"); |
| 96 | + out.println(" disable the OAuth 1 access token."); |
| 97 | + out.println(""); |
| 98 | + } |
| 99 | + |
| 100 | + private static final class Config |
| 101 | + { |
| 102 | + public final String appInfoFile; |
| 103 | + public final String accessTokenKey; |
| 104 | + public final String accessTokenSecret; |
| 105 | + public final boolean disable; |
| 106 | + |
| 107 | + public Config(String appInfoFile, String accessTokenKey, String accessTokenSecret, boolean disable) |
| 108 | + { |
| 109 | + this.appInfoFile = appInfoFile; |
| 110 | + this.accessTokenKey = accessTokenKey; |
| 111 | + this.accessTokenSecret = accessTokenSecret; |
| 112 | + this.disable = disable; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private static Config parseArgs(String[] args) |
| 117 | + { |
| 118 | + if (args.length == 0) { |
| 119 | + printHelp(System.out); |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + ArrayList<String> remainingArgs = new ArrayList<String>(); |
| 124 | + boolean optionsAllowed = true; |
| 125 | + boolean disable = false; |
| 126 | + boolean sawDisable = false; |
| 127 | + for (int i = 0; i < args.length; i++) { |
| 128 | + String arg = args[i]; |
| 129 | + if (optionsAllowed && arg.startsWith("-")) { |
| 130 | + if (arg.equals("--")) { |
| 131 | + optionsAllowed = false; |
| 132 | + continue; |
| 133 | + } |
| 134 | + if (arg.equals("--disable")) { |
| 135 | + if (sawDisable) { |
| 136 | + System.err.println("Option \"--disable\" used more than once."); |
| 137 | + return null; |
| 138 | + } |
| 139 | + sawDisable = true; |
| 140 | + disable = true; |
| 141 | + } |
| 142 | + else { |
| 143 | + System.err.println("Unrecognized option \"" + arg + "\"."); |
| 144 | + System.err.println("Run with no arguments for help."); |
| 145 | + return null; |
| 146 | + } |
| 147 | + } else { |
| 148 | + remainingArgs.add(arg); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if (remainingArgs.size() != 3) { |
| 153 | + System.err.println("Expecting at least 3 non-option arguments, got " + remainingArgs.size() + "."); |
| 154 | + System.err.println("Run with no arguments for help."); |
| 155 | + return null; |
| 156 | + } |
| 157 | + |
| 158 | + return new Config(remainingArgs.get(0), remainingArgs.get(1), remainingArgs.get(2), disable); |
| 159 | + } |
| 160 | +} |
0 commit comments