py-trello: 403 Error

Hello, I’m getting a 403 error but the site seems to be up and all services are functional on Trello. Not sure if this is a Cloudflare server-side issue with Trello or an API issue.

Recreate the issue with this:

from trello import TrelloClient TrelloClient(TRELLO_API_KEY).list_boards()


Error:
trello.exceptions.ResourceUnavailable: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
</HEAD><BODY>
<H1>403 ERROR</H1>
<H2>The request could not be satisfied.</H2>
<HR noshade size="1px">
Bad request.
We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
<BR clear="all">
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.
<BR clear="all">
<HR noshade size="1px">
<PRE>
Generated by cloudfront (CloudFront)
Request ID: qCRWUowLKZ3ESPwzuDguR8rVTFatc3uu6U1O8jvYVwnZhRVJr-Elpg==
</PRE>
<ADDRESS>
</ADDRESS>
</BODY></HTML> at https://api.trello.com/1/members/me/boards/?filter=all (HTTP status: 403)

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 14
  • Comments: 15 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Problem is located here. https://github.com/sarumont/py-trello/blob/a628d545e936ccdab299cb139f56b8f98836618e/trello/trelloclient.py#L221

When files are not specified in the case for GET calls, it will attempt to convert an empty dictionary to JSON string. This will be sent as “{}” in the body of our GET request.

Currently, I am monkey patching with the following to get by.

import trello


def patched_fetch_json(self,
                       uri_path,
                       http_method='GET',
                       headers=None,
                       query_params=None,
                       post_args=None,
                       files=None):
    """ Fetch some JSON from Trello """

    # explicit values here to avoid mutable default values
    if headers is None:
        headers = {}
    if query_params is None:
        query_params = {}
    if post_args is None:
        post_args = {}

    # if files specified, we don't want any data
    data = None
    if files is None and post_args != {}:
        data = json.dumps(post_args)

    # set content type and accept headers to handle JSON
    if http_method in ("POST", "PUT", "DELETE") and not files:
        headers['Content-Type'] = 'application/json; charset=utf-8'

    headers['Accept'] = 'application/json'

    # construct the full URL without query parameters
    if uri_path[0] == '/':
        uri_path = uri_path[1:]
    url = 'https://api.trello.com/1/%s' % uri_path

    if self.oauth is None:
        query_params['key'] = self.api_key
        query_params['token'] = self.api_secret

    # perform the HTTP requests, if possible uses OAuth authentication
    response = self.http_service.request(http_method, url, params=query_params,
                                            headers=headers, data=data,
                                            auth=self.oauth, files=files,
                                            proxies=self.proxies)

    if response.status_code == 401:
        raise trello.Unauthorized("%s at %s" % (response.text, url), response)
    if response.status_code != 200:
        raise trello.ResourceUnavailable("%s at %s" % (response.text, url), response)

    return response.json()


trello.TrelloClient.fetch_json = patched_fetch_json

client = trello.TrelloClient(
        api_key="ABC123",
        token="456DEF")
list_of_boards = client.list_boards()
print(list_of_boards)

Problem is located here.

https://github.com/sarumont/py-trello/blob/a628d545e936ccdab299cb139f56b8f98836618e/trello/trelloclient.py#L221

When files are not specified in the case for GET calls, it will attempt to convert an empty dictionary to JSON string. This will be sent as “{}” in the body of our GET request.

Currently, I am monkey patching with the following to get by.

import trello


def patched_fetch_json(self,
                       uri_path,
                       http_method='GET',
                       headers=None,
                       query_params=None,
                       post_args=None,
                       files=None):
    """ Fetch some JSON from Trello """

    # explicit values here to avoid mutable default values
    if headers is None:
        headers = {}
    if query_params is None:
        query_params = {}
    if post_args is None:
        post_args = {}

    # if files specified, we don't want any data
    data = None
    if files is None and post_args != {}:
        data = json.dumps(post_args)

    # set content type and accept headers to handle JSON
    if http_method in ("POST", "PUT", "DELETE") and not files:
        headers['Content-Type'] = 'application/json; charset=utf-8'

    headers['Accept'] = 'application/json'

    # construct the full URL without query parameters
    if uri_path[0] == '/':
        uri_path = uri_path[1:]
    url = 'https://api.trello.com/1/%s' % uri_path

    if self.oauth is None:
        query_params['key'] = self.api_key
        query_params['token'] = self.api_secret

    # perform the HTTP requests, if possible uses OAuth authentication
    response = self.http_service.request(http_method, url, params=query_params,
                                            headers=headers, data=data,
                                            auth=self.oauth, files=files,
                                            proxies=self.proxies)

    if response.status_code == 401:
        raise trello.Unauthorized("%s at %s" % (response.text, url), response)
    if response.status_code != 200:
        raise trello.ResourceUnavailable("%s at %s" % (response.text, url), response)

    return response.json()


trello.TrelloClient.fetch_json = patched_fetch_json

client = trello.TrelloClient(
        api_key="ABC123",
        token="456DEF")
list_of_boards = client.list_boards()
print(list_of_boards)

Tested and the code worked. This is a quick fix for those who cannot wait for package update.

For personal needs, I have forked this repository and merged two pending PRs into the new repository. https://github.com/Konano/py-trello-api

As a temporary solution, I have also uploaded the new package to Pypi. You can download the fixed py-trello with this command: pip install py-trello-api==0.20.0

I hope this helps someone in need.

Response from Trello support:

We recently released a change in making GET requests. Trello’s API will no longer permit supplying a body with a GET request and will now be blocked with a 403 response. For more information: https://developer.atlassian.com/changelog/#CHANGE-1459

To resolve these errors, you should remove the request body in your API GET requests.

Hope this helps but let me know if you have any more questions or concerns.

This looks to be a permanent change to the API as per: https://developer.atlassian.com/changelog/#CHANGE-1459

Thanks to @jitvimol for the monkey patch – that did the trick for me!