forked from dropbox/dropbox-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
109 lines (96 loc) · 4.13 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.dropbox.core.examples.authorize;
import com.dropbox.core.*;
import com.dropbox.core.json.JsonReader;
import java.io.*;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* An example command-line application that runs through the web-based OAuth
* flow (using {@link DbxWebAuth}).
*/
public class Main
{
public static void main(String[] args)
throws IOException
{
// Only display important log messages.
Logger.getLogger("").setLevel(Level.WARNING);
if (args.length == 0) {
printHelp(System.out);
return;
}
if (args.length != 2) {
System.err.println("Expecting exactly 2 arguments, got " + args.length + ".");
System.err.println("Run with no arguments for help.");
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
String userLocale = Locale.getDefault().toString();
DbxRequestConfig requestConfig = new DbxRequestConfig("examples-authorize", userLocale);
DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(requestConfig, appInfo);
String authorizeUrl = webAuth.start();
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.finish(code);
}
catch (DbxException ex) {
System.err.println("Error in DbxWebAuth.start: " + ex.getMessage());
System.exit(1); return;
}
System.out.println("Authorization complete.");
System.out.println("- User ID: " + authFinish.userId);
System.out.println("- Access Token: " + authFinish.accessToken);
// Save auth information to output file.
DbxAuthInfo authInfo = new DbxAuthInfo(authFinish.accessToken, appInfo.host);
try {
DbxAuthInfo.Writer.writeToFile(authInfo, argAuthFileOutput);
System.out.println("Saved authorization information to \"" + argAuthFileOutput + "\".");
}
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;
}
}
private static void printHelp(PrintStream out)
{
out.println("Usage: COMMAND <app-info-file> <auth-file-output>");
out.println("");
out.println("<app-info-file>: a JSON file with information about your API app. Example:");
out.println("");
out.println(" {");
out.println(" \"key\": \"Your Dropbox API app key...\",");
out.println(" \"secret\": \"Your Dropbox API app secret...\"");
out.println(" }");
out.println("");
out.println(" Get an API app key by registering with Dropbox:");
out.println(" https://dropbox.com/developers/apps");
out.println("");
out.println("<auth-file-output>: If authorization is successful, the resulting API");
out.println(" access token will be saved to this file, which can then be used with");
out.println(" other example programs, such as the one in \"examples/account-info\".");
out.println("");
}
}