Help - 401 Authentication failure


#1

So, I am trying to make a terminal app to follow people, in Python, with no external libraries.

I am able to get the access token and it’s secret. - It matches the token/secret my developer page for the app shows.
The hiccup is when I try to follow someone.

Some basic info I found on other threads (so they don’t repeat):

  1. Access level Read, write, and direct messages
  2. For authentication, the parameters are generated in the exact same way as when I’m trying to get the access token from the Pin.
    Only, the “verifier” param is not there, and the token/secret pair is not the one I get from the “…/request_token” url, but the one I get from the “…/access_token” url. i.e. I’ve replaced the token/secret pair with the access token, secret.
  3. I’m a novice programmer, so I thought my coding may be bad, so I tried using tweepy with python-oauth. The tweepy program also gave an authentication error.
  4. I tried following “twitter”, “twitterapi”, and another friends account … and I doubt they’re protected …
  5. After struggling for like a week, I finally decided to try posting a status today ! … so, I used Tweepy … and it FAILED.

… I’m really really confused. Please help me out …

CODE :

  1. Tweepy code - got from http://log.widnyana.web.id/2012/05/simple-python-twitter-client-using-tweepy.txt
    Note : just putting the relevant bits
import os, sys
import tweepy as tw

CK = '' # consumer key code
CS = '' # consumer secret code
AC = '' # don't worry, I did fill these correctly !
AS = ''

def auth_():
  """ Function doc """
  auth = tw.OAuthHandler(CK, CS)
  auth.set_access_token(AC, AS)
  api = tw.API(auth)
  return api

def update(a):
  try:
    api = auth_()
    api.update_status(a)
    print "\033[1mSukses!\033[0;0m"
  except KeyboardInterrupt:
    print "\nAborted"
  except tw.TweepError as error:
    print "Error: %s" %error

def autoFollow ():
  """ Auto Followback Your friend """
  api = auth_()
  print "\t Getting Your Followers"
  pol = api.followers()
  print "\t Getting Your Friends"
  pren = api.friends()
  follow = [x for x in pol if x not in pren]
  print "\t Checking..."
  for u in follow:
    try:
      u.follow()
    except tw.TweepError as err:
      print "Error: " + err + "when following" + u

  print "\t Done~!"
  1. My code
    Note : TOKEN is consumer key … and ACCESS_TOKEN is acces token, and corresponding secrets
url = "https://api.twitter.com/1/friendships/create.json"
# Apply authentication
params = {
	'oauth_consumer_key': TOKEN,
	'oauth_nonce':  ''.join([str(random.randint(0, 9)) for i in range(8)]), 
	'oauth_signature_method': 'HMAC-SHA1',
	'oauth_timestamp': int(time.time()),
	'oauth_token': ACCESS_TOKEN,
	'oauth_version': '1.0',
}
key = '&'.join([urllib.request.quote(TOKEN_SECRET, safe='~'), urllib.request.quote(ACCESS_TOKEN_SECRET, safe='~')])
print("PARAMS : ", params)
raw = '&'.join(["POST", url, urllib.parse.urlencode(params)])
sig = binascii.b2a_base64(hmac.new(key.encode("utf-8"), raw.encode("utf-8"), sha1).digest())[:-1]
params['oauth_signature'] = sig
h = 'OAuth realm="", ' + ', '.join([k + '="' +  urllib.request.quote(str(v), safe='~') + '"' for k, v in params.items()])
print("HEADER : ")
print(h)
       
# Execute request
http_req = urllib.request.Request(url, urllib.parse.urlencode({"screen_name": name}), {"Authorization": h})
t = urllib.request.urlopen(http_req, bytes('', 'ascii'))