discogs / discogs_client

DEPRECATED - Official Python Client for the Discogs API
http://www.discogs.com/developers
Other
479 stars 132 forks source link

get_authorize_url throws discogs_client.exceptions.AuthorizationError: 401: Could not get request token. Response: 'Parameter not found: oauth_consumer_key' #78

Closed robbat2 closed 4 years ago

robbat2 commented 7 years ago

The oauth consumer key is not being placed into the POST data.

oauthlib-1.1.2 requests-2.12.5

Testcase from the quickstart documentation:

import discogs_client as dc
consumer_key = '...'
consumer_secret = '...'
ds = dc.Client('my_user_agent/1.0',
        consumer_key=consumer_key,
        consumer_secret=consumer_secret,
        )
auth_url = ds.get_authorize_url()

This results in:

Traceback (most recent call last):
  File "discogs-test.py", line 12, in <module>
    auth_url = ds.get_authorize_url()
  File "/usr/lib64/python2.7/site-packages/discogs_client/client.py", line 63, in get_authorize_url
    raise AuthorizationError('Could not get request token.', status_code, content)
discogs_client.exceptions.AuthorizationError: 401: Could not get request token. Response: 'Parameter not found: oauth_consumer_key'

Enabling httplib & requests debug, the following can be seen:

DEBUG:oauthlib.oauth1.rfc5849:Collected params: [(u'oauth_version', u'1.0'), (u'oauth_consumer_key', xxxxx'), (u'oauth_signature_method', u'HMAC-SHA1'), (u'oauth_nonce', u'42713897882581496191487477323'), (u'oauth_timestamp', u'1487477323'), (u'Content-Type', u'application/x-www-form-urlencoded'), (u'User-Agent', u'beets/1.4.3 +http://beets.io/')]
DEBUG:oauthlib.oauth1.rfc5849:Normalized params: Content-Type=application%2Fx-www-form-urlencoded&User-Agent=beets%2F1.4.3%20%2Bhttp%3A%2F%2Fbeets.io%2F&oauth_consumer_key=xxxx&oauth_nonce=42713897882581496191487477323&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1487477323&oauth_version=1.0
DEBUG:oauthlib.oauth1.rfc5849:Normalized URI: https://api.discogs.com/oauth/request_token
DEBUG:oauthlib.oauth1.rfc5849:Base signing string: POST&https%3A%2F%2Fapi.discogs.com%2Foauth%2Frequest_token&Content-Type%3Dapplication%252Fx-www-form-urlencoded%26User-Agent%3Dbeets%252F1.4.3%2520%252Bhttp%253A%252F%252Fbeets.io%252F%26oauth_consumer_key%xxxx%26oauth_nonce%3D42713897882581496191487477323%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1487477323%26oauth_version%3D1.0
DEBUG:oauthlib.oauth1.rfc5849:Signature: 4+mN1iYQqtw+//7vjlkqStd1wbQ=
DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTPS connection (1): api.discogs.com
send: 'POST /oauth/request_token HTTP/1.1\r\nHost: api.discogs.com\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: beets/1.4.3 +http://beets.io/\r\nContent-Type: application/x-www-form-urlencoded\r\nAuthorization: Basic YW5vbnltb3VzOmNtZGZ0cEBleGFtcGxlLmNvbQ==\r\nContent-Length: 101\r\n\r\n'
send: u'Content-Type=application%2Fx-www-form-urlencoded&User-Agent=beets%2F1.4.3+%2Bhttp%3A%2F%2Fbeets.io%2F'
reply: 'HTTP/1.1 401 UNAUTHORIZED\r\n'
header: Server: nginx
header: Date: Sun, 19 Feb 2017 04:08:43 GMT
header: Content-Type: application/x-www-form-urlencoded
header: Content-Length: 39
header: Access-Control-Allow-Headers: Content-Type, authorization
header: Access-Control-Allow-Methods: GET,PUT,POST,DELETE,PATCH
header: WWW-Authenticate: OAuth realm="http://api.discogs.com"
header: X-Discogs-Media-Type: discogs.v2
header: Access-Control-Allow-Origin: *
header: X-Frame-Options: SAMEORIGIN
header: X-XSS-Protection: 1; mode=block
header: X-Content-Type-Options: nosniff
header: Strict-Transport-Security: max-age=16000000
DEBUG:requests.packages.urllib3.connectionpool:https://api.discogs.com:443 "POST /oauth/request_token HTTP/1.1" 401 39
error: communication with Discogs failed
robbat2 commented 4 years ago

I FINALLY figured out what happened here, because I hit the same cmdftp@example.com string in another project, and the top google hit was my own bug.

$ ls -lad /home/robbat2/.netrc ; cat /home/robbat2/.netrc 
-rw------- 1 robbat2 robbat2 52 Mar  8  2003 /home/robbat2/.netrc
default
login anonymous
password cmdftp@example.com