def _request(self, method, url, data=None): if self.verbose: print(' '.join((method, url))) self._check_user_agent() headers = { 'Accept-Encoding': 'gzip', 'User-Agent': self.user_agent, } if data: headers['Content-Type'] = 'application/json' content, status_code = self._fetcher.fetch(self, method, url, data=data, headers=headers) if status_code == 204: return None body = json.loads(content) if 200 <= status_code < 300: return body else: raise HTTPError(body['message'], status_code)
def get_authorize_url(self, callback_url=None): """ Returns a tuple of (<access_token>, <access_secret>, <authorize_url>). Send a Discogs user to the authorize URL to get the verifier for the access token. """ # Forget existing tokens self._fetcher.forget_token() params = {} params['User-Agent'] = self.user_agent if callback_url: params['oauth_callback'] = callback_url postdata = urllib.urlencode(params) content, status_code = self._fetcher.fetch(self, 'POST', self._request_token_url, data=postdata, headers=params) if status_code != 200: raise HTTPError('Invalid response from request token URL.', status_code) token, secret = self._fetcher.store_token_from_qs(content) params = {'oauth_token': token} query_string = urllib.urlencode(params) return (token, secret, '?'.join((self._authorize_url, query_string)))
def get_access_token(self, verifier): """ Uses the verifier to exchange a request token for an access token. """ if isinstance(verifier, bytes): verifier = verifier.decode('utf8') self._fetcher.set_verifier(verifier) params = {} params['User-Agent'] = self.user_agent content, status_code = self._fetcher.fetch(self, 'POST', self._access_token_url, headers=params) if status_code != 200: raise HTTPError('Invalid response from access token URL.', status_code) token, secret = self._fetcher.store_token_from_qs(content) return token, secret