Skip to content

Instantly share code, notes, and snippets.

@okoye
Created September 18, 2013 04:54
Show Gist options
  • Save okoye/6604695 to your computer and use it in GitHub Desktop.
Save okoye/6604695 to your computer and use it in GitHub Desktop.
sample jira python oauthentication demostration. showing two ways to access the jira api using python 2.6 (dance0) and python 2.7 (dance1)
'''
sample code to demonstrate using JIRA's oauth API
'''
from rauth.session import OAuth1Session
from rauth.oauth import RsaSha1Signature
from Crypto.PublicKey import RSA
from Crypto.Hash import MD5
from Crypto import Random
from jira.client import JIRA
############### helper routines ###############
def fetch_configuration(config_file='configuration.txt'):
#open file, parse config and return key, value pairs found in dict
config = {}
for line in open(config_file):
key, value = line.strip().split('=')
config[key] = value
return config
def generate_keys():
rng = Random.new().read
rsa_key = RSA.generate(2048, rng)
assert(rsa_key.has_private())
return rsa_key
############# main oauth method ##############
def dance0():
#first, compute RsaSha1Signature
#WARNING: PyCrypto must already be installed or this will __fail__
config = fetch_configuration()
crypt = RsaSha1Signature()
key = generate_keys()
session = OAuth1Session(config.get('consumer_key'),
key.exportKey(),
access_token=config.get('access_token'),
access_token_secret=config.get('access_token_secret'),
signature=RsaSha1Signature)
print session.get(config.get('url'), verify=False)
def dance1():
keys = generate_keys()
config = fetch_configuration()
oauth_parameters = {
'access_token': config.get('access_token'),
'access_token_secret': config.get('access_token_secret'),
'consumer_key': config.get('consumer_key'),
'key_cert': keys.exportKey()
}
options = {
'server': 'https://ecomjira.wsgc.com',
'verify': False
}
authenticated_jira = JIRA(oauth=oauth_parameters, options=options)
#print authenticated_jira.issue('PNP-8609')
print authenticated_jira.projects()
if __name__ == '__main__':
dance0()
dance1()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment