由于身份验证,可以加载模型

时间:2017-01-06 13:08:48

标签: autodesk-forge

我只是按照startup tutorial加载模型。

首先,我有一个来自autodesk开发人员的现有client_id和client_secret,然后使用client_id和client_secret建立一个基于快速的应用程序,以便检索访问令牌,例如

var config ={
    credentials: {
        client_id:  'xxxxxxxx',
        client_secret:  'xxxxxxx',
        grant_type: 'client_credentials',
        scope:'data:read data:write data:create bucket:create bucket:read'
    },
    BaseEndPoint: 'https://developer.api.autodesk.com',
    Version: 'v1'
} ;
config.AuthenticateEndPoint =config.BaseEndPoint + '/authentication/' + config.Version + '/authenticate' ;

unirest.post (config.AuthenticateEndPoint)
    .header ('Accept', 'application/json')
    .send (config.credentials)
    .end (function (response) {

    }

{"access_token":"ruTBP6POxlpcy8HK2KlWzoFu61oE","token_type":"Bearer","expires_in":86399}

然后将此访问令牌发送回简单的html客户端。

<!DOCTYPE html>
<html>
  <head>
    <title>Very Basic 3D Viewer</title>
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=no" />
    <meta charset="utf-8">


    <script   src="js/jquery-3.1.1.min.js" ></script>

    <!-- The Viewer CSS -->
    <link rel="stylesheet" href="https://developer.api.autodesk.com/viewingservice/v1/viewers/style.min.css" type="text/css">
    <link rel="stylesheet" href="https://developer.api.autodesk.com/viewingservice/v1/viewers/A360.css" type="text/css">
  </head>

  <body>
    <div id="viewer"></div>
    <!-- The Viewer JS -->
    <script src="https://developer.api.autodesk.com/viewingservice/v1/viewers/three.min.js?v=v1.2.22"></script>
    <script src="https://developer.api.autodesk.com/viewingservice/v1/viewers/viewer3D.min.js?v=v1.2.22"></script>
    <script src="https://developer.api.autodesk.com/viewingservice/v1/viewers/Autodesk360App.js"></script>

    <!-- Developer JS -->
    <script>
        $(document).ready(function () {

                var viewerApp;
                var options = {
                    env: 'AutodeskProduction',
                    accessToken: 'YOUR ACCESS TOKEN'
                };

                var documentId = 'YOUR BASE 64 ENCODED URN';

                $.getJSON( 'http://'+window.location.host+ "/gettoken", function( data ) {
                    console.log(data);

                    options.accessToken = data.accessToken;
                    documentId = data.urn;
                    options.document = data.urn;
                });


                console.log(options.accessToken, documentId);

                Autodesk.Viewing.Initializer(options, function onInitialized(){
                    viewerApp = new Autodesk.A360ViewingApplication('viewer');
                    //viewerApp.registerViewer(viewerApp.k3D, Autodesk.Viewing.Private.GuiViewer3D);
                    //viewerApp.loadDocumentWithItemAndObject(documentId);
                });


        });


    </script>
  </body>
</html>

问题发生在客户端,可以成功获取访问令牌。但是这给了我一个错误

'POST https://developer.api.autodesk.com/utility/v1/settoken 401(未经授权)'

        Autodesk.Viewing.Initializer(options, function onInitialized(){
            viewerApp = new Autodesk.A360ViewingApplication('viewer');
            //viewerApp.registerViewer(viewerApp.k3D, Autodesk.Viewing.Private.GuiViewer3D);
            //viewerApp.loadDocumentWithItemAndObject(documentId);
        });

我无法弄清楚问题是什么,api或客户端或服务器端有问题?

注意:在注册开发人员api时,我只是将回调命名为http://localhost:3000,因为目前我在本地环境中测试它,问题是什么?

1 个答案:

答案 0 :(得分:0)

这里的代码

var config ={
    credentials: {
        client_id:  'xxxxxxxx',
        client_secret:  'xxxxxxx',
        grant_type: 'client_credentials',
        scope:'data:read data:write data:create bucket:create bucket:read'
    },
    BaseEndPoint: 'https://developer.api.autodesk.com',
    Version: 'v1'
} ;
config.AuthenticateEndPoint =config.BaseEndPoint + '/authentication/' + config.Version + '/authenticate' ;

unirest.post (config.AuthenticateEndPoint)
    .header ('Accept', 'application/json')
    .send (config.credentials)
    .end (function (response) {

    }

是对的。您获得了有效的访问令牌,我假设您从node.js服务器运行此代码。在您的服务器上,您实现了一个端点,即/ gettoken,您的客户端应用程序将调用该端点以获取返回到您的页面的访问令牌,从而初始化查看器。到目前为止一切都很好。

但是,当您考虑客户端上的呼叫顺序时,就会出现问题。

$(document).ready(function () {

意味着你的代码将在DOM准备就绪时执行 - 这很好。 在这里,您初始化变量:

var options = {
    env: 'AutodeskProduction',
    accessToken: 'YOUR ACCESS TOKEN'
};

var documentId = 'YOUR BASE 64 ENCODED URN';

直到这里仍然可以,但请注意,accessToken和documentId都无效。

接下来,使用$ .getJSON()查询访问令牌,这是一种调用端点的异步方式。这意味着在您阅读回复之前,此函数会立即返回。

因此,下一个执行的代码不是回调函数中的代码,而是执行此代码:

console.log(options.accessToken, documentId);

Autodesk.Viewing.Initializer(options, function onInitialized(){
    viewerApp = new Autodesk.A360ViewingApplication('viewer');
    //viewerApp.registerViewer(viewerApp.k3D, Autodesk.Viewing.Private.GuiViewer3D);
    //viewerApp.loadDocumentWithItemAndObject(documentId);
});

此时,accessToken和documentId仍然获得无效值,这将导致您的代码失败。简而言之,您需要从回调初始化Viewer以等待/ gettoken响应返回。

$.getJSON( 'http://'+window.location.host+ "/gettoken", function( data ) {
    console.log(data);

    options.accessToken = data.accessToken;
    documentId = data.urn;
    options.document = data.urn;

    Autodesk.Viewing.Initializer(options, function onInitialized(){
        viewerApp = new Autodesk.A360ViewingApplication('viewer');
        //viewerApp.registerViewer(viewerApp.k3D, Autodesk.Viewing.Private.GuiViewer3D);
        //viewerApp.loadDocumentWithItemAndObject(documentId);
    });
});