[Backend]修改forge相關參數的位置及對應的撈取方式

This commit is contained in:
張家睿 2024-04-08 10:16:56 +08:00
parent fcbb74d411
commit 5365ab66c0
6 changed files with 36 additions and 16 deletions

View File

@ -1,6 +1,7 @@
using Autodesk.Forge; using Autodesk.Forge;
using Autodesk.Forge.Model; using Autodesk.Forge.Model;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -9,6 +10,9 @@ namespace forgeSample.Controllers
[ApiController] [ApiController]
public class ModelDerivativeController : ControllerBase public class ModelDerivativeController : ControllerBase
{ {
private static IConfiguration _configuration;
public ModelDerivativeController(IConfiguration configuration) { _configuration = configuration; }
/// <summary> /// <summary>
/// Start the translation job for a give bucketKey/objectName /// Start the translation job for a give bucketKey/objectName
/// </summary> /// </summary>
@ -18,7 +22,7 @@ namespace forgeSample.Controllers
[Route("api/forge/modelderivative/jobs")] [Route("api/forge/modelderivative/jobs")]
public async Task<dynamic> TranslateObject([FromBody] TranslateObjectModel objModel) public async Task<dynamic> TranslateObject([FromBody] TranslateObjectModel objModel)
{ {
dynamic oauth = await OAuthController.GetInternalAsync(); dynamic oauth = await OAuthController.GetInternalAsync(_configuration);
// prepare the payload // prepare the payload
List<JobPayloadItem> outputs = new List<JobPayloadItem>() List<JobPayloadItem> outputs = new List<JobPayloadItem>()

View File

@ -1,5 +1,6 @@
using Autodesk.Forge; using Autodesk.Forge;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -14,6 +15,9 @@ namespace forgeSample.Controllers
// them after the expires_in time (in seconds) // them after the expires_in time (in seconds)
private static dynamic InternalToken { get; set; } private static dynamic InternalToken { get; set; }
private static dynamic PublicToken { get; set; } private static dynamic PublicToken { get; set; }
private static IConfiguration _configuration;
public OAuthController(IConfiguration configuration) { _configuration = configuration; }
/// <summary> /// <summary>
/// Get access token with public (viewables:read) scope /// Get access token with public (viewables:read) scope
@ -24,7 +28,7 @@ namespace forgeSample.Controllers
{ {
if (PublicToken == null || PublicToken.ExpiresAt < DateTime.UtcNow) if (PublicToken == null || PublicToken.ExpiresAt < DateTime.UtcNow)
{ {
PublicToken = await Get2LeggedTokenAsync(new Scope[] { Scope.ViewablesRead }); PublicToken = await Get2LeggedTokenAsync(_configuration, new Scope[] { Scope.ViewablesRead });
PublicToken.ExpiresAt = DateTime.UtcNow.AddSeconds(PublicToken.expires_in); PublicToken.ExpiresAt = DateTime.UtcNow.AddSeconds(PublicToken.expires_in);
} }
return PublicToken; return PublicToken;
@ -33,11 +37,11 @@ namespace forgeSample.Controllers
/// <summary> /// <summary>
/// Get access token with internal (write) scope /// Get access token with internal (write) scope
/// </summary> /// </summary>
public static async Task<dynamic> GetInternalAsync() public static async Task<dynamic> GetInternalAsync(IConfiguration Configuration)
{ {
if (InternalToken == null || InternalToken.ExpiresAt < DateTime.UtcNow) if (InternalToken == null || InternalToken.ExpiresAt < DateTime.UtcNow)
{ {
InternalToken = await Get2LeggedTokenAsync(new Scope[] { Scope.BucketCreate, Scope.BucketRead, Scope.BucketDelete, Scope.DataRead, Scope.DataWrite, Scope.DataCreate, Scope.CodeAll }); InternalToken = await Get2LeggedTokenAsync(Configuration, new Scope[] { Scope.BucketCreate, Scope.BucketRead, Scope.BucketDelete, Scope.DataRead, Scope.DataWrite, Scope.DataCreate, Scope.CodeAll });
InternalToken.ExpiresAt = DateTime.UtcNow.AddSeconds(InternalToken.expires_in); InternalToken.ExpiresAt = DateTime.UtcNow.AddSeconds(InternalToken.expires_in);
} }
@ -47,13 +51,13 @@ namespace forgeSample.Controllers
/// <summary> /// <summary>
/// Get the access token from Autodesk /// Get the access token from Autodesk
/// </summary> /// </summary>
private static async Task<dynamic> Get2LeggedTokenAsync(Scope[] scopes) private static async Task<dynamic> Get2LeggedTokenAsync(IConfiguration Configuration, Scope[] scopes)
{ {
TwoLeggedApi oauth = new TwoLeggedApi(); TwoLeggedApi oauth = new TwoLeggedApi();
string grantType = "client_credentials"; string grantType = "client_credentials";
dynamic bearer = await oauth.AuthenticateAsync( dynamic bearer = await oauth.AuthenticateAsync(
GetAppSetting("FORGE_CLIENT_ID"), GetAppSetting(Configuration, "Forge:CLIENT_ID"),
GetAppSetting("FORGE_CLIENT_SECRET"), GetAppSetting(Configuration, "Forge:CLIENT_SECRET"),
grantType, grantType,
scopes); scopes);
return bearer; return bearer;
@ -62,9 +66,9 @@ namespace forgeSample.Controllers
/// <summary> /// <summary>
/// Reads appsettings from web.config /// Reads appsettings from web.config
/// </summary> /// </summary>
public static string GetAppSetting(string settingKey) public static string GetAppSetting(IConfiguration Configuration, string settingKey)
{ {
return Environment.GetEnvironmentVariable(settingKey).Trim(); return Configuration.GetValue<string>(settingKey).Trim();
} }
} }
} }

View File

@ -3,6 +3,7 @@ using Autodesk.Forge.Model;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -13,8 +14,11 @@ namespace forgeSample.Controllers
public class OSSController : ControllerBase public class OSSController : ControllerBase
{ {
private IWebHostEnvironment _env; private IWebHostEnvironment _env;
public OSSController(IWebHostEnvironment env) { _env = env; } private static IConfiguration _configuration;
public string ClientId { get { return OAuthController.GetAppSetting("FORGE_CLIENT_ID").ToLower(); } }
public OSSController(IWebHostEnvironment env, IConfiguration configuration) { _env = env; _configuration = configuration; }
public string ClientId { get { return _configuration.GetValue<string>("Forge:CLIENT_ID").ToLower(); } }
/// <summary> /// <summary>
/// Return list of buckets (id=#) or list of objects (id=bucketKey) /// Return list of buckets (id=#) or list of objects (id=bucketKey)
@ -24,7 +28,7 @@ namespace forgeSample.Controllers
public async Task<IList<TreeNode>> GetOSSAsync(string id) public async Task<IList<TreeNode>> GetOSSAsync(string id)
{ {
IList<TreeNode> nodes = new List<TreeNode>(); IList<TreeNode> nodes = new List<TreeNode>();
dynamic oauth = await OAuthController.GetInternalAsync(); dynamic oauth = await OAuthController.GetInternalAsync(_configuration);
if (id == "#") // root if (id == "#") // root
{ {
@ -81,7 +85,7 @@ namespace forgeSample.Controllers
public async Task<dynamic> CreateBucket([FromBody] CreateBucketModel bucket) public async Task<dynamic> CreateBucket([FromBody] CreateBucketModel bucket)
{ {
BucketsApi buckets = new BucketsApi(); BucketsApi buckets = new BucketsApi();
dynamic token = await OAuthController.GetInternalAsync(); dynamic token = await OAuthController.GetInternalAsync(_configuration);
buckets.Configuration.AccessToken = token.access_token; buckets.Configuration.AccessToken = token.access_token;
PostBucketsPayload bucketPayload = new PostBucketsPayload(string.Format("{0}-{1}", ClientId, bucket.bucketKey.ToLower()), null, PostBucketsPayload bucketPayload = new PostBucketsPayload(string.Format("{0}-{1}", ClientId, bucket.bucketKey.ToLower()), null,
PostBucketsPayload.PolicyKeyEnum.Transient); PostBucketsPayload.PolicyKeyEnum.Transient);
@ -111,7 +115,7 @@ namespace forgeSample.Controllers
// get the bucket... // get the bucket...
dynamic oauth = await OAuthController.GetInternalAsync(); dynamic oauth = await OAuthController.GetInternalAsync(_configuration);
ObjectsApi objects = new ObjectsApi(); ObjectsApi objects = new ObjectsApi();
objects.Configuration.AccessToken = oauth.access_token; objects.Configuration.AccessToken = oauth.access_token;

View File

@ -82,5 +82,9 @@
"Account": "FMiVG4sIeDFmAUc/8Hn/kw==", // "Account": "FMiVG4sIeDFmAUc/8Hn/kw==", //
"Password": "4+ussQ8rHohjPWpNvoujJQ==" // "Password": "4+ussQ8rHohjPWpNvoujJQ==" //
} }
},
"Forge": {
"CLIENT_ID": "kDOSvxcoLPCFKTncWV9GxgFcGFMkUz9W",
"CLIENT_SECRET": "peaXsHcAorGNdZYd"
} }
} }

View File

@ -64,5 +64,9 @@
"Account": "FMiVG4sIeDFmAUc/8Hn/kw==", // "Account": "FMiVG4sIeDFmAUc/8Hn/kw==", //
"Password": "4+ussQ8rHohjPWpNvoujJQ==" // "Password": "4+ussQ8rHohjPWpNvoujJQ==" //
} }
},
"Forge": {
"CLIENT_ID": "kDOSvxcoLPCFKTncWV9GxgFcGFMkUz9W",
"CLIENT_SECRET": "peaXsHcAorGNdZYd"
} }
} }

View File

@ -287,8 +287,8 @@
//2023-05 三菱 dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6em80emd1eWFsaGpybDVvODF2YTM5cGtkZ3I4MndhbXEtbWl6dWJpc2hpLyVFMyU4MCU5MCVFNSU4RiVCMCVFNSU4QyU5NyVFNCVCOCVBRCVFOCU4RiVCMSVFNSVBNCVBNyVFNiVBOCU5MyVFMyU4MCU5MUFSQyVFOSU5QiU5OSVFNiVBOCVBMSVFNSVCQyU4RitNRVAlRTYlOEIlODYlRTclQjMlQkIlRTclQjUlQjFfJUU5JTlCJUJCJUU2JUEyJUFGJUU3JUI0JUIwJUU3JUFGJTgwJUU3JTg5JTg4Lm53ZA //2023-05 三菱 dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6em80emd1eWFsaGpybDVvODF2YTM5cGtkZ3I4MndhbXEtbWl6dWJpc2hpLyVFMyU4MCU5MCVFNSU4RiVCMCVFNSU4QyU5NyVFNCVCOCVBRCVFOCU4RiVCMSVFNSVBNCVBNyVFNiVBOCU5MyVFMyU4MCU5MUFSQyVFOSU5QiU5OSVFNiVBOCVBMSVFNSVCQyU4RitNRVAlRTYlOEIlODYlRTclQjMlQkIlRTclQjUlQjFfJUU5JTlCJUJCJUU2JUEyJUFGJUU3JUI0JUIwJUU3JUFGJTgwJUU3JTg5JTg4Lm53ZA
//launchViewer("dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6em80emd1eWFsaGpybDVvODF2YTM5cGtkZ3I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Viewer("dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6a2Rvc3Z4Y29scGNma3RuY3d2OWd4Z2ZjZ2Zta3V6OXctZGVtby8lRTMlODAlOTAlRTklQkIlOTElRTQlQjglQjglRTMlODAlOTElRTQlQkElOEMlRTUlQkIlQTArJUU0JUI4JTg5JUU1JUJCJUEwLm53ZA"); launchViewer("dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6a2Rvc3Z4Y29scGNma3RuY3d2OWd4Z2ZjZ2Zta3V6OXctZGVtby8lRTMlODAlOTAlRTklQkIlOTElRTQlQjglQjglRTMlODAlOTElRTQlQkElOEMlRTUlQkIlQTArJUU0JUI4JTg5JUU1JUJCJUEwKCVFNiVCMCVCNCVFNyVBRSVBMSkwMzE2Lm53ZA");
}); });
function move1Floor() { function move1Floor() {
setElevatorSpeed(0.2); setElevatorSpeed(0.2);