代码之家  ›  专栏  ›  技术社区  ›  Liora Haydont

如何使用openproject api获取附件数据

  •  1
  • Liora Haydont  · 技术社区  · 7 年前

    我目前正试图在我的网站上使用openproject api v3来显示关于openproject工作包的数据(我在symfony中使用php)。

    我对获取一般数据(主题、描述、优先级等)没有问题,但我不确定如何向用户显示附件。

    我试着用 "/api/v3/work_packages/".$id."/attachments" ,但返回的json中没有图像:

    "_type":"Collection",
    "total":1,
    "count":1,
    "_embedded":{
        "elements":[{
            "_type":"Attachment",
            "id":1888,"fileName":"128-128-logo.png",
            "fileSize":9583,
            "description":{"format":"plain","raw":null,"html":""},
            "contentType":"image/png",
            "digest":{"algorithm":"md5","hash":"/*-hash-here-*/"},
            "createdAt":"2018-07-09T16:49:26Z",
            "_links":{
                 "self":{"href":"/api/v3/attachments/1888","title":"128-128-logo.png"},
                 "author":{"href":"/api/v3/users/7","title":"User Name"},
                 "container":{"href":"/api/v3/work_packages/1697","title":"Subject -\u003E Test Query OpenProject"},
                 "downloadLocation":{"href":"/attachments/1888/128-128-logo.png"},
                 "delete":{"href":"/api/v3/attachments/1888","method":"delete"}
            }
        }]
    },
    "_links":{
        "self":{"href":"/api/v3/work_packages/1697/attachments"}
    }
    

    我也试过直接链接到附件,但是

    "_type":"Attachment",
    "id":1888,
    "fileName":"128-128-logo.png",
    "fileSize":9583,
    "description":{"format":"plain","raw":null,"html":""},
    "contentType":"image/png",
    "digest":{"algorithm":"md5","hash":"/*-hash-here-*/"},
    "createdAt":"2018-07-09T16:49:26Z",
    "_links":{
        "self":{"href":"/api/v3/attachments/1888","title":"128-128-logo.png"},
        "author":{"href":"/api/v3/users/7","title":"User Name"},
        "container":{"href":"/api/v3/work_packages/1697","title":"Subject -\u003E Test Query OpenProject"},
        "downloadLocation":{"href":"/attachments/1888/128-128-logo.png"},
        "delete":{"href":"/api/v3/attachments/1888","method":"delete"}
    }
    

    下载位置不是api url,如果我试图使用img标记访问它,则返回406-未经授权( <img src="https://XXXXXX.openproject.com/attachments/1888/128-128-logo.png"> )

    所以我想我的问题是: 如何向用户显示附件,即使他们在我的openproject上没有帐户?

    1 回复  |  直到 7 年前
        1
  •  1
  •   ulferts    7 年前

    要在网站上显示附件,您必须:

    • 授予对openproject实例的匿名访问权。
    • 在公共项目中具有附件,这意味着它必须附加到工作包、wiki页面…公共项目的

    授予对openproject实例的匿名访问权

    取消选中“系统设置”(“管理”->“系统设置”->“身份验证”(选项卡))中的“需要身份验证”,然后保存。

    enter image description here

    公开项目

    选中“项目设置”中的“公用”(导航到项目后->“项目设置”->“信息”(选项卡))并保存。

    enter image description here

    您的网站只能访问附加到该现已公开项目的资源的附件。

    后果

    每个人都可以访问您的openproject实例,至少是公共项目的一部分。

    备选方案1

    如果是一个私人网站,并且该网站的所有访问者也将在openproject实例上拥有一个帐户,并且如果得到保证,他们在访问该网站时将始终登录到openproject,他们的浏览器将在获取图像时自动将会话cookie发送到openproject实例,这将导致他们被授权获取附件。

    备选方案2

    一旦op 8.0发布,api客户端将能够下载附件。然后,带有api密钥的脚本可以下载感兴趣的附件并将其存储在您的网站可访问的位置。网站访问者将直接从网站获得附件。