我有一个Django应用程序,正在使用.stl,.stp和.igs文件。我必须在HTML模板中显示这些文件,而我正试图为此使用AutoDesk API。我先创建了一个帐户并进行了身份验证,但是在显示图像时遇到了麻烦。
以下是包含身份验证并将图像上传到存储桶的视图:
def viewer(request):
req = { 'client_id' : CLIENT_ID, 'client_secret': CLIENT_SECRET, 'grant_type' : 'client_credentials','scope':'code:all bucket:create bucket:read data:read data:write'}
resp = requests.post(BASE_URL+'/authentication/v1/authenticate', req)
if resp.status_code == 200:
TOKEN = resp.json()['token_type'] + " " + resp.json()['access_token']
else:
print('Get Token Failed! status = {0} ; message = {1}'.format(resp.status_code,resp.text))
TOKEN = ""
filepath = "C:/Users/imgea/desktop/"
filename = '55.stl'
my_file = Path(filepath + filename)
#check package file (*.zip) exists
if my_file.is_file():
filesize = os.path.getsize(filepath + filename)
# encoding the file name
if sys.version_info[0] < 3:
encodedfilename= urllib.pathname2url(filename)
else:
encodedfilename= urllib.parse.quote(filename)
resp = requests.put(BASE_URL + '/oss/v2/buckets/'+'bucket'+'/objects/'+ encodedfilename, headers={'Authorization': TOKEN,'Content-Type' : 'application/octet-stream','Content-Length' : str(filesize)},data= open(filepath+filename, 'rb'))
if resp.status_code == 200:
urn = resp.json()['objectId']
else:
print('Upload File to Bucket of Data Management Failed! status ={0} ; message = {1}'.format(resp.status_code,resp.text ))
urn = "none"
else:
print(' ' + filename + ' does not exist')
urn = "none"
urn_bytes = urn.encode('ascii')
base64_bytes = base64.b64encode(urn_bytes)
base64_urn = base64_bytes.decode('ascii')
headers = {
'Authorization': TOKEN,
'Content-Type': 'application/json',
}
data = '{"input": { "urn": "' + base64_urn + '"},"output": {"destination": {"region": "us" }, "formats": [{ "type": "obj" }]}}'
resp = requests.post(BASE_URL + '/modelderivative/v2/designdata/job', headers=headers, data=data)
context = {
'token': "'"+TOKEN+"'",
'urn': "'"+urn+"'",
}
return render(request, 'viewer.html', context)
这是模板:
<body>
<!-- The Viewer will be instantiated here -->
<div id="MyViewerDiv"></div>
<!-- The Viewer JS -->
<script src="https://developer.api.autodesk.com/modelderivative/v2/viewers/6.*/viewer3D.min.js"></script>
<!-- Developer JS -->
<script>
var viewer;
var options = {
env: 'AutodeskProduction',
getAccessToken: function(onGetAccessToken) {
var accessToken = {{token|safe}};
var expireTimeSeconds = 86400;
onGetAccessToken(accessToken, expireTimeSeconds);
},
api: 'derivativeV2' // for models uploaded to EMEA change this option to 'derivativeV2_EU'
};
var documentId = {{urn|safe}};
Autodesk.Viewing.Initializer(options, function onInitialized(){
Autodesk.Viewing.Document.load(documentId, onDocumentLoadSuccess, onDocumentLoadFailure);
});
function onDocumentLoadSuccess(doc) {
// A document contains references to 3D and 2D geometries.
var geometries = doc.getRoot().search({'type':'geometry'});
if (geometries.length === 0) {
console.error('Document contains no geometries.');
return;
}
// Choose any of the avialable geometries
var initGeom = geometries[0];
// Create Viewer instance
var viewerDiv = document.getElementById('MyViewerDiv');
var config = {
extensions: initGeom.extensions() || []
};
viewer = new Autodesk.Viewing.Private.GuiViewer3D(viewerDiv, config);
// Load the chosen geometry
var svfUrl = doc.getViewablePath(initGeom);
var modelOptions = {
sharedPropertyDbPath: doc.getPropertyDbPath()
};
viewer.start(svfUrl, modelOptions, onLoadModelSuccess, onLoadModelError);
}
function onDocumentLoadFailure(viewerErrorCode) {
console.error('onDocumentLoadFailure() - errorCode:' + viewerErrorCode);
}
function onLoadModelSuccess(model) {
console.log('onLoadModelSuccess()!');
console.log('Validate model loaded: ' + (viewer.model === model));
console.log(model);
}
function onLoadModelError(viewerErrorCode) {
console.error('onLoadModelError() - errorCode:' + viewerErrorCode);
}
</script>
</body>
根据这些代码块,我可以成功获取访问令牌和图像urn。同时,它们被正确发送到模板。但我有一个错误
{ "developerMessage":"Token is not provided in the request.", "moreInfo": "https://forge.autodesk.com/en/docs/oauth/v2/developers_guide/error_handling/", "errorCode": "AUTH-010"}
。如何解决此问题并以HTML显示3D文件?
答案 0 :(得分:0)
请注意,在将令牌添加到授权标头时,您必须在令牌前添加“ Bearer”,因此标头应为"Authorization": "<token>"
,而不是"Authorization": "Bearer <token>"
。