Connect to Fusion Lifecycle from Fusion 360 add-in

Fusion Lifecycle has a RESTful API which can be accessed through simple HTTP requests, and so can be used from a Fusion 360 add-in/script as well:
http://modthemachine.typepad.com/my_weblog/2015/10/call-web-services-from-fusion-add-in.html

I had a bit of difficulty doing HTTP requests from a JavaScript Fusion 360 script, but all is fine from Python

It’s very easy to create a new Python script/add-in inside Fusion 360 and open it for edit.
Once it’s open, you can add the following code that will log into your Fusion Lifecycle tenant.
Note: POST login endpoint help: http://help.autodesk.com/view/PLM/ENU/?guid=GUID-43302513-5120-4A42-8804-EF835D97D10D

You just have to set the correct userID, password, and tenant URL in the code:

#Author-
#Description-
import adsk.core, adsk.fusion, adsk.cam, traceback, json, http.client
def login():
body = {
"userID": "<your user id>",
"password": "<your password>"
}
h = http.client.HTTPSConnection('<name of your tenant>.autodeskplm360.net')
headers = {
'User-Agent': 'Fusion360',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
h.request('POST', '/rest/auth/1/login', json.dumps(body), headers)
res = h.getresponse()
return res.status, res.reason, res.read()
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui  = app.userInterface
status, response, data = login()
dataString = data.decode('utf-8')
ui.messageBox(dataString)
# parse the json string
dataObject = json.loads(dataString)
ui.messageBox("My sessionid is " + dataObject["sessionid"])
except:
if ui:
ui.messageBox('Failed:n{}'.format(traceback.format_exc()))

This what you get when running the code (if everything goes well):

LoginSuccess

-Adam


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading