revokeToken method throws error in JavaSDK when trying to revoke downscoped token
AnsweredI'm trying to revoke a down scoped token using JavaSDK and I get invalid client error.
com.box.sdk.BoxAPIResponseException: The API returned an error code [400 | .0ndnmo1u27ki9h3nu1p7m5tvq49] invalid_request - The client is invalid
Here is my code
import com.box.sdk.*;
import java.io.FileReader;
import java.io.Reader;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;
public class DocUpload {
public static void main(String[] args) {
Reader reader = null;
try {
reader = new FileReader("C:\\dev\\repositories\\BoxIntegrationTest\\src\\main\\resources\\box-config.json");
BoxConfig boxConfig = BoxConfig.readFrom(reader);
//Proxy config
Proxy proxy = new Proxy(Proxy.Type.HTTP, new
InetSocketAddress(Inet4Address.getByName("199.19.***.***"), 80));
int MAX_CACHE_ENTRIES = 100;
IAccessTokenCache accessTokenCache = new
InMemoryLRUAccessTokenCache(MAX_CACHE_ENTRIES);
// Create new app connection object
String userId = "****";
BoxDeveloperEditionAPIConnection client = new BoxDeveloperEditionAPIConnection(userId, DeveloperEditionEntityType.USER, boxConfig, accessTokenCache);
client.setProxy(proxy);
System.out.println(client.getAccessToken());
System.out.println(client.getExpires());
// Down scoping a token to a particular folder
List scopes = new ArrayList();
scopes.add("item_preview");
StringBuilder sb = new StringBuilder();
sb.append(client.getBaseURL());
sb.append("folders/8784**");
ScopedToken scoped = client.getLowerScopedToken(scopes, sb.toString());
System.out.println(scoped.getAccessToken());
// Use the down scoped token to read a folder
BoxAPIConnection api = new BoxAPIConnection(scoped.getAccessToken());
api.setProxy(proxy);
BoxFolder folder1 = new BoxFolder(api, "8784**");
BoxFolder.Info info1 = folder1.getInfo();
System.out.println(info1.getName());
//revoke token
api.revokeToken();
} catch (Exception e) {
e.printStackTrace();
}
}
}
And here is the output I'm getting
***TOKEN REMOVED***
4055000
***TOKEN REMOVED***
***TOKEN REMOVED***
DocUpload-Test
com.box.sdk.BoxAPIResponseException: The API returned an error code [400 | .0ndnmo1u27ki9h3nu1p7m5tvq49] invalid_request - The client is invalid
at com.box.sdk.BoxAPIResponse.(BoxAPIResponse.java:92)
at com.box.sdk.BoxJSONResponse.(BoxJSONResponse.java:32)
at com.box.sdk.BoxAPIRequest.trySend(BoxAPIRequest.java:579)
at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:354)
at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:329)
at com.box.sdk.BoxAPIConnection.revokeToken(BoxAPIConnection.java:757)
at DocUpload.main(DocUpload.java:50)
Am i doing anything wrong here in the above example?
In this post it is mentioned that, that isn't an option to set token expiration through SDK. Is it still the case?
-------------------------------------------------------------------
Here is what I've been trying to do -
- I have a web app in ReactJS that calls my API written in java to fetch a token from Box.
- API would call Box API using JavaSDK to get a down scoped to a particular folder.
- Once the user is finished uploading a file to the folder, I would like to destroy the token.
- So to do that, the web app would send back the token to API to destroy. API would then use this token to create a client and call Java SDK's client.revokeToken() method that would destroy the token in Box.
Am I doing it right? Is this the right usage pattern?
Appreciate any input in this regard.
-
Thanks for that. Would be looking forward for this feature.
Meanwhile we will create temporary folder for each upload and scope the tokens to upload only to that folder and delete it once it is done. Hope this would achieve our goal to avoid reusing of scoped token before they expire.
-
Hi
Just wanted to pop in and tell you that we launched the revoke endpoint yesterday! You should now be able to revoke downscoped tokens from Java SDK: New Security Enhancements for Revoking Access Tokens
Cheers,
Braden
Please sign in to leave a comment.
Comments
3 comments