Skip to content

Commit 1496e43

Browse files
author
Kannan Goundan
committed
Support OAuth 1 token upgrade and OAuth 1/2 token disabling.
1 parent 84eca6d commit 1496e43

9 files changed

Lines changed: 394 additions & 4 deletions

File tree

ChangeLog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
- Stricter SSL: Hard-code the ciphersuites and trusted root
22
certificates (instead of using the system defaults).
3+
- Add DbxOAuth1Upgrader, which upgrades existing OAuth 1 access tokens
4+
to OAuth 2 access tokens.
35

46
---------------------------------------------
57
1.7.5 (2013-09-16)

examples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<module>account-info</module>
1717
<module>upload-file</module>
1818
<module>web-file-browser</module>
19+
<module>upgrade-oauth1-token</module>
1920
</modules>
2021

2122
<dependencies>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4+
http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<name>Upgrade OAuth 1 Token Example (Dropbox Core API SDK)</name>
8+
<artifactId>examples-upgrade-oauth1-token</artifactId>
9+
<packaging>jar</packaging>
10+
11+
<parent>
12+
<groupId>com.dropbox.core</groupId>
13+
<artifactId>examples</artifactId>
14+
<version>0-SNAPSHOT</version>
15+
<relativePath>../pom.xml</relativePath>
16+
</parent>
17+
18+
<build>
19+
<plugins>
20+
<plugin>
21+
<groupId>org.apache.maven.plugins</groupId>
22+
<artifactId>maven-dependency-plugin</artifactId>
23+
<executions>
24+
<execution>
25+
<phase>compile</phase>
26+
<goals>
27+
<goal>build-classpath</goal>
28+
</goals>
29+
</execution>
30+
</executions>
31+
</plugin>
32+
</plugins>
33+
</build>
34+
</project>
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
}

src/com/dropbox/core/DbxClient.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,30 @@ public DbxAccountInfo handle(HttpRequestor.Response response) throws DbxExceptio
277277
});
278278
}
279279

280+
// -----------------------------------------------------------------
281+
// /unlink_access_token
282+
283+
/**
284+
* Disable the access token that you constructed this {@code DbxClient}
285+
* with. After calling this, API calls made with this {@DbxClient} will
286+
* fail.
287+
*/
288+
public void disableAccessToken()
289+
throws DbxException
290+
{
291+
String host = this.host.api;
292+
String apiPath = "1/disable_access_token";
293+
294+
doPost(host, apiPath, null, null, new DbxRequestUtil.ResponseHandler<Void>() {
295+
@Override
296+
public Void handle(HttpRequestor.Response response) throws DbxException
297+
{
298+
if (response.statusCode != 200) throw new DbxException.BadResponse("unexpected response code: " + response.statusCode);
299+
return null;
300+
}
301+
});
302+
}
303+
280304
// -----------------------------------------------------------------
281305
// /files (GET)
282306

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.dropbox.core;
2+
3+
/**
4+
* Use with {@link DbxOAuth1Upgrader} to convert old OAuth 1 access tokens
5+
* to OAuth 2 access tokens. This SDK doesn't support using OAuth 1 access
6+
* tokens for regular API calls.
7+
*/
8+
public final class DbxOAuth1AccessToken
9+
{
10+
/**
11+
* The OAuth 1 access token key.
12+
*/
13+
public final String key;
14+
15+
/**
16+
* The OAuth 1 access token secret.
17+
*/
18+
public final String secret;
19+
20+
/**
21+
* @param key {@link #key}
22+
* @param secret {@link #secret}
23+
*/
24+
public DbxOAuth1AccessToken(String key, String secret)
25+
{
26+
this.key = key;
27+
this.secret = secret;
28+
}
29+
}

0 commit comments

Comments
 (0)