Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An overview of what I changed:</p> <ol> <li>I swapped out <code>binascii.b2a_base64</code> for <code>base64.standard_b64encode</code></li> <li>I converted the bytes to string using the <code>bytes.decode('ascii')</code> method. str() seemed to be appending b to the string.</li> <li>I fixed the order of the parameters to <code>hmac.new</code> - it's <code>KEY, MESSAGE</code>, not <code>MESSAGE, KEY</code></li> <li>I removed the reference to <code>include_entities</code> in <code>PARAMETER_STRING</code> - if you don't use include_entities in the request (and I believe it doesn't make sense for the token request) it mustn't be included in the PARAMETER_STRING</li> <li>I added <code>oauth_callback=oob</code> to the begin of the <code>PARAMETER_STRING</code> - all oauth parameters except for the oauth_signature must be included in the base string.</li> <li>I changed the section where the request is made to pre-create the Request object, then add the Authorization header - you were sending the Authorization header as the HTTP body. </li> <li>To match this change, I removed the trailing semicolon from <code>HEADER_TITLE</code>.</li> <li>I added the missing semicolon after <code>oauth_callback="oob"</code>.</li> </ol> <p>Enjoy!</p> <p>Here's a fixed version of your code:</p> <pre class="lang-py prettyprint-override"><code>import urllib.parse, urllib.request, json from hashlib import sha1 import hmac import base64 import time import random import sys #Server Links REQUEST_URL = "https://api.twitter.com/oauth/request_token"; ACCESS_URL = "https://api.twitter.com/oauth/access_token"; AUTHORIZE_URL = "https://api.twitter.com/oauth/authorize"; #Consumer keys TOKEN = "Omitted" TOKEN_SECRET = "Omitted" #Access keys ACCESS_TOKEN = "" ACCESS_TOKEN_SECRET = "" TWEET = "" count = 1 while len(sys.argv) &gt; count: TWEET += sys.argv[count] + " " count += 1 TWEET = TWEET[:-1] #Get rid of trailing space print(TWEET + "\n") #Build content header for POST to return request tokens HEADER_TITLE = "Authorization" #Consumer key HEADER = 'OAuth oauth_callback="oob", oauth_consumer_key="' + TOKEN + '", ' #Nonce HEADER += 'oauth_nonce="' NONCE = "" for i in range(32): NONCE += chr(random.randint(97, 122)) HEADER += NONCE HEADER += '", ' #Timestamp TIMESTAMP = str(int(time.time())) #Signature HEADER += 'oauth_signature="' PARAMETER_STRING = "oauth_callback=oob&amp;oauth_consumer_key=" + TOKEN + "&amp;oauth_nonce=" + NONCE + "&amp;oauth_signature_method=HMAC-SHA1&amp;oauth_timestamp=" + TIMESTAMP + "&amp;oauth_version=1.0" BASE_STRING = 'POST&amp;' + urllib.parse.quote(REQUEST_URL, '') + '&amp;' + urllib.parse.quote(PARAMETER_STRING, '') SIGNING_KEY = urllib.parse.quote(TOKEN_SECRET, '') + '&amp;' print("DEBUG : SIGNING KEY " + SIGNING_KEY + " BASE STRING " + BASE_STRING + "\n") HEADER += urllib.parse.quote(base64.standard_b64encode(hmac.new(SIGNING_KEY.encode(), BASE_STRING.encode(), sha1).digest()).decode('ascii')) HEADER += '", ' #Signature Method HEADER += 'oauth_signature_method="HMAC-SHA1", ' #Timestamp HEADER += 'oauth_timestamp="' + TIMESTAMP + '", ' #Version HEADER += 'oauth_version="1.0"' print(HEADER_TITLE + ":\n" + HEADER) HTTP_REQUEST = urllib.request.Request(REQUEST_URL) HTTP_REQUEST.add_header(HEADER_TITLE, HEADER) print(urllib.request.urlopen(HTTP_REQUEST, bytes('', 'ascii')).read()) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload