0

I am using the Forge data management API to access my A360 files and aim to translate them into the SVF format so that I can view them in my viewer. So far I have been able to reach the desired item using the ForgeDataManagement.ItemsApi, but I don't know what to do with the item to upload it to the bucket in my application.

From the documentation it seems like uploadObject is the way to go (https://github.com/Autodesk-Forge/forge.oss-js/blob/master/docs/ObjectsApi.md#uploadObject), but I don't know exactly how to make this function work.

var dmClient = ForgeDataManagement.ApiClient.instance;
var dmOAuth = dmClient.authentications ['oauth2_access_code'];
dmOAuth.accessToken = tokenSession.getTokenInternal();
var itemsApi = new ForgeDataManagement.ItemsApi();

fileLocation = decodeURIComponent(fileLocation);
var params = fileLocation.split('/');
var projectId = params[params.length - 3];
var resourceId = params[params.length - 1];

itemsApi.getItemVersions(projectId, resourceId)
 .then (function(itemVersions) {
   if (itemVersions == null || itemVersions.data.length == 0) return;

   // Use the latest version of the item (file).
   var item = itemVersions.data[0];

   var contentLength = item.attributes.storageSize;                  
   var body = new ForgeOSS.InputStream();
   // var body = item; // Using the item directly does not seem to work.
   // var stream = fs.createReadStream(...) // Should I create a stream object lik suggested in the documention?

   objectsAPI.uploadObject(ossBucketKey, ossObjectName, contentLength, body, {}, function(err, data, response) {
     if (err) {
       console.error(err);
     } else {
       console.log('API called successfully. Returned data: ' + data);

       //To be continued...
     }

I hope someone can help me out!

My current data:

ossObjectName = "https://developer.api.autodesk.com/data/v1/projects/"myProject"/items/urn:"myFile".dwfx";
ossBucketKey = "some random string based on my username and id";

Regards, torjuss

torjuss
  • 11
  • 4

2 Answers2

0

When using the DataManagement API, you can either work with

  • 2 legged oAuth (client_credentials) and access OSS' buckets and objects,
  • or 3 legged (authorization_code) and access a user' Hubs, Projects, Folders, Items, and Revisions

When using 3 legged, you do access someones content on A360, or BIM360 and these files are automatically translated by the system, so you do not need to translate them again, not to transfer them on a 2 legged application bucket. The only thing you need to do it is get the manifest of the Item or its revision and use the URN to see it in the viewer.

Checkout an example here: https://developer.autodesk.com/en/docs/data/v2/reference/http/projects-project_id-versions-version_id-GET/

you'll see something like Examples: Successful Retrieval of a Specific Version (200)

curl -X GET -H "Authorization: Bearer kEnG562yz5bhE9igXf2YTcZ2bu0z" "https://developer.api.autodesk.com/data/v1/projects/a.45637/items/urn%3Aadsk.wipprod%3Adm.lineage%3AhC6k4hndRWaeIVhIjvHu8w"
{
  "data": {
    "relationships": {
      "derivatives": {
        "meta": {
          "link": {
            "href": "/modelderivative/v2/designdata/dXJuOmFkc2sud2lwcWE6ZnMuZmlsZTp2Zi50X3hodWwwYVFkbWhhN2FBaVBuXzlnP3ZlcnNpb249MQ/manifest"
          }
        },

Now, to answer teh other question about upload, I got an example available here: https://github.com/Autodesk-Forge/forge.commandline-nodejs/blob/master/forge-cb.js#L295. I copied the relevant code here for everyone to see how to use it:

fs.stat (file, function (err, stats) {
    var size =stats.size ;
    var readStream =fs.createReadStream (file) ;
    ossObjects.uploadObject (bucketKey, fileKey, size, readStream, {}, function (error, data, response) {
        ...
    }) ;
}) ;

Just remember that ossObjects is for 2 legged, where as Items, Versions are 3 legged.

cyrille
  • 2,616
  • 1
  • 10
  • 18
  • Thank you for your answer, cyrille! It makes sense to use the A360 files directly without transferring to the application bucket. However; when I now try to launch my viewer and load the document I get a 401 Unauthorized status, regardless if I use 2 or 3 legged authentication (not quite sure which one to use). I use the following method: Autodesk.Viewing.Document.load(documentId, oauth3legtoken, function (doc) { ... }); And get the following HTTP status: https://developer.api.autodesk.com/oss-ext/v1/acmsessions 401 (Unauthorized) – torjuss Dec 16 '16 at 10:54
  • I found the following post that looks like it is related to this problem. Do you know if it has been fixed, or do I have to use the alternative approach that Philippe Leefsma suggest? http://stackoverflow.com/questions/37835178/creating-a-viewer-application-with-an-urn-from-autodesk-a360/38328133#38328133 – torjuss Dec 16 '16 at 11:12
0

We figured out how to get things working after some support from Adam Nagy. To put it simple we had to do everything by use of 3 legged OAuth since all operations involves a document from an A360 account. This includes accessing and showing the file structure, translating a document to SVF, starting the viewer and loading the document into the viewer.

Also, we were targeting the wrong id when trying to translate the document. This post shows how easily it can be done now, thanks to Adam for the info!

torjuss
  • 11
  • 4