使用node.js的简单Forge 3格Oauth



我是Forge的新手,使用node.js-我很难让一个简单的三级Oauth进程工作。

这就是我在下面走了多远。它给了我一个错误";无法获取/api/forge/oauth/recallback";

我已经检查了我的回调url是否与Forge应用程序中的内容相匹配。

最终,我试图实现的是,在Fusion团队中获得新创建文件的共享链接,或者至少打开浏览器进入文件概述页面。

有人能帮忙吗?

var express = require('express');
var ForgeSDK = require('forge-apis');
const { stringify } = require('node:querystring');
var opn = require('opn'); 
// Set up Express web server
var app = express();
// Set the Forge Variables
var FORGE_CLIENT_ID = 'client-id', FORGE_CLIENT_SECRET = 'client-secret', REDIRECT_URL = 'http://localhost:3000/api/forge/oauth/callback';
// This is for web server to start listening to port 3000
app.set('port', 3000);
var server = app.listen(app.get('port'), function () {
console.log('Server listening on port ' + server.address().port);
});
// Initialize the 3-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;
var oAuth2ThreeLegged = new ForgeSDK.AuthClientThreeLegged(FORGE_CLIENT_ID, FORGE_CLIENT_SECRET, REDIRECT_URL, [
'data:read',
'data:write'
], autoRefresh);
// Generate a URL page that asks for permissions for the specified scopes.
var AuthURL = oAuth2ThreeLegged.generateAuthUrl();
console.log(AuthURL)
opn('https://developer.api.autodesk.com/authentication/v1/authorize?response_type=code&client_id=<<client-id>>&redirect_uri=http://localhost:3000/api/forge/oauth/callback&scope=data:read+data:write&state=undefined', {app: 'chrome'}) 

代码中的回调API在哪里?

BTW 3脚oauth在nodejs中实现,您可以将其作为参考:https://learnforge.autodesk.io/#/oauth/3legged/nodejs

问题是您有一个需要等待的回调。我还添加了一个自动刷新令牌的功能:

import * as express from "express";
import * as open from 'open';
const os = require("os");
var ForgeSDK = require('forge-apis');
const fs = require("fs");
import { dirname } from 'path';
//thats the function: I wrapped in a class together with settings. you need also a function to save the credentials as json:
async _3leggedOff(autoRefresh = true, options?) {
// Initialize the 3-legged OAuth2 client, set specific scopes and optionally set the `autoRefresh` parameter to true
var oAuth2ThreeLegged = new ForgeSDK.AuthClientThreeLegged(this._settings.clientId, this._settings.clientSecret, this._settings.callback, this.settings.opts.scope.split(' '), autoRefresh);
//check file exists when autorefresh is enabled
if (autoRefresh && fs.existsSync(this._settings.path_credentials)) {
console.debug("try to read credentials from " + this._settings.path_credentials)
let data = await fs.readFileSync(this._settings.path_credentials, 'utf8');
data = await JSON.parse(data);
console.debug(this._settings.path_credentials + ":" + JSON.stringify(data))
try {
let res = await oAuth2ThreeLegged.refreshToken(data, this.settings.opts.scope.split(' '));
console.log("Refreshed the token successful")
this.credentials_save(res);
return true
}
catch (err) {
console.error(err)
}
}
// Generate a URL page that asks for permissions for the specified scopes.
const url = oAuth2ThreeLegged.generateAuthUrl()
console.debug("url:", url);
//configure and start the server to listen to the callback
const app = express();
let resolve;
const p = new Promise((_resolve) => {
resolve = _resolve;
});
//this assumes the callback url is localhost/oauth
app.get('/oauth', function (req, res) {
resolve(req.query.code);
res.end('');
});
const server = await app.listen(this._settings.PORT);
console.debug("Started localhost", this._settings.PORT, "waiting for callback ", this._settings.callback);
//open the browser to login
open(url);
// Wait for the first auth code on the port of the localhost
const returned_code = await p;
//close the server because else cli would never exit
await server.close();
console.debug("retured:", returned_code)
//get the token for the credentials
oAuth2ThreeLegged.getToken(returned_code).then(function (credentials) {
// The `credentials` object contains an `access_token` and an optional `refresh_token` that you can use to call the endpoints.
this.credentials_save(credentials);
return true
}, function (err) {
console.error(err);
});
}

最新更新