Search code examples
djangotwitterdjango-allauth

Code: unknown, Error: Invalid response while obtaining request token from "api.twitter.com" - how to obtain and print Twitter error message


I am using Django Allauth with Django 2.0.8 and 3.5

I have followed the instructions for using Twitter to login to a site. However, when I attempt to login to my website, I am getting the following message:

Social Network Login Failure

An error occurred while attempting to login via your social network account.

There is no log output containing more information - which makes debugging this unnecessarily difficult. Based onan answer for this question, I have added information to my tempate which sheds some more light:

Code: {{ auth_error.code }}, Error: {{ auth_error.exception }}

However, the error message is:

Code: unknown, Error: Invalid response while obtaining request token from "api.twitter.com"

Which doesn't help very much. I want to see the actual response returned by the twitter.api, so I can get to the bottom of the problem - however, it is not obvious to me how to output or log the ACTUAL response obtained from the twitter API.

How do I see/capture the Twitter API response when I attempt to login using django-allauth?


Solution

  • Checking the django-allauth code I could find the logic which is giving you the following error. Error Code

            if response.status_code not in [200, 201]:
                raise OAuthError(
                    _('Invalid response while obtaining request token'
                      ' from "%s".') % get_token_prefix(
                          self.request_token_url))
    

    This shows that if the response code is not successful i.e. 200 or 201 then it would raise this exception. So you can see that it is raising an error without doing much of logging the response.

    There might be other ways to log response, but that would need some help from the maintainer of the library or someone who has explored the whole library.

    Now coming to your question, how can you log this response. The simplest way that I see is to edit the local file socialaccount/providers/oauth/client.py in your server and place a logger. The new file socialaccount/providers/oauth/client.py would look something like this:

    import logging
    LOG_FILENAME = "django_allauth_response.log"
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)
    handler = RotatingFileHandler(LOG_FILENAME, maxBytes=20000000, backupCount=2)
    formatter = logging.Formatter('%(levelname)s: %(asctime)s %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    ......... 
    .........
    same code as in allauth/socialaccount/providers/oauth/client.py
    .........
    
    logger.info("Response of authentication:" + str(response)) #add this line
    if response.status_code not in [200, 201]:
        raise OAuthError(
            _('Invalid response while obtaining request token'
              ' from "%s".') % get_token_prefix(
                  self.request_token_url))
    

    After this once you run it in your server then you can open the django_allauth_response.log file and check the response.

    P.S. There might be few minor tweaks needed for setting up the logger and also mostly you have to work little more on the string conversion of the response.