Error 403 Forbidden

upload

(Ibean1996) #1

im using the data api to upload my model by using my api token,but when i run the application it throws the Error 403 forbidden.i dont konw where is wrong or the api has problem?How could i make it?

Have a good day


#2

Hello,

Can you please share your script? Thanks!


(Ibean1996) #3
import java.io.File;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;

/**
 * cn.fserve.controller
 *
 * @author 
 * @date 2018/4/12
 **/
public class SketchfabUploadController {

public SketchfabUploadController() {
}

public static void main(String[] args) {
/**
 * Model Upload
 * https://docs.sketchfab.com/data-api/v3/index.html#!/models/post_v3_models
 *
 * Implementation Notes
 * Uploads a new model. Accepts multipart/form-data only.
 */
String token = "my api token";

//Setting up client parameters
//https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client
ClientConfig config = new ClientConfig();

//becarefull with timeout during large model file upload and your connection speed
config.property("jersey.config.client.connectTimeout", 5000);
config.property("jersey.config.client.readTimeout", 50000);
config.register(MultiPartFeature.class);

Client client = ClientBuilder.newClient(config);

WebTarget webTarget = client.target("https://api.sketchfab.com/v3").path("/models");

//https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-multipart
FormDataMultiPart uploadModelMultipart = new FormDataMultiPart();
uploadModelMultipart.field("name", "Java Test1", MediaType.TEXT_PLAIN_TYPE);
//pro only
uploadModelMultipart.field("private", Boolean.TRUE, MediaType.TEXT_PLAIN_TYPE);
//pro only
uploadModelMultipart.field("password", "", MediaType.TEXT_PLAIN_TYPE);
//optional - default TRUE
uploadModelMultipart.field("isPublished", Boolean.TRUE, MediaType.TEXT_PLAIN_TYPE);
//optional - default FALSE
uploadModelMultipart.field("isInspectable", Boolean.FALSE, MediaType.TEXT_PLAIN_TYPE);
//limit of 1024 characters
uploadModelMultipart.field("description", "Model description", MediaType.TEXT_PLAIN_TYPE);

//https://docs.sketchfab.com/data-api/v3/index.html
//you can specify extra informations like
//licence
//tags
//categories
//options
//just follow the example above, always using "MediaType.TEXT_PLAIN_TYPE"

//For create body parts that contains a file attachments we will need to use "FileDataBodyPart" class
//model path on your local storage
String filePath3D = "F://test.fbx";

File modelFile = new File(filePath3D);
FileDataBodyPart modelFilePart = new FileDataBodyPart("modelFile", modelFile);

//attaching our "modelFilePart" to the request body
uploadModelMultipart.bodyPart(modelFilePart);

//Setting up our Token to request header
//if we don't, response will be "NotAuthorizedException"
Builder builder = webTarget.request(MediaType.APPLICATION_JSON_TYPE)
        .header("Authorization", "Token " + token);

try {
    //For a correct response of an object representation of JSON, we will use "jersey-media-json-jackson"
    //https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson
    SketchfabUploadModelResponse sketchfabUploadModelResponse = builder.post(Entity.entity(uploadModelMultipart, MediaType.MULTIPART_FORM_DATA), SketchfabUploadModelResponse.class);

    System.out.println("Upload sucess!!! " + sketchfabUploadModelResponse.getUid() + " (" + sketchfabUploadModelResponse.getUri() + ")");
} catch (NotAuthorizedException ex) {
    //call not authorized, possible TOKEN problem
    System.out.println("Invalid Token -> " + token);
} catch (BadRequestException ex) {
    //bad parameters
    //remember that are some limitations on different plans of Sketchfab API
    //Model size
    //Basic - 50MB per upload
    //Pro - 200MB per upload
    //Business - 500MB per upload
    System.out.println("bad parameters");
}

}

//Inner class helper on JSON response convert
class SketchfabUploadModelResponse {

private String uid;

private String uri;

public SketchfabUploadModelResponse() {
}

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}

public String getUri() {
    return uri;
}

public void setUri(String uri) {
    this.uri = uri;
}
}
}

This is my code by java and i hide my token then run it ,the console get the expection

Exception in thread "main" javax.ws.rs.ForbiddenException: HTTP 403 Forbidden
	at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1017)
	at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:819)
	at org.glassfish.jersey.client.JerseyInvocation.access$700(JerseyInvocation.java:92)
	at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:701)
	at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
	at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
	at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
	at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444)
	at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:697)
	at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:448)
	at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:349)
	at cn.fserve.controller.SketchfabUploadController.main(SketchfabUploadController.java:92)

(Ibean1996) #4

I have solved this problem because I have less references to a dependency and the network times out. Thank you for your answer.Have fun!


#5

Cool, glad you figured it out!