Search code examples
node.jstrello

Trello attachment download returning 401 unauthorized permission requested


I have a simple script that attempts to download attachments on cards that include "template" as a prefix and are an svg.

I generated my token with https://trello.com/1/authorize?expiration=never&scope=read,write&response_type=token&key=MYKEY.

I'm aware of the access to S3 announcement https://community.developer.atlassian.com/t/authenticated-access-to-s3/40647.

trello.get("/1/lists/{idList}/cards", (err, data) => {
  for (const card of data) {
    trello.get("/1/cards/" + card.id + "/attachments", (err, data) => {
      for (const file of data) {
        if (file.mimeType != "image/svg+xml") {
          continue;
        }

        const prefix = file.name.split("-")[0];

        if (prefix === "template") {
          trello.get(
            "/1/cards/" +
              card.id +
              "/attachments/" +
              file.id +
              "/download/" +
              file.name,
            (err, data) => {
              console.log(data);
            }
          );
        }
      }
    });
  }
});

I then get a 401 unauthorized permission requested and I'm unsure why. Is this something to do with my trello subscription (14 day trial) or some setting in trello. What am I missing?

Thanks


Solution

  • Answer to this is don't use node-trello API wrapper. The wrapper passes the auth via the query parameters. This particular download url doesn't support that, shown here https://community.developer.atlassian.com/t/update-authenticated-access-to-s3/43681.

    const response = await axios.get(
      "https://api.trello.com/1/cards/" +
         card.id +
         "/attachments/" +
         file.id +
         "/download/" +
         file.name, // file.url
       {
       headers: {
         Authorization:
           'OAuth oauth_consumer_key="' +
           TRELLO_SECRET_KEY +
           '", oauth_token="' +
           TRELLO_TOKEN +
           '"',
         },
       }
    );