如何使用三足令牌将文件上传到 Autodesk Forge 中的存储位置?

How to upload file to storage location in Autodesk Forge using 3-legged Token?

我通过创建存储位置 API 生成了 accessToken 三足令牌,bucketKeyobjectName,如 this tutorial(step 4) 所示。

我想上传文件到存储location.My请求returns'OK 200'状态,但它没有上传文件到A360,我需要。 错误在哪里?如何使用三足Token正确上传文件到用户存储位置?

    [HttpGet]
    public JObject UploadFile(string accessToken,string bucketKey,string objectName,string filePath)
    {                      
        var url = String.Format("https://developer.api.autodesk.com/oss/v2/buckets/{0}/objects/{1}", bucketKey, objectName);

        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            var response = client.PutAsync(url, new StreamContent(System.IO.File.OpenRead(filePath))).Result.EnsureSuccessStatusCode();
            System.Diagnostics.Debug.WriteLine(response);
        }

    }

请求必须 returns JSON 带有创建文件的参数,但是 returns:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Access-Control-Allow-Credentials: true
  Access-Control-Allow-Headers: Authorization, Accept-Encoding, Range, Content-Type
  Access-Control-Allow-Methods: GET
  Access-Control-Allow-Origin: *
  Connection: keep-alive
  Date: Wed, 11 Jan 2017 15:36:21 GMT
  Server: Apigee
  Server: Router
  Content-Length: 440
  Content-Type: application/json; charset=utf-8
}

我之前已经将它的文件上传到我的 a360 hub,按照该教程中的步骤没有问题,您是否尝试过仅通过 Rest 调用或 cURL 调用来确保过程正确?我可以告诉你我使用 cURL 的步骤,这样你就可以尝试并告诉我。

注意:使用 cURL 时,我使用 JQ 处理器以更有条理的方式在我的终端中可视化响应。 https://stedolan.github.io/jq/

三足认证

步骤 0.1:将用户引导至授权 Web 流程,设置正确的范围

https://developer.api.autodesk.com/authentication/v1/authorize?response_type=code&client_id=xxxxxxxxxx&redirect_uri=http%3A%2F%2Fxxxxx.com%2Foauth%2Fcallback&scope=data:read data:write bucket:create bucket:read data:create

步骤 0.2:实施提取授权码的代码

代码 = xxxxxxxxxxxxx-xxxxx-xxxxxxxxx

步骤 0.3:交换访问令牌的授权码

curl -v 'https://developer.api.autodesk.com/authentication/v1/gettoken' -X 'POST' -H 'Content-Type: application/x-www-form-urlencoded' -d 'client_id=xxxxxxxxxxxxx&client_secret=xxxxxxxxxxxxxxx&grant_type=authorization_code&code=xxxxxxxxx-xxxxxxx&redirect_uri=http://xxxxxx.com/oauth/callback' | jq '.'

回应

{ "access_token": "OC5yVHwVyjZRfxQhXJxxxxxxxxx", "refresh_token": "o3pKZaUvEsIozWwdyldoHGmLjxxxxxxxxxxx", "token_type": "Bearer", "expires_in": 86399 }

现在我们有了 3 条腿的令牌,让我们开始将文件上传到 A360 Hub 的步骤

第 1 步:找到拥有您的资源的中心

curl -X GET -H "Authorization: Bearer xxxxxxxxxxxxxx" "https://developer.api.autodesk.com/project/v1/hubs" | jq '.'

响应将是:

