Search code examples
autodesk-forgeautodeskautodesk-model-derivative

passing authentication through fetch (Forge API)


I've been testing out the Forge API in JS (.NET)

when I hard code the Bearer Token in to the fetch call it works fine, however when I try to pass it through via another function it gives me errors

here is my fetch

fetch('https://developer.api.autodesk.com/oss/v2/buckets/'+Buk+'/objects?region=EMEA', {
    method: 'GET',
    headers: {
        "Authorization": "Bearer "

    }
})

here is my call that collects the token

function getForgeToken(callback) {
fetch('/api/forge/oauth/token').then(res => {
    res.json().then(data => {
       callback(data.access_token, data.expires_in);
    });
});

}

As you can tell I'm a novice with JS and any guidance on this would be great.


Solution

  • Let me start by saying that accessing Forge APIs directly from the browser is not recommended as it is basically exposing an access token that could potentially have additional privileges aside from "data:read". For that reason it is always better to access the Forge APIs from your server-side code.

    With that said, if you still want to communicate with Forge from the browser, you can the forge-server-utils. This is an unofficial Forge SDK for Node.js that I'm maintaining, and it has an experimental support for use in browsers: https://github.com/petrbroz/forge-server-utils#client-side-experimental.