# Buggy code
def authenticate(self, c_key, c_secret):
    # Get the link for the OAuth page.
    auth_client = Client(USER_AGENT, c_key, c_secret)
    _, _, url = auth_client.get_authorize_url()
    beets.ui.print_("To authenticate with Discogs, visit:")
    beets.ui.print_(url)

    # Ask for the code and validate it.
    code = beets.ui.input_("Enter the code:")
    try:
        token, secret = auth_client.get_access_token(code)
    except DiscogsAPIError:
        raise beets.ui.UserError("Discogs authorization failed")
    except CONNECTION_ERRORS as e:
        self._log.debug("connection error: {0}", e)
        raise beets.ui.UserError("communication with Discogs failed")

    # Save the token for later use.
    self._log.debug("Discogs token {0}, secret {1}", token, secret)
    with open(self._tokenfile(), "w") as f:
        json.dump({"token": token, "secret": secret}, f)

    return token, secret


# Traceback (discogs_client.exceptions.HTTPError)
Successfully installed beets discogs-client pyacoustid enum34 mutagen munkres unidecode musicbrainzngs pyyaml requests six oauthlib audioread
Cleaning up...
(env)jwyant@bigbox ~/code/beets $ ./env/bin/beet import ~/.music-incoming/flac
Traceback (most recent call last):
File "./env/bin/beet", line 9, in <module>
load_entry_point('beets==1.3.11', 'console_scripts', 'beet')()
File "/home/jwyant/code/beets/env/lib/python2.7/site-packages/beets/ui/__init__.py", line 1081, in main
_raw_main(args)
File "/home/jwyant/code/beets/env/lib/python2.7/site-packages/beets/ui/__init__.py", line 1071, in _raw_main
subcommand.func(lib, suboptions, subargs)
File "/home/jwyant/code/beets/env/lib/python2.7/site-packages/beets/ui/commands.py", line 892, in import_func
import_files(lib, paths, query)
File "/home/jwyant/code/beets/env/lib/python2.7/site-packages/beets/ui/commands.py", line 869, in import_files
session.run()
File "/home/jwyant/code/beets/env/lib/python2.7/site-packages/beets/importer.py", line 313, in run
plugins.send('import_begin', session=self)
File "/home/jwyant/code/beets/env/lib/python2.7/site-packages/beets/plugins.py", line 455, in send
result = handler(**arguments)
File "/home/jwyant/code/beets/env/lib/python2.7/site-packages/beets/plugins.py", line 123, in wrapper
return func(*args, **kwargs)
File "/home/jwyant/code/beets/env/lib/python2.7/site-packages/beetsplug/discogs.py", line 76, in setup
token, secret = self.authenticate(c_key, c_secret)
File "/home/jwyant/code/beets/env/lib/python2.7/site-packages/beetsplug/discogs.py", line 98, in authenticate
_, _, url = auth_client.get_authorize_url()
File "/home/jwyant/code/beets/env/lib/python2.7/site-packages/discogs_client/client.py", line 61, in get_authorize_url
raise HTTPError('Invalid response from request token URL.', status_code)
discogs_client.exceptions.HTTPError: 401: Invalid response from request token URL.

# Fixed code
def authenticate(self, c_key, c_secret):
    # Get the link for the OAuth page.
    auth_client = Client(USER_AGENT, c_key, c_secret)
    try:
        _, _, url = auth_client.get_authorize_url()
    except CONNECTION_ERRORS as e:
        self._log.debug("connection error: {0}", e)
        raise beets.ui.UserError("communication with Discogs failed")

    beets.ui.print_("To authenticate with Discogs, visit:")
    beets.ui.print_(url)

    # Ask for the code and validate it.
    code = beets.ui.input_("Enter the code:")
    try:
        token, secret = auth_client.get_access_token(code)
    except DiscogsAPIError:
        raise beets.ui.UserError("Discogs authorization failed")
    except CONNECTION_ERRORS as e:
        self._log.debug("connection error: {0}", e)
        raise beets.ui.UserError("Discogs token request failed")

    # Save the token for later use.
    self._log.debug("Discogs token {0}, secret {1}", token, secret)
    with open(self._tokenfile(), "w") as f:
        json.dump({"token": token, "secret": secret}, f)

    return token, secret