这是我的问题:我目前正在开发Forge Autodesk Web应用程序,它需要令牌才能工作。到目前为止,在手动请求令牌后,我自己在代码中设置了令牌。
我找到了一种通过forge-apis
NodeJS包获取令牌的方法(请检查以下代码)。 console.log
显示正确的令牌。
function getToken() {
var ForgeSDK = require('forge-apis');
var CLIENT_ID = '###' , CLIENT_SECRET = '###';
var token;
// Initialize the 2-legged OAuth2 client, set specific scopes and optionally set the `autoRefresh` parameter to true
// if you want the token to auto refresh
var autoRefresh = true; // or false
var oAuth2TwoLegged = new ForgeSDK.AuthClientTwoLegged(CLIENT_ID, CLIENT_SECRET, [
'data:read',
'data:write'
], autoRefresh);
oAuth2TwoLegged.authenticate().then(function(credentials){
console.log(credentials);
token = credentials.access_token;
// The `credentials` object contains an access_token that is being used to call the endpoints.
// In addition, this object is applied globally on the oAuth2TwoLegged client that you should use when calling secure endpoints.
}, function(err){
console.error(err);
});
return token;
};
console.log(getToken());
console.log(credentials);
返回的值的示例是Object {access_token: "eyJhbGciOiJIUzI1NiIsImtpZCI6Imp3dF9zeW1tZXRyaWNfa2…", token_type: "Bearer", expires_in: 3599, expires_at: Wed Jun 19 2019 12:29:20 GMT+0200 (GMT+02:00)}
。
我希望方法getToken
返回access_token
,但是返回的值为undefined
。
我不熟悉Web应用程序开发以及Forge的新功能,但是我认为我的错误来自对oAuth2TwoLegged.authenticate().then(function(credentials)
行为的误解。
谢谢!
(PS:此应用程序的目的是探索Forge Autodesk的可能性,我知道由直接在我的代码中设置客户端ID和Secret导致的安全漏洞,我将重点关注这一点。更好地了解其工作原理)