0

I want to use autodesk model derivative api to show DWG file in forge viewer. In

result = _client.Execute(objReq);
            if (result.StatusCode == System.Net.HttpStatusCode.OK || result.StatusCode==System.Net.HttpStatusCode.Created)
            { 
            }

I get badrequest.

   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;
        }
    }
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

Could you try this again, we had some down time from our side during that time, but everything should be back up and running for you. In case does not work I will take a look at your request.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jaime Rosales
  • 1,106
  • 1
  • 6
  • 8
  • Hello. Thnak you for your reply. Actually I want to calculate area and Quantity surveying and estimating from forge viewer. Can I do this with autodesk apis? If yes, please help me how to do that – user3166544 Nov 15 '16 at 09:54
  • Hi, the original question was about down time on the server and getting a failed status with your request. If you need help with a different question, could you submit a separate request and explain in more detail what you are trying to do. from what I read on the request above I don't quite get what you are trying to do. – Jaime Rosales Nov 28 '16 at 15:00