{ "jsonapi": { "version": "1.0" }, "links": { "self": { "href": "https://developer.api.autodesk.com/project/v1/hubs" } }, "data": [ { "type": "hubs", "id": "a.cGVyc29uYWw6dWxxxxxxxxxxx", // MAKE NOTE OF THIS ID "attributes": { "name": "autodesk", "extension": { "type": "hubs:autodesk.core:Hub", "version": "1.0", "schema": { "href": "https://developer.api.autodesk.com/schema/v1/versions/hubs%3Aautodesk.core%3AHub-1.0" }, "data": {} } }, "links": { "self": { "href": "https://developer.api.autodesk.com/project/v1/hubs/a.YnVzaW5lc3M6YXV0xxxxxxxxxx" } }, "relationships": { "projects": { "links": { "related": { "href": "https://developer.api.autodesk.com/project/v1/hubs/a.YnVzaW5lc3M6YXV0b2Rlc2sxNDU/projects" } } } } }

第 2 步:找到拥有您的资源的项目

curl -X GET -H "Authorization: Bearer OC5yVHwVyxxxxxxxxxxxx" "https://developer.api.autodesk.com/project/v1/hubs/a.cGVyc29uYWw6dWxxxxxxxxxxx/projects" | jq '.'

回应

{ "jsonapi": { "version": "1.0" }, "links": { "self": { "href": "https://developer.api.autodesk.com/project/v1/hubs/a.cGVyc29uYWw6dWxxxxxxxxxxx/projects" } }, "data": [ { "type": "projects", "id": "a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ", "attributes": { "name": "Design Files to Share", "extension": { "type": "projects:autodesk.core:Project", "version": "1.0", "schema": { "href": "https://developer.api.autodesk.com/schema/v1/versions/projects%3Aautodesk.core%3AProject-1.0" }, "data": {} } }, "links": { "self": { "href": "https://developer.api.autodesk.com/project/v1/hubs/a.cGVyc29uYWw6dWxxxxxxxxxxx/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ" } }, "relationships": { "hub": { "data": { "type": "hubs", "id": "a.cGVyc29uYWw6dWxxxxxxxxxxx" }, "links": { "related": { "href": "https://developer.api.autodesk.com/project/v1/hubs/a.cGVyc29uYWw6dWxxxxxxxxxxx" } } }, "rootFolder": { "data": { "type": "folders", "id": "urn:adsk.wipprod:fs.folder:co.BKklEuaHRsiii-mKNxxxxxx" }, "meta": { "link": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/folders/urn%3Aadsk.wipprod%3Afs.folder%3Aco.BKklEuaHRsiii-mKNxxxxxx" } } }, "topFolders": { "links": { "related": { "href": "https://developer.api.autodesk.com/project/v1/hubs/a.cGVyc29uYWw6dWxxxxxxxxxxx/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/topFolders" } } } } }

//第三步:创建存储位置

curl -X POST -H "Content-Type: application/vnd.api+json" -H "Accept: application/vnd.api+json" -H "Authorization: Bearer OC5yVHwVyjZRfxxxxxxxxx" -d '{
      "jsonapi": { "version": "1.0" },
      "data": {
        "type": "objects",
        "attributes": {
          "name": "lego-racecar.3dm"
        },
        "relationships": {
          "target": {
            "data": { "type": "folders", "id": "urn:adsk.wipprod:fs.folder:co.BKklEuaHRsiii-mKNxxxxxx" }
          }
        }
      }
}' "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/storage" | jq '.'

调用的响应

{ "jsonapi": { "version": "1.0" }, "data": { "type": "objects", "id": "urn:adsk.objects:os.object:wip.dm.prod/6d106e5b-2ac6-41d5-aa1c-b08694xxxxxx.3dm", "relationships": { "target": { "data": { "type": "folders", "id": "urn:adsk.wipprod:fs.folder:co.BKklEuaHRsiii-mKNxxxxxx" }, "links": { "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/folders/urn%3Aadsk.wipprod%3Afs.folder%3Aco.BKklEuaHRsiii-mKNxxxxxx" } } } } } }

第 4 步:将文件上传到存储位置

curl -X PUT -H "Authorization: Bearer OC5yVHwVyjZRfxQhXJjxxxxxxx" --data-binary @lego-racecar.3dm "https://developer.api.autodesk.com/oss/v2/buckets/wip.dm.prod/objects/6d106e5b-2ac6-41d5-aa1c-b08694xxxxxx.3dm" | jq '.'

回应

{ "bucketKey": "wip.dm.prod", "objectId": "urn:adsk.objects:os.object:wip.dm.prod/6d106e5b-2ac6-41d5-aa1c-b08694xxxxxx.3dm", "objectKey": "6d106e5b-2ac6-41d5-aa1c-b08694xxxxxx.3dm", "sha1": "9c8e51403adc6b3bb6f586433bca66689xxxxxx", "size": 107743049,
"contentType": "application/x-www-form-urlencoded", "location": "https://developer.api.autodesk.com/oss/v2/buckets/wip.dm.prod/objects/6d106e5b-2ac6-41d5-aa1c-b08694xxxxxx.3dm" }

最后是第 5 步:创建上传文件的第一个版本

curl -X POST -H "Authorization: Bearer OC5yVHwVyjZRfxQhXJjxxxxxxxx" -H "Content-Type: application/vnd.api+json" -H "Accept: application/vnd.api+json" -d '{
    "jsonapi": { "version": "1.0" },
    "data": {
      "type": "items",
      "attributes": {
        "displayName": "lego-racecar.3dm",
        "extension": {
          "type": "items:autodesk.core:File",
          "version": "1.0"
        }
      },
      "relationships": {
        "tip": {
          "data": {
            "type": "versions", "id": "1"
          }
        },
        "parent": {
          "data": {
            "type": "folders",
            "id": "urn:adsk.wipprod:fs.folder:co.BKklEuaHRsiii-mKNxxxxxx"
          }
        }
      }
    },
    "included": [
      {
        "type": "versions",
        "id": "1",
        "attributes": {
          "name": "lego-racecar.3dm",
          "extension": {
            "type": "versions:autodesk.core:File",
            "version": "1.0"
          }
        },
        "relationships": {
          "storage": {
            "data": {
              "type": "objects",
              "id": "urn:adsk.objects:os.object:wip.dm.prod/6d106e5b-2ac6-41d5-aa1c-b08694xxxxxx.3dm"
            }
          }
        }
      }
    ]
  }' "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/items" | jq '.'

回应

{ "jsonapi": { "version": "1.0" }, "links": { "self": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg" } }, "data": { "type": "items", "id": "urn:adsk.wipprod:dm.lineage:BlMbtGTyQ2eMLvZ05rVvAg", "attributes": { "displayName": "lego-racecar.3dm", "createTime": "2016-11-02T18:49:51+00:00", "createUserId": "TNSUM9VPW79S", "lastModifiedTime": "2016-11-02T18:49:51+00:00", "lastModifiedUserId": "TNSUM9VPW79S", "extension": { "type": "items:autodesk.core:File", "version": "1.0", "schema": { "href": "https://developer.api.autodesk.com/schema/v1/versions/items%3Aautodesk.core%3AFile-1.0" }, "data": {} } }, "links": { "self": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg" } }, "relationships": { "tip": { "data": { "type": "versions", "id": "urn:adsk.wipprod:fs.file:vf.BlMbtGTyQ2eMLvZ05rVvAg?version=1" }, "links": { "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg/tip" } } }, "versions": { "links": { "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg/versions" } } }, "parent": { "data": { "type": "folders", "id": "urn:adsk.wipprod:fs.folder:co.YgCsk6CGSUSTFeexhncYNw" }, "links": { "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg/parent" } } }, "refs": { "links": { "self": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg/relationships/refs" }, "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg/refs" } } }, "links": { "links": { "self": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dWUyOTg4MTEzMSMyMDE2MDYxNTM1MDA2NjA2/items/urn%3Aadsk.wipprod%3Adm.lineage%3ABlMbtGTyQ2eMLvZ05rVvAg/relationships/links" } } } } }, "included": [ { "type": "versions", "id": "urn:adsk.wipprod:fs.file:vf.BlMbtGTyQ2eMLvZ0xxxxxx?version=1", "attributes": { "name": "lego-racecar.3dm", "displayName": "lego-racecar.3dm", "createTime": "2016-11-02T18:49:51+00:00", "createUserId": "TNSUM9VPxxxxxx", "lastModifiedTime": "2016-11-02T18:49:51+00:00", "lastModifiedUserId": "TNSUM9VPxxxxxx", "versionNumber": 1, "mimeType": "application/vnd.autodesk.rhinoceros", "storageSize": 107743049, "fileType": "3dm", "extension": { "type": "versions:autodesk.core:File", "version": "1.0", "schema": { "href": "https://developer.api.autodesk.com/schema/v1/versions/versions%3Aautodesk.core%3AFile-1.0" }, "data": {} } }, "links": { "self": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/versions/urn%3Aadsk.wipprod%3Afs.file%3Avf.BlMbtGTyQ2eMLvZ05rVvAg%3Fversion%3D1" } }, "relationships": { "item": { "data": { "type": "items", "id": "urn:adsk.wipprod:dm.lineage:BlMbtGTyQ2eMLvZ0xxxxxxx" }, "links": { "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/versions/urn%3Aadsk.wipprod%3Afs.file%3Avf.BlMbtGTyQ2eMLvZ05rVvAg%3Fversion%3D1/item" } } }, "links": { "links": { "self": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/versions/urn%3Aadsk.wipprod%3Afs.file%3Avf.BlMbtGTyQ2eMLvZ05rVvAg%3Fversion%3D1/relationships/links" } } }, "refs": { "links": { "self": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/versions/urn%3Aadsk.wipprod%3Afs.file%3Avf.BlMbtGTyQ2eMLvZ05rVvAg%3Fversion%3D1/relationships/refs" }, "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/versions/urn%3Aadsk.wipprod%3Afs.file%3Avf.BlMbtGTyQ2eMLvZ05rVvAg%3Fversion%3D1/refs" } } }, "downloadFormats": { "links": { "related": { "href": "https://developer.api.autodesk.com/data/v1/projects/a.cGVyc29uYWw6dxxxxxxxxxxxxxxxxxQwNjEwMTQ3NDA1MQ/versions/urn%3Aadsk.wipprod%3Afs.file%3Avf.BlMbtGTyQ2eMLvZ05rVvAg%3Fversion%3D1/downloadFormats" } } }, "derivatives": { "data": { "type": "derivatives", "id": "dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLkJsTWJ0R1R5UTJlTUx2WjA1clZ2QWc_dmVxxxxxxxxx" }, "meta": { "link": { "href": "https://developer.api.autodesk.com/modelderivative/v2/designdata/dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLkJsTWJ0R1R5UTJlTUx2WjA1clZ2QWc_dmVxxxxxxxx/manifest" } } }, "thumbnails": { "data": { "type": "thumbnails", "id": "dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLkJsTWJ0R1R5UTJlTUx2WjA1clZ2QWc_dmVxxxxxxxxx" }, "meta": { "link": { "href": "https://developer.api.autodesk.com/modelderivative/v2/designdata/dXJuOmFkc2sud2lwcHJvZDpmcy5maWxlOnZmLkJsTWJ0R1R5UTJlTUx2WjA1clZ2QWc_dmVyc2lxxxxx/thumbnail" } } }, "storage": { "data": { "type": "objects", "id": "urn:adsk.objects:os.object:wip.dm.prod/6d106e5b-2ac6-41d5-aa1c-b086943xxxxx.3dm" }, "meta": { "link": { "href": "https://developer.api.autodesk.com/oss/v2/buckets/wip.dm.prod/objects/6d106e5b-2ac6-41d5-aa1c-b086943xxxxx.3dm" } } } } } ] }

信息量可能有点大,但这是我遵循的流程,之前也曾使用过。您可以像我之前说的那样尝试,让 Postman 执行一组 REST 调用,然后再构建您的 ajax 调用。