title: Django and Twitter request_token
body: <p>I am stuck with twitter signing in. By now I realized, that it is a several step thing (rather stupid in comparison with OAuth2):</p>
  
  <ol>
  <li><p>Make a POST request to /request_token endpoint in order to get the initial token</p></li>
  <li><p>Redirect user to /authenticate with token param from (1) in order to get another token</p></li>
  <li><p>Make another POST request to /access-token with token from (2) and finally get the desired token.</p></li>
  </ol>
  
  <p>Right now I am a bit stuck with (1): twitter API always returns ERROR 215, Bad Authentication Data.</p>
  
  <p>The code is like this:</p>
  
  <pre><code>    key = b"my_key&amp;"
      raw_init = "POST" + "&amp;" + quote("https://api.twitter.com/1.1/oauth/request_token", safe='')
      time_param = str(calendar.timegm(time.gmtime()))
      raw_params = quote('oauth_callback', safe='') + "=" + quote('http://example.com/twitter-auth/', safe='')
      raw_params += "&amp;" + quote('oauth_consumer_key', safe='') + "=" + quote('consumer_key', safe='')
      raw_params += "&amp;" + quote('oauth_nonce', safe='') + "=" + quote('aAbBcDadadwrwwrwrwr', safe='')
      raw_params += "&amp;" + quote('oauth_signature_method', safe='') + "=" + quote('HMAC-SHA1', safe='')
      raw_params += "&amp;" + quote('oauth_timestamp', safe='') + "=" + quote(time_param, safe='')
      raw_params += "&amp;" + quote('oauth_version', safe='') + "=" + quote('1.0', safe='')
      #
      raw_params = quote(raw_params, safe='')
      raw_final = bytes(raw_init + "&amp;" + raw_params, encoding='utf-8')
      hashed = hmac.new(key, raw_final, sha1)
  
      request.raw_final = hashed
  
      request.auth_header = base64.b64encode(hashed.digest()).decode()
  </code></pre>
  
  <p>The code above make the signature for the Authorization header.</p>
  
  <p>The POST request:</p>
  
  <pre><code>    url = 'https://api.twitter.com/1.1/oauth/request_token'
      data = {}
      oauth_sign_method = "HMAC-SHA1"
  
      headers = {'Authorization': 'OAuth oauth_nonce="aAbBcDadadwrwwrwrwr"' +
                                  'oauth_callback="' + quote('http://example.com/twitter-auth/', safe='') + '",' +
                                  'oauth_signature_method="HMAC-SHA1",' +
                                  'oauth_timestamp="' + time_param + '",'+
                                  'oauth_consumer_key="consumer_key",'+
                                  'oauth_signature="' + base64.b64encode(hashed.digest()).decode() + '",'+
                                  'oauth_version="1.0"'}
  
      r = requests.post(url, data=json.dumps(data), headers=headers)
  
      answer = json.loads(r.text)
  
      request.twitter_answer = answer
  </code></pre>
  
  <p>And the answer is always with error 215 - Bad Authentication Data. Any idea why?</p>