Messing around with
View and Data API Instead of using
this classI thought it was much easier to deal with
public class AuViewerClient
{
HttpClient Client { get; set; }
private static string acessToken = string.Empty;
private static DateTime expireTime = DateTime.MinValue;
private readonly FormUrlEncodedContent _form;
public AuViewerClient(string key, string password, Uri baseAddressUri)
{
Client
= new HttpClient
() {
BaseAddress = baseAddressUri
};
_form
= new FormUrlEncodedContent
(new Dictionary
<string,
string> { { "client_id", key
},
{ "client_secret", password }, { "grant_type", "client_credentials" } });
}
public async Task<string> GetAccessTokenAsync()
{
if (expireTime > DateTime.Now)
{
return acessToken;
}
var response = await GetToken();
if (!response.IsSuccessStatusCode)
{
return String.Empty;
}
expireTime = DateTime.Now.AddMinutes(18);
var tokenResponse = await response.Content.ReadAsAsync<AccessTokenResponse>();
acessToken = tokenResponse.AccessToken;
return acessToken;
}
private async Task<HttpResponseMessage> GetToken()
{
return await Client.PostAsync("authentication/v1/authenticate", _form);
}
public async Task<HttpResponseMessage> Upload(HttpPostedFileBase file, string bucketKey)
{
var token = await GetAccessTokenAsync();
Client
.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue
("Bearer", token
); var objectKey = Uri.EscapeDataString(file.FileName);
var requestMessage
= new HttpRequestMessage
{
Method = HttpMethod.Put,
RequestUri
= new Uri
(Client
.BaseAddress,
"oss/v1/buckets/" + bucketKey
+ "/objects/" + objectKey
),
Content
= new StreamContent
(file
.InputStream) };
requestMessage
.Content.Headers.ContentType = new MediaTypeHeaderValue
(file
.ContentType); requestMessage.Content.Headers.ContentLength = file.ContentLength;
Client.Timeout = TimeSpan.FromSeconds(3600);
return await Client.SendAsync(requestMessage);
}
public async Task<HttpResponseMessage> Register(string fileId)
{
var token = await GetAccessTokenAsync();
Client
.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue
("Bearer", token
); var requestMessage
= new HttpRequestMessage
() {
Method = HttpMethod.Post,
RequestUri
= new Uri
(Client
.BaseAddress,
"viewingservice/v1/register") };
requestMessage.Headers.Add("x-ads-force", "true");
requestMessage
.Content = new StringContent
("{\"urn\":\"" + fileId
+ "\"}", Encoding
.UTF8,
"application/json"); requestMessage
.Content.Headers.ContentType = new MediaTypeHeaderValue
("application/json");
return await Client.SendAsync(requestMessage);
}
public async Task<HttpResponseMessage> GetViewable(string urn)
{
var token = await GetAccessTokenAsync();
Client
.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue
("Bearer", token
); return await Client.GetAsync("viewingservice/v1/" + urn + "/status");
}
}
Here is a drawing and a revit project uploaded, registered, and translated with API.
DRAWING
REVIT PROJECT
Attached is website solution.
A couple notes about website
- The selected model in combobox will be shown in viewer
- Instead of database it uses a text file serialized to Json to keep up with models uploaded so anyone playing with it would not have to mess with creating, attaching, etc... a database
- To upload a view you have to browse to model then click view upload button
Using built-in control reason why 2 buttons and would normally change to a AJAX request.
Which in turn when uploading model there is no feedback for status just will refresh to viewer index page or an error page will show.
When you upload a model successfully it will be added to combobox and for it show properly in viewer it must return success and complete
It uses a user account I created with gmail account so do not upload any models that you would not want anyone else to see.
In almost all examples you see
/// NEVER DO THIS IN REAL APP
/// NEVER STORE PASSWORD INFO IN PUBLIC FILES
So instead of using clause I store info in a file private.config with Build Action set to NONE
If you use the this method with git make sure to add private.config to your list of ignore files.
<appSettings>
<add key="client_id" value="DMxD1g5NVG5A4HALFe6925ADCUEMphgw" />
<add key="client_secret" value="GnqG5LgYmZLmPFUM" />
<add key="bucket_key" value="testertransientbucket" />
<add key="redirect_url" value="https://developer.api.autodesk.com" />
</appSettings>
Inside the web.config it just reference the file which is not published or copied with version control which you need to set.
<appSettings file="Private.config">
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
Once published for example Azure will let you add the keys and values by logging in and adding them through portal allowing you to test and never expose info publicly.
Project should work with IE11, firefox, and chrome and visual studio 2013.
Do not think any setup or settings required except if you use IE11 will need to set these setting mentioned in
ADN blogLet me know if anyone has problems getting working and needs help.
Having problem attaching project so here is dropbox temporarily until get it attached.
Download Solution