Skip to content

Commit 9153575

Browse files
committed
Add getAccountId() to DbxAuthFinish.
1 parent 4f08600 commit 9153575

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

examples/authorize/src/main/java/com/dropbox/core/examples/authorize/Main.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public static void main(String[] args) throws IOException {
8787

8888
System.out.println("Authorization complete.");
8989
System.out.println("- User ID: " + authFinish.getUserId());
90+
System.out.println("- Account ID: " + authFinish.getAccountId());
9091
System.out.println("- Access Token: " + authFinish.getAccessToken());
9192

9293
// Save auth information to output file.

src/main/java/com/dropbox/core/DbxAuthFinish.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
public final class DbxAuthFinish {
2222
private final String accessToken;
2323
private final String userId;
24+
private final String accountId;
2425
private final /*@Nullable*/String urlState;
2526

2627
/**
@@ -29,9 +30,10 @@ public final class DbxAuthFinish {
2930
* @param urlState State data passed in to {@link DbxWebAuth#start} or {@code null} if no state
3031
* was passed
3132
*/
32-
public DbxAuthFinish(String accessToken, String userId, /*@Nullable*/String urlState) {
33+
public DbxAuthFinish(String accessToken, String userId, String accountId, /*@Nullable*/String urlState) {
3334
this.accessToken = accessToken;
3435
this.userId = userId;
36+
this.accountId = accountId;
3537
this.urlState = urlState;
3638
}
3739

@@ -47,14 +49,24 @@ public String getAccessToken() {
4749

4850
/**
4951
* Returns the Dropbox user ID of the user who just approved your app for access to their
50-
* Dropbox account.
52+
* Dropbox account. We use user ID to identify user in API V1.
5153
*
5254
* @return Dropbox user ID of user that approved your app for access to their account
5355
*/
5456
public String getUserId() {
5557
return userId;
5658
}
5759

60+
/**
61+
* Returns the Dropbox account ID of the user who just approved your app for access to their
62+
* Dropbox account. We use account ID to identify user in API V2.
63+
*
64+
* @return Dropbox account ID of user that approved your app for access to their account
65+
*/
66+
public String getAccountId() {
67+
return accountId;
68+
}
69+
5870
/**
5971
* Returns the state data you passed in to {@link DbxWebAuth#start}. If you didn't pass
6072
* anything in, or you used {@link DbxWebAuthNoRedirect}, this will be {@code null}.
@@ -76,7 +88,7 @@ DbxAuthFinish withUrlState(/*@Nullable*/ String urlState) {
7688
if (this.urlState != null) {
7789
throw new IllegalStateException("Already have URL state.");
7890
}
79-
return new DbxAuthFinish(accessToken, userId, urlState);
91+
return new DbxAuthFinish(accessToken, userId, accountId, urlState);
8092
}
8193

8294
/**
@@ -89,6 +101,7 @@ public DbxAuthFinish read(JsonParser parser) throws IOException, JsonReadExcepti
89101
String accessToken = null;
90102
String tokenType = null;
91103
String userId = null;
104+
String accountId = null;
92105
String state = null;
93106

94107
while (parser.getCurrentToken() == JsonToken.FIELD_NAME) {
@@ -105,6 +118,9 @@ else if (fieldName.equals("access_token")) {
105118
else if (fieldName.equals("uid")) {
106119
userId = JsonReader.StringReader.readField(parser, fieldName, userId);
107120
}
121+
else if (fieldName.equals("account_id")) {
122+
accountId = JsonReader.StringReader.readField(parser, fieldName, accountId);
123+
}
108124
else if (fieldName.equals("state")) {
109125
state = JsonReader.StringReader.readField(parser, fieldName, state);
110126
}
@@ -123,8 +139,9 @@ else if (fieldName.equals("state")) {
123139
if (tokenType == null) throw new JsonReadException("missing field \"token_type\"", top);
124140
if (accessToken == null) throw new JsonReadException("missing field \"access_token\"", top);
125141
if (userId == null) throw new JsonReadException("missing field \"uid\"", top);
142+
if (accountId == null) throw new JsonReadException("missing field \"account_id\"", top);
126143

127-
return new DbxAuthFinish(accessToken, userId, state);
144+
return new DbxAuthFinish(accessToken, userId, accountId, state);
128145
}
129146
};
130147

src/test/java/com/dropbox/core/DbxWebAuthTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,17 @@ public void testFinishWithState() throws Exception {
150150

151151
assertNotNull(sessionStore.get());
152152

153-
DbxAuthFinish expected = new DbxAuthFinish("test-access-token", "test-user-id", state);
153+
DbxAuthFinish expected = new DbxAuthFinish(
154+
"test-access-token", "test-user-id", "test", state
155+
);
154156
ByteArrayOutputStream body = new ByteArrayOutputStream();
155157
ByteArrayInputStream responseStream = new ByteArrayInputStream(
156158
(
157159
"{" +
158160
"\"token_type\":\"Bearer\"" +
159161
",\"access_token\":\"" + expected.getAccessToken() + "\"" +
160162
",\"uid\":\"" + expected.getUserId() + "\"" +
163+
",\"account_id\":\"" + expected.getAccountId() + "\"" +
161164
"}"
162165
).getBytes("UTF-8")
163166
);

0 commit comments

Comments
 (0)