matthew-vidovic
matthew-vidovic

Reputation: 85

Autodesk-forge viewer: Access token

I was following forge tutorials to embed the forge viewer in an html page. I ended up at this forge-made page, link: https://autodesk-forge.github.io/forge-tutorial-postman/display_svf.html I understand how to retrieve an access token using cURL however I would like to modify that website so that I don't have to enter the access token myself. I would like the access-token from the cURL response to be automatically imported as the access token for that website. How is this possible. The code for the webpage is here: https://github.com/Autodesk-Forge/forge-tutorial-postman/blob/master/docs/display_svf.html How can I add a function/method to automatically retrieve an access token when I hit submit on the webpage. Any help is much appeciated! Cheers!

Upvotes: 0

Views: 990

Answers (1)

Rahul Bhobe
Rahul Bhobe

Reputation: 4451

The server side code you are looking for is:

app.get('/api/forge/oauth', function (req, res) {
    Axios({
        method: 'POST',
        url: 'https://developer.api.autodesk.com/authentication/v1/authenticate',
        headers: {
            'content-type': 'application/x-www-form-urlencoded',
        },
        data: querystring.stringify({
            client_id: FORGE_CLIENT_ID,
            client_secret: FORGE_CLIENT_SECRET,
            grant_type: 'client_credentials',
            scope: scopes
        })
    })
        .then(function (response) {
            // Success
            access_token = response.data.access_token;
            console.log(response);
            res.send('<p>Authentication success!</p>');
        })
        .catch(function (error) {
            // Failed
            console.log(error);
            res.send('Failed to authenticate');
        });
});

Please refer the Forge 2-Legged Authentication tutorials for the code and more details. We also have more tutorials and workflow on Learn Autodesk Forge.

Upvotes: 1

Related Questions