View And Data API with PowerShell

Last week, we posted about the View And Data API and showed how to upload an assembly from Vault via powerJobs and PowerShell. This week, I’d like to add some more background. For this purpose I’ve created a stand-alone script that uploads a little sample assembly.

In order to get started, you need your own access keys. It’s free, so go on and register for a free account. Under “My Apps” you can create a new app. Just give it a name and select the View and Data API. Once you have your app registered, you will receive a Consumer Key and Consumer Secret. These two strings are necessary for the login. In the script, at the top, you’ll find the two variables where you can enter your keys.

The following script will take an assembly from the Inventor sample folder and upload it to the cloud. As this is pure PowerShell, you may create your own batch scripts for uploading the files. Here is the complete script:

$samplePath = "C:\Users\Public\Documents\Autodesk\Inventor 2015\Samples\Models\Assemblies\Tuner"
$login = Invoke-RestMethod -Uri "" -ContentType "application/x-www-form-urlencoded" -Method Post -Body "client_id=$ConsumerKey&amp;client_secret=$ConsumerSecret&amp;grant_type=client_credentials"
$auth = $login.token_type + " " + $login.access_token
#create bucket
$bucketName = "tuner" #must be lower case and/or numbers
$policy = "transient" #transient
$json = '{"bucketKey":"'+$bucketName+'","policy":"'+$policy+'"}'
$bucket = Invoke-RestMethod -Uri "" -ContentType "application/json" -Method Post -Body $json -Headers @{"Authorization"=$auth}
#upload assembly
$assembly = Get-Item "$samplePath\Tuner.iam"
$uploadedFiles = @()
$uploadedFiles += Invoke-RestMethod -Uri "$bucketName/objects/$($assembly.Name.ToLower())" -ContentType "application/octet-stream" -Method Put -Headers @{"Authorization"=$auth;"Content-Length"=$assembly.Length;"Expect"=""} -InFile $assembly.FullName.ToLower()

$json = '{ "master" : "urn:adsk.objects:os.object:'+$bucketName+'/'+$assembly.Name.ToLower()+'", "dependencies" : ['

#upload children
$files = Get-ChildItem -LiteralPath "$samplePath\Tuner Components" -File
foreach ($file in $files) {
  echo "uploading $($file.Name) ..."
  $uploadedFiles += Invoke-RestMethod -Uri "$bucketName/objects/$($file.Name)" -ContentType "application/octet-stream" -Method Put -Headers @{"Authorization"=$auth;"Content-Length"=$file.Length;"Expect"=""} -InFile $file.FullName
  $json += '{ "file" : "urn:adsk.objects:os.object:'+$bucketName+'/'+$file.Name+'", "metadata" : { "childPath" : "'+$file.Name+'", "parentPath" : "'+$assembly.Name+'" } },'

$json = $json.Substring(0,$json.Length-1)
$json += ']}'
#set references
Invoke-RestMethod -Uri "" -ContentType "application/json" -Method Post -Body $json -Headers @{"Authorization"=$auth}
#register view service
foreach ($uploadedFile in $uploadedFiles) {
  $base64 = [convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($uploadedFile.objects[0].id))
  $json = '{"urn":"'+$base64+'"}'
  $result = Invoke-RestMethod -Uri "" -ContentType "application/json" -Method Post -Body $json -Headers @{"Authorization"=$auth}
  echo "$($uploadedFile.objects[0].key) = $($result.Result)"
#get status
foreach ($uploadedFile in $uploadedFiles) {
  $base64 = [convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($uploadedFile.objects[0].id))
  $status.progress = ""
  while ($status.progress -ne "complete") {
    $status = Invoke-RestMethod -Uri "$base64/status" -ContentType "application/json" -Method Get -Headers @{"Authorization"=$auth}
    Start-Sleep -Seconds 1
  echo "$($uploadedFile.objects[0].key) = $($status.progress)"

echo "Access token: $($login.access_token)"
$file = $uploadedFiles[1].objects[0]
$base64 = [convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($
echo "$($file.key) urn: $base64"

As mentioned in my previous post, you’ll need PowerShell 3.0 (default on Windows 8), as it provides the Invoke-RestMethod.

The first step is to login into the view and data API backend. We do this with this command-let

Invoke-RestMethod -Uri &quot;; -ContentType &quot;application/x-www-form-urlencoded&quot; -Method Post -Body &quot;client_id=$ConsumerKey&amp;client_secret=$ConsumerSecret&amp;grant_type=client_credentials&quot;

Then we create a folder (bucket) where we can upload all our files, like this

Invoke-RestMethod -Uri &quot;; -ContentType &quot;application/json&quot; -Method Post -Body $json -Headers @{&quot;Authorization&quot;=$auth}

Unfortunately, there is no way (yet) to check whether the bucket is already created or not, so the first time we execute the command above, everything should be fine, but the next time the commandlets returns an error

Invoke-RestMethod : {&quot;reason&quot;:&quot;Bucket already exists&quot;}

The script will continue anyway, but the error message is annoying. Hopefully there will be a call for testing a bucket existence or retrieve the list of buckets soon.

Then, we upload the assembly

Invoke-RestMethod -Uri &quot;$bucketName/objects/$($assembly.Name.ToLower())&quot; -ContentType &quot;application/octet-stream&quot; -Method Put -Headers @{&quot;Authorization&quot;=$auth;&quot;Content-Length&quot;=$assembly.Length;&quot;Expect&quot;=&quot;&quot;} -InFile $assembly.FullName.ToLower()

We will repeat this operation for all the children as well. Once the upload is completed, we will set the references between the parts. The according json string has been created while uploading the files.

Invoke-RestMethod -Uri &quot;; -ContentType &quot;application/json&quot; -Method Post -Body $json -Headers @{&quot;Authorization&quot;=$auth}
So, now that all the data is in the cloud and the references are set, we can start the transformation of our files into to a web suitable format. We do this with the registration service, for each file, like this:
Invoke-RestMethod -Uri &quot;; -ContentType &quot;application/json&quot; -Method Post -Body $json -Headers @{&quot;Authorization&quot;=$auth}

At the end, we will check the status of the transformation. This is not mandatory, but as some file may take a bit longer, this way, we can wait until everything is done.

Invoke-RestMethod -Uri &quot;$base64/status&quot; -ContentType &quot;application/json&quot; -Method Get -Headers @{&quot;Authorization&quot;=$auth}

The last two lines of the script just prints out the token and the assembly URN that might be helpful in case you like to use them in your HTML code for showing the assembly.

There are more API calls, but i hope that with this simple sample, you have something that works and might be useful, and you have a feel of how to use PowerShell for consuming the View And Data API.

This entry was posted in PowerShell, ViewAndDataAPI. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s