Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7811037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:00:52+00:00 2026-06-02T04:00:52+00:00

I am attempting to use Python 3 to interface with the Twitter API to

  • 0

I am attempting to use Python 3 to interface with the Twitter API to return the link for a page that gives me a pin number to use to request access tokens. As detailed here: https://dev.twitter.com/docs/auth/pin-based-authorization

Twitter’s API responds to me telling me that I have not properly authorized my POST request by returning a 401. My best guess as to why is that I’m not encoding the HMAC signature in base64 correctly. Everything other part of the POST request I generate appears correct based on samples of correct POST requests I have looked at.

I have spent several days working on this and I am hoping someone can help nudge me past the final part.

Here are the most relevant parts of the Twitter API docs: https://dev.twitter.com/docs/api/1/post/oauth/request_token

https://dev.twitter.com/docs/auth/authorizing-request

This is the code I am using:

import urllib.parse, urllib.request, json
from hashlib import sha1
import hmac
import binascii
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) > 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 = "include_entities=true&oauth_consumer_key=" + TOKEN + "&oauth_nonce=" + NONCE + "&oauth_signature_method=HMAC-SHA1&oauth_timestamp=" + TIMESTAMP + "&oauth_version=1.0"
BASE_STRING = 'POST&' + urllib.parse.quote(REQUEST_URL, '') + '&' + urllib.parse.quote(PARAMETER_STRING, '')
SIGNING_KEY = urllib.parse.quote(TOKEN_SECRET, '') + '&'
print("DEBUG : SIGNING KEY " + SIGNING_KEY + " BASE STRING " + BASE_STRING + "\n")
HEADER += str(binascii.b2a_base64(hmac.new(BASE_STRING.encode(), SIGNING_KEY.encode(), sha1).digest()[:-1]))#Note to self, we may not want to remove the last character...
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)

print(urllib.request.urlopen(urllib.request.Request(REQUEST_URL, bytes(HEADER_TITLE+HEADER, 'utf-8'))).read())

Finally, I would like to note that I am aware of the existence of Python OAuth and Twitter modules which aid in development for this. However, as a learning experience, I am choosing not to use them.

Thank you very much in advance for your time and assistance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:00:53+00:00Added an answer on June 2, 2026 at 4:00 am

    An overview of what I changed:

    1. I swapped out binascii.b2a_base64 for base64.standard_b64encode
    2. I converted the bytes to string using the bytes.decode('ascii') method. str() seemed to be appending b to the string.
    3. I fixed the order of the parameters to hmac.new – it’s KEY, MESSAGE, not MESSAGE, KEY
    4. I removed the reference to include_entities in PARAMETER_STRING – 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
    5. I added oauth_callback=oob to the begin of the PARAMETER_STRING – all oauth parameters except for the oauth_signature must be included in the base string.
    6. 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.
    7. To match this change, I removed the trailing semicolon from HEADER_TITLE.
    8. I added the missing semicolon after oauth_callback="oob".

    Enjoy!

    Here’s a fixed version of your 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) > 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&oauth_consumer_key=" + TOKEN + "&oauth_nonce=" + NONCE + "&oauth_signature_method=HMAC-SHA1&oauth_timestamp=" + TIMESTAMP + "&oauth_version=1.0"
    BASE_STRING = 'POST&' + urllib.parse.quote(REQUEST_URL, '') + '&' + urllib.parse.quote(PARAMETER_STRING, '')
    SIGNING_KEY = urllib.parse.quote(TOKEN_SECRET, '') + '&'
    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())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to use Python to convert a multi-page PDF into a series of
I am attempting to use the tweepy api to make a twitter function and
I'm attempting to use the python subprocess module to log in to a secure
Im attempting to use a C++ extension for Python called PySndObj. and getting an
In Python 3.0.1, I am attempting to use the Counter part of the collections
When attempting to use HttpWebRequest to retrieve a page from my dev server, I
In attempting to use std::select1st from <functional> in a VS2008 project I found that
Suppose I need to create my own small DSL that would use Python to
I am attempting to use a parametrized LIKE query with Python's Sqlite library as
I'm attempting to use Paramiko (Python SSH library) to read a remote file, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.