getSharedLink is returning garbage
AnsweredI am testing the ability to get the shared link to a file so I can auto distribute it to users. Unfortunately, I am getting jibberish when making the request. Although the end goal is to feed a path\file and get the link in return, this is just a small step in the process. When I issue the getSharedLink call from the java API, I get:
com.box.sdk.BoxSharedLink@6b09bb57
When I go to the file through the UI and select "Copy Link" I get:
https://app.box.com/s/f44fuy1zrx59t2wkxvoka3kzxolrioqw
I would understand it more if the API result was some truncated version of the actual link, but it is not. Any assistance/suggestions would be most appreciated.
Note that I have also used the API to first create the link and then retrieve it and had the same results. The code is listed below, and the jars I use are (as suggested by Box documentation):
bcpkix-jdk15on-1.52.jar
bcprov-jdk15on-1.52.jar
box-java-sdk-2.14.1.jar
jose4j-0.4.4.jar
minimal-json-0.9.1.jar
Also:
commons-logging-1.2.jar
package getboxidandlink;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.box.sdk.*;
import com.box.sdk.BoxAPIConnection;
import com.box.sdk.BoxFolder;
import com.box.sdk.BoxUser;
/**
*
* @author saulf
*/
public class GetBoxIDandLink {
public static final String DEVELOPER_TOKEN = "";
public static final int MAX_DEPTH = 1;
public static void main(String[] args) {
// Turn off logging to prevent polluting the output.
Logger.getLogger("com.box.sdk").setLevel(Level.OFF);
BoxAPIConnection api = new BoxAPIConnection(DEVELOPER_TOKEN);
BoxUser.Info userInfo = BoxUser.getCurrentUser(api).getInfo();
System.out.format("Welcome, %s <%s>!\n\n", userInfo.getName(), userInfo.getLogin());
BoxFolder rootFolder = BoxFolder.getRootFolder(api);
listFolder(rootFolder, 0,DEVELOPER_TOKEN);
}
private static void listFolder(BoxFolder folder, int depth,String DEVELOPER_TOKEN) {
BoxAPIConnection api = new BoxAPIConnection(DEVELOPER_TOKEN);
for (BoxItem.Info itemInfo : folder) {
String indent = "";
for (int i = 0; i < depth; i++) {
indent += " ";
}
String fileType = String.valueOf(itemInfo.getClass());
fileType=fileType.substring(21,fileType.length()-5);
String fileID = String.valueOf(itemInfo.getID());
System.out.println(indent + itemInfo.getName()+" id:"+itemInfo.getID()+" class: "+fileType+" sharedLink: "+itemInfo.getSharedLink());
if (fileID.trim().equals("28***phone number removed for privacy***")) {
System.out.println(itemInfo.getName()+" is a file");
BoxFile file = new BoxFile(api, fileID);
Calendar unshareAt = Calendar.getInstance();
unshareAt.set(2100,12,31);
BoxSharedLink.Permissions permissions= new BoxSharedLink.Permissions();
permissions.setCanDownload(true);
permissions.setCanPreview(true);
//file.createSharedLink(BoxSharedLink.Access.OPEN,unshareAt.getTime(),permissions);
System.out.println(file.getInfo().getSharedLink());
}
//String astring = BoxSharedLink (api,itemInfo.getID());
if (itemInfo instanceof BoxFolder.Info) {
BoxFolder childFolder = (BoxFolder) itemInfo.getResource();
if (depth < MAX_DEPTH) {
listFolder(childFolder, depth + 1,DEVELOPER_TOKEN);
}
}
}
}
}
-
Hey, I'm Matt and my team maintains the Box Java SDK — I'm happy to help you out here! The output you're seeing isn't garbage, it's just Java telling you that what you're dealing with is an Object of some sort that doesn't have an overridden .toString() method (specifically one with type com.box.sdk.BoxSharedLink). If you look at the Javadocs for the getSharedLink() method, you'll see that it returns this type of object, not a String. To get the actual URL string, you'll need to something like this:
BoxSharedLink sharedLink = itemInfo.getSharedLink(); if (sharedLink != null) { String sharedLinkURL = sharedLink.getURL(); // do something with the shared link URL }
Please sign in to leave a comment.
Comments
3 comments