You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.
Forum Discussion
Thang2000
2 years agoExplorer | Level 4
getMetadataValue() return null on "shared link already exists", JAVA SDK
I'm trying to implement a function where I can get shared links to dropbox folders via API.
I was able to generate the shared link but if the link has been generated, the next call on the same folder will return this exception:
"CreateSharedLinkWithSettingsErrorException"
I tried to handle it so that I could retrieve the pre-existed shared link/URL but struggling on implementation as I got unxepcted null values, please tell me what I should change in my code, down below is my function in full:
SharedLinkMetadata sharedLinkMetadata = error.getSharedLinkAlreadyExistsValue().getMetadataValue();
"error.getSharedLinkAlreadyExistsValue()" just return null:
// function starts from here
public Optional<String> getSharedLink(String path) throws DbxException {
try {
SharedLinkSettings.Builder builder = SharedLinkSettings.newBuilder();
builder.withRequestedVisibility(RequestedVisibility.PUBLIC);
SharedLinkSettings settings = builder.build();
DbxUserSharingRequests sharingRequests = dbxClient.sharing();
SharedLinkMetadata asd = sharingRequests.createSharedLinkWithSettings(path, settings);
return Optional.of(asd.getUrl());
}
catch (DbxException e) {
if (e instanceof CreateSharedLinkWithSettingsErrorException){
CreateSharedLinkWithSettingsErrorException createSharedLinkException = (CreateSharedLinkWithSettingsErrorException) e;
CreateSharedLinkWithSettingsError error = createSharedLinkException.errorValue;
if (error.isSharedLinkAlreadyExists()) {
SharedLinkMetadata sharedLinkMetadata = error.getSharedLinkAlreadyExistsValue().getMetadataValue();
return Optional.of(sharedLinkMetadata.getUrl());
}
}
System.out.println(e.getMessage());
return Optional.empty();
}
}
here's the return objects for Exception e:
com.dropbox.core.v2.sharing.CreateSharedLinkWithSettingsErrorException: Exception in 2/sharing/create_shared_link_with_settings: {".tag":"shared_link_already_exists","shared_link_already_exists":null}
box.core.v2.sharing.CreateSharedLinkWithSettingsError.getSharedLinkAlreadyExistsValue()" is null] with root cause
java.lang.NullPointerException: Cannot invoke "com.dropbox.core.v2.sharing.SharedLinkAlreadyExistsMetadata.getMetadataValue()" because the return value of "com.dropbox.core.v2.sharing.CreateSharedLinkWithSettingsError.getSharedLinkAlreadyExistsValue()" is null
at com.powers.customers.service.DropboxClientService.getSharedLink(DropboxClientService.java:67) ~[classes/:na]
at com.powers.customers.controller.DropboxWebhookController.getSharedLink(DropboxWebhookController.java:56) ~[classes/:na]
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:577) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.22.jar:5.3.22]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.22.jar:5.3.22]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.22.jar:5.3.22]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.22.jar:5.3.22]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.22.jar:5.3.22]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.22.jar:5.3.22]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1070) ~[spring-webmvc-5.3.22.jar:5.3.22]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.22.jar:5.3.22]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.22.jar:5.3.22]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.22.jar:5.3.22]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:655) ~[tomcat-embed-core-9.0.65.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.22.jar:5.3.22]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.65.jar:4.0.FR]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.65.jar:9.0.65]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
- ЗдравкоLegendary | Level 20
Hi Thang2000,
Your mistake is that you're expecting Dropbox always returns data for link metadata (i.e. getSharedLinkAlreadyExistsValue() returns valid, non null, value). Unfortunately, returning valid value is something possible and likely, but not mandatory (i.e. it's absolutely normal, data for the existing link to be unavailable immediately in the exception)! If you take a look on "getSharedLinkAlreadyExistsValue"s documentation, there is:
... You can call DbxUserSharingRequests.listSharedLinks(com.dropbox.core.v2.sharing.ListSharedLinksArg) to get the existing link, or use the provided metadata if it is returned. ...
Emphasize of "if it is returned"! So, you have to check always for non null value before further processing. If value is null, as mentioned in documentation, explicit requesting the file/folder link metadata is applicable (the same as if in advance you know that link to the target already exists). 😉 Don't forget to set the target path and denote that you want direct link (otherwise you will need to sort out lot of returned links yourself, not just the targets one).
Hope this helps.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,880 PostsLatest Activity: 2 hours agoIf you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!