Search code examples
javascriptautodesk-forgeautodeskautodesk-model-derivativeautodesk-data-management

Autodesk Forge API createBucket not working


I'm trying to build an application by using the autodesk-forge-api's. To start with I created an App inside my Autodesk configuration to receive an "ClientID" and a "Client Secret" which are required to make API calls.

Somehow, when I try to use the createBucket-API call, which is documentet here I get an answer bad request 400 and I dont't know why.

here is my API call:

let oAuth2TwoLegged = new ForgeSDK.AuthClientTwoLegged(clientId, 
  clientSecret, [
  'data:read',
  'data:write',
  'bucket:create',
  'bucket:read',
  'data:write',
  'data:read',
  'viewables:read'           
], autoRefresh);

oAuth2TwoLegged.authenticate().then(function(credentials){

  var HubsApi = new ForgeSDK.HubsApi(); //Hubs Client
  var BucketsApi = new ForgeSDK.BucketsApi(); //Buckets Client

  BucketsApi.createBucket({bucketKey :"Test", policyKey: "transient"},{}, oAuth2TwoLegged, credentials).then((response) => {
    console.log(' new BUCKET: ', response)
  }).catch((err) => {
    console.log('ERROR BLA: ', err)
  });

}).catch((err) => {
  console.log('oauth error: ', err)
})

Does anyone have a suggestion what I might doing wrong?

when I use a different call, for example this:

BucketsApi.getBuckets({}, oAuth2TwoLegged, credentials).then(function(response){
   console.log('buckets: ', response.body);
  }, function(err){
    console.error(err);
});

it works...

EDIT

If I do the following:

BucketsApi.createBucket(xyda_select_rtl, {'bucketKey' :'xyda_select_rtl', 
   'policyKey': 'transient'}, oAuth2TwoLegged, credentials).then((response) => {
    console.log(' new BUCKET: ', response)
  }).catch((err) => {
    console.log('ERROR BLA: ', err)
});

I get xyda_select_rtl is not defined!?!

When I add:

var xyda_select_rtl;

I get the error Missing the required parameter 'postBuckets' when calling createBucket

Ehhm.... yeah :-/


Solution

  • You need to come up with a unique name for your buckets, sorry for that bad news. It's not so bad that you think. For example simply add your client_id to the bucket name:

    "test-tAp1fqjjtcgqS4CKpCYDjAyNbKW4IVCC"
    

    Or use a random guid generated by code:

    function guid() {
    
        var d = new Date().getTime();
    
        var guid = 'xxxx-xxxx-xxxx'.replace(
          /[xy]/g,
          function (c) {
            var r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c == 'x' ? r : (r & 0x7 | 0x8)).toString(16);
          });
    
        return guid;
      }
    

    Then use it to generate a bucket name:

    var bucketKey = "test-" + guid()