Watson Workspace will no longer be available after February 28, 2019. Learn more.

Developers

How to access an attachment/file posted on a space ?

Comments

17 comments

  • Avatar
    Miguel Estrada

    Hello.  Stay tuned to https://github.com/watsonwork/watsonwork-developer-docs for a preview on documentation on how to do this using `BETA` APIs

  • Avatar
    Steven Vaughan

    Any update on this facility Miguel?

  • Avatar
    Miguel Estrada

    Hello Steven and thanks for the `reminder`.  I just finished a draft copy and will get it reviewed this week and share it, still as an experimental draft at https://github.com/watsonwork/watsonwork-developerdocs  Will comment here again when I have done so.

  • Avatar
    Charissa Gravell

    Hi there -- is this still being worked on?

  • Avatar
    Darren Cacy

    You can access a file today.  To do so, you'll need the fileId, which you can get from the annotation on the post which has the file.  If a message has a file attached, it will have an annotation with a type of "file", and that annotation will contain the fileId.

    I wrote an app to export all the messages and files from a space; what I do is this:

    1. Get a list of messages in a space, including annotations.
    2. Parse the annotations and look for one (or more) with a type of "file"
    3. Get the file as a stream and zip it into an archive along with the other files

    If you're interested, I'll publish this sample code.

  • Avatar
    Vitalijus Rudzinskas (Edited )

    I am getting the first 250 files posted in space, by doing this:

    String body = "query {" +
    " space(id:\""+spaceId+"\"" +")"+
    " {" +
    " conversation " +
    " {" +
    " messages" +
    " (" +
    " first: 250" +
    " annotationType:\"file\"" +
    " )" +
    " {" +
    " items " +
    " {" +
    " content" +
    " id" +
    " created" +
    " }" +
    " }" +
    " }" +
    " }" +
    "} ";

    GraphQLQuery graphqlQuery = new GraphQLQuery();
    graphqlQuery.setQuery(body);

    You can get the graphQL query out of that.

    Here is graphQL Explorer example of executing it:



    If anyone has anything different or better or more efficient, please share.


    Update:
    Actually, I just checked, they improved it, maximum 250 results restriction is removed and the query succeeds without specifying "first:250" parameter, which hopefully means now the query will return ALL the files posted, not just first 250:

    Example:

    I still hate graphQL, I just have to use to be paid and have a job.


  • Avatar
    Darren Cacy

    Great work on just getting the files! I had a need to export everything and didn't even know about the ability to filter on annotationType.

    I'm guessing that there is a built-in limit to the number of results returned, and if you used a cursor you can iterate through to get everything.

  • Avatar
    Vitalijus Rudzinskas (Edited )

    Yeah, at some point, if I ever have time for it, I might create an app that will share 260 files, naming them by the numbers from 1 to 260, just to see if the query on that spaceID will return them all at once, to know for sure or something.

  • Avatar
    Izack Tuaf

    Hi, I am looking for an option to get the file itself after it was uploaded to the space with a GET, there was a mention that an example will be added to the API documentation. Is there any update on that?

  • Avatar
    Vitalijus Rudzinskas (Edited )

    Haven't had to do it myself yet, I was mostly doing sharing the files in spaces and deleting previously shared files.

    What I assume you would have to do is do a query to get the files shared in space and to get the fileId of the file you want to download.

    If you download any file shared in space, as user in web browser, it shows you the URL you got it from:

    https://downloads.workspace.ibm.com/downloads/[ SPACE ID ]/file-[ FILE ID ]/?token=[ TOKEN ]&Expires=1528194653

    So you have spaceID and FileID, you need to find someone who knows how your app should get that token that allows to download the file to finish the URL. I don't know how to get that token - it looks too short to be JWT tokens I am using to authenticate app or work on behalf of user.

    Hopefully someone else can explain how to obtain that piece of puzzle.

    Most likely it will involve TimeStamp, AppSecret and one-way mathematical function to generate token.


    Edit:
    If you have enough time to experiment on it, try getting this token:
    https://developer.watsonwork.ibm.com/docs/api-reference/authenticate-as-an-app and sending that token.

    If that doesn't work, try sending: Basic base64(appId:appSecret).

    One of these might work eventually, although it looks like the token changes every time you download file, so it must be salted with timestamp or something.

  • Avatar
    Darren Cacy

    I wrote a sample app to export messages and files from a space; you can see it in action here: https://exportspace.mybluemix.net.

    Source code is here: https://github.com/dcacy/exportspace

    This is written in node.js, using the unofficial SDK found here: https://github.com/van-ibm/watsonworkspace-bot.

    The usual disclaimers apply. :-)

  • Avatar
    Miguel Estrada

    The following documentation had been created.  This is subject to change and rather not have folks rely on it.  Given the above reverse engineering and the potential support costs associated.... I figured it is best to at least state where things are currently at.  I have prepared a PR in our doc.  It will stay as a doc in github and won't make it to the main Developer Experience, but it gives you more context for above.

    https://github.com/watsonwork/watsonwork-developer-docs/blob/1a4090aa3b4f832dc0e225d7134bc561c95a159f/guides/files_experimental.md

    https://github.com/watsonwork/watsonwork-developer-docs/pull/88/files

  • Avatar
    Vitalijus Rudzinskas

    1. Created new space from my account.
    2. Posted file in space from my account.
    3. Queried the space for fileID.
    4. Made this URL, put it in browser:
    https://api.watsonwork.ibm.com/files/api/v1/files/file/ibm0@default@5b17b431e4b0ea07a1eb7360@file-1a932e9c-58a9-4778-aea9-91a4cce425e4

    5. Got this:

    Is this supposed to work for the app only, not user ?

  • Avatar
    Vitalijus Rudzinskas (Edited )

    @DarrenCacy

    I am tempted to try the link, but since I know all too well what authorizing app to work on behalf of user does, I am TERRIFIED to click it. And I am javascript-challenged as well, so can't understand the code.
    But definately great job, if you get the link and download files.

  • Avatar
    Darren Cacy

    Yup, I understand the hesitation with giving an app authorization to act on your behalf. :-)

    I believe the error you received does indicate that it's the app which can access the URL, not the user. I have another application which shows the content of a space, and in order to render images I have the app retrieve the stream and return it to the browser, rather than have the browser go after the URL directly.  And Yes, that app also asks the user to authorize, and YES it's also written in JavaScript so you may never have the joy of using it. :-)

    In case you are interested, you can see it here: https://workspace-explorer.mybluemix.net

  • Avatar
    Izack Tuaf

    I have also tried what Vitalijus Rudzinskas did by creating the url with the FieldID and got the 401 error.

    Is there a way to access the JSON as a user? or steps to do so as the app?

  • Avatar
    Miguel Estrada

    >4. Made this URL, put it in browser:
    https://api.watsonwork.ibm.com/files/api/v1/files/file/ibm0@default@5b17b431e4b0ea07a1eb7360@file-1a932e9c-58a9-4778-aea9-91a4cce425e4

     

    That won't work.... the browser is not sending a JWT with that request.  Try using postman or paw instead.

Please sign in to leave a comment.