我想使用 Autodesk 模型衍生 API 在 Forge 查看器中显示 DWG 文件。在
result = _client.Execute(objReq);
if (result.StatusCode == System.Net.HttpStatusCode.OK || result.StatusCode==System.Net.HttpStatusCode.Created)
{
}
我收到了不好的请求。
public virtual ActionResult Index()
{
SetupViewer();
if (_bucketFound)
{
//upload file
RestRequest uploadReq = new RestRequest();
uploadReq.Resource = "oss/v2/buckets/"+_bucketName+"/objects/"+_filepath;
uploadReq.Method = Method.PUT;
uploadReq.AddHeader("Content-Type", _fileContentType);
uploadReq.AddParameter("Authorization", "Bearer " + _accessToken, ParameterType.HttpHeader);
var result= _client.Execute(uploadReq);
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
String responseString = result.Content;
int len = responseString.Length;
int objectKeyIndex = responseString.IndexOf("\"objectKey\" : \"");
int index = responseString.IndexOf("urn:");
responseString = responseString.Substring(index, objectKeyIndex - index-5).Replace("urn:", "").Trim();
_fileUrn = "urn:"+responseString;
//convert urn to base64
string base64Urn = Base64Convertor.Base64Encode(_fileUrn);
// Remove ending '=' signs
// Use _ instead of /
// Use - insteaqd of +
base64Urn = base64Urn.Replace("=", "");
//translate to SVF format
//RestRequest svfReq = new RestRequest();
//svfReq.Resource = "modelderivative/v2/designdata/job";
//svfReq.Method = Method.POST;
//svfReq.AddParameter("Authorization", "Bearer " + _accessToken, ParameterType.HttpHeader);
//svfReq.AddParameter("Content-Type", "application/json;charset=utf-8", ParameterType.HttpHeader);
//string body = "{\"input\":{\"urn\":\"" + base64Urn + "\",\"compressedUrn\":true,\"rootFilename\":\""+_filepath+ "\"},\"output\":{\"formats\":[{\"type\":\"svf\",\"views\":[\"2d\"]}]}}";
//svfReq.AddParameter("application/json", body, ParameterType.RequestBody);
// translate to OBJ format
RestRequest objReq = new RestRequest();
objReq.Resource = "modelderivative/v2/designdata/job";
objReq.Method = Method.POST;
objReq.AddParameter("Authorization", "Bearer " + _accessToken, ParameterType.HttpHeader);
objReq.AddParameter("Content-Type", "application/json", ParameterType.HttpHeader);
string body = "{\"input\":{\"urn\":\"" + base64Urn + "\"},\"output\":{\"formats\":[{\"type\":\"obj\"}]}}";
result = _client.Execute(objReq);
if (result.StatusCode == System.Net.HttpStatusCode.OK || result.StatusCode==System.Net.HttpStatusCode.Created)
{
//check the transition complete
RestRequest checkTransitionCompleteGetReq = new RestRequest();
checkTransitionCompleteGetReq.Resource = "modelderivative/v2/designdata/"+base64Urn+"/manifest";
checkTransitionCompleteGetReq.Method = Method.GET;
checkTransitionCompleteGetReq.AddParameter("Authorization", "Bearer " + _accessToken, ParameterType.HttpHeader);
var result2 = _client.Execute(checkTransitionCompleteGetReq);
if (result2.StatusCode == System.Net.HttpStatusCode.OK)
{
ViewBag.BucketFound = result.Content;
}
}
}
}
return View();
}
void SetupViewer()
{
// Authentication
bool authenticationDone = false;
RestRequest authReq = new RestRequest();
authReq.Resource = "authentication/v1/authenticate";
authReq.Method = Method.POST;
authReq.AddHeader("Content-Type", "application/x-www-form-urlencoded");
authReq.AddParameter("client_id", ConfigurationManager.AppSettings["ClientId"]);
authReq.AddParameter("client_secret", ConfigurationManager.AppSettings["ClientSecret"]);
authReq.AddParameter("grant_type", "client_credentials");
authReq.AddParameter("scope", "bucket:create bucket:read data:write data:read");
IRestResponse result = _client.Execute(authReq);
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
String responseString = result.Content;
int len = responseString.Length;
int index = responseString.IndexOf("\"access_token\":\"") + "\"access_token\":\"".Length;
responseString = responseString.Substring(index, len - index - 1);
int index2 = responseString.IndexOf("\"");
_accessToken = responseString.Substring(0, index2);
//Set the token.
RestRequest setTokenReq = new RestRequest();
setTokenReq.Resource = "utility/v1/settoken";
setTokenReq.Method = Method.POST;
setTokenReq.AddHeader("Content-Type", "application/x-www-form-urlencoded");
setTokenReq.AddParameter("access-token", _accessToken);
IRestResponse resp = _client.Execute(setTokenReq);
if (resp.StatusCode == System.Net.HttpStatusCode.OK)
{
authenticationDone = true;
}
}
if (!authenticationDone)
{
ViewData["Message"] = "View and Data client authentication failed !";
_accessToken = String.Empty;
return;
}
RestRequest bucketReq = new RestRequest();
bucketReq.Resource = "oss/v2/buckets";
bucketReq.Method = Method.POST;
bucketReq.AddParameter("Authorization", "Bearer " + _accessToken, ParameterType.HttpHeader);
bucketReq.AddParameter("Content-Type", "application/json", ParameterType.HttpHeader);
//bucketname is the name of the bucket.
string body = "{\"bucketKey\":\"" + _bucketName + "\",\"policyKey\":\"transient\"}";
bucketReq.AddParameter("application/json", body, ParameterType.RequestBody);
result = _client.Execute(bucketReq);
if (result.StatusCode == System.Net.HttpStatusCode.Conflict ||
result.StatusCode == System.Net.HttpStatusCode.OK)
{
// _bucketFound = true;
//Check bucket
RestRequest bucketGetReq = new RestRequest();
bucketGetReq.Resource = "oss/v2/buckets";
bucketGetReq.Method = Method.GET;
bucketGetReq.AddParameter("Authorization", "Bearer " + _accessToken, ParameterType.HttpHeader);
bucketGetReq.AddParameter("Content-Type", "application/json", ParameterType.HttpHeader);
result = _client.Execute(bucketGetReq);
if (result.StatusCode == System.Net.HttpStatusCode.OK)
{
_bucketFound = true;
ViewBag.BucketFound = "Found";
}
else
ViewBag.BucketFound = "NotFound";
}
else
{
ViewData["Message"] = "View and Data bucket could not be accessed !";
_bucketFound = false;
return;
}
}