• Joseph Daily 16
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Hello,

I'm trying to set up a Lightning app that will allow a user to provide a url to a zip file, have that zip file loaded as a dataset into the predictive vision service, then specify the newly created dataset to be trained into a datamodel. I'm able to load the data model from the menu, and get back a dataset id.

But I'm stuck on how to call the training service. I modified the provided VisionController code as follows:

    @AuraEnabled
    public static String postDatasetTrain(string datasetId, string datasetName) {
        // Get a new token
        JWT jwt = new JWT('RS256');
        jwt.cert = 'SelfSignedCert_18Jul2016_235259';
        jwt.iss = 'developer.force.com';
        jwt.sub = 'joseph.daily@careerbuilder.com';
        jwt.aud = 'https://api.metamind.io/v1/oauth2/token';
        jwt.exp = '3600';
        String access_token = JWTBearerFlow.getAccessToken('https://api.metamind.io/v1/oauth2/token', jwt);      
        
        String VISION_API = 'https://api.metamind.io/v1/vision';
        String DATA_TRAIN = VISION_API + '/train';
        string contentType = HttpFormBuilder.GetContentType();
        //  Compose the form
        string form64 = '';      
        form64 += HttpFormBuilder.WriteBoundary();
        form64 += HttpFormBuilder.WriteBodyParameter('name', datasetName);
        form64 += HttpFormBuilder.WriteBoundary();
        form64 += HttpFormBuilder.WriteBodyParameter('datasetId', datasetId);        
        form64 += HttpFormBuilder.WriteBoundary(HttpFormBuilder.EndingType.CrLf);
        
        blob formBlob = EncodingUtil.base64Decode(form64);
        string contentLength = string.valueOf(formBlob.size());
        //  Compose the http request
        HttpRequest httpRequest = new HttpRequest();
        
        httpRequest.setBodyAsBlob(formBlob);
        httpRequest.setHeader('Connection', 'keep-alive');
        httpRequest.setHeader('Content-Length', contentLength);
        httpRequest.setHeader('Content-Type', contentType);
        httpRequest.setMethod('POST');
        httpRequest.setTimeout(120000);
        httpRequest.setHeader('Authorization','Bearer ' + access_token);
        httpRequest.setEndpoint(DATA_TRAIN);
        
        Http http = new Http();
        HTTPResponse res = http.send(httpRequest);
        system.debug('res: ' + res);
        system.debug('res.getBody(): ' + res.getBody());
        return res.getBody();
    }    

 

But the response that comes back is always a 403 : forbidden message.

Is there anything obvious causing this in my code?