Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

issues with the creation of an OAuth client to use it with Jira Server.

Abhishek Barnwal September 10, 2020

We followed https://developer.atlassian.com/server/jira/platform/oauth/ documentation and completed step-1. Howeverwe are unable to proceed with step-2 as mvn clean compile assembly:single is throwing the error.

1 answer

0 votes
Shivaprasad Hattaraki
Contributor
September 10, 2020

Hello,

Use python script to generate oauth token

import requests
from oauthlib.oauth1 import SIGNATURE_RSA
from requests_oauthlib import OAuth1Session
from jira.client import JIRA

def read(file_path):
""" Read a file and return it's contents. """
with open(file_path) as f:
return f.read()

# The Consumer Key created while setting up the "Incoming Authentication" in
# JIRA for the Application Link.
CONSUMER_KEY = 'JIRA'
VERIFIER = 'jira_verifier'

# The contents of the rsa.pem file generated (the private RSA key)
RSA_KEY = read('path\\to\\rsa.pem')

# The URLs for the JIRA instance
JIRA_SERVER = 'http://jira.com'
REQUEST_TOKEN_URL = JIRA_SERVER + '/plugins/servlet/oauth/request-token'
AUTHORIZE_URL = JIRA_SERVER + '/plugins/servlet/oauth/authorize'
ACCESS_TOKEN_URL = JIRA_SERVER + '/plugins/servlet/oauth/access-token'

# Step 1: Get a request token

oauth = OAuth1Session(CONSUMER_KEY, signature_type='auth_header',
signature_method=SIGNATURE_RSA, rsa_key=RSA_KEY, verifier=VERIFIER)
request_token = oauth.fetch_request_token(REQUEST_TOKEN_URL,verify=True)

print("STEP 1: GET REQUEST TOKEN")
print(" oauth_token={}".format(request_token['oauth_token']))
print(" oauth_token_secret={}".format(request_token['oauth_token_secret']))
print("\n")


# Step 2: Get the end-user's authorization

print("STEP2: AUTHORIZATION")
print(" Visit to the following URL to provide authorization:")
print(" {}?oauth_token={}".format(AUTHORIZE_URL, request_token['oauth_token']))
print("\n")


destination= open("linkJira.txt","w+")
destination.write(" oauth_token={}".format(request_token['oauth_token']))
destination.write(" oauth_token_secret={}".format(request_token['oauth_token_secret']))
destination.write(" {}?oauth_token={}".format(AUTHORIZE_URL, request_token['oauth_token']))
destination.close()

while input("Press any key to continue..."):
pass


# Step 3: Get the access token

access_token = oauth.fetch_access_token(ACCESS_TOKEN_URL)

# print("STEP2: GET ACCESS TOKEN")
# print(" oauth_token={}".format(access_token['oauth_token']))
# print(" oauth_token_secret={}".format(access_token['oauth_token_secret']))
# print("\n")

print("STEP2: GET ACCESS TOKEN")
print(" oauth_token={}".format(access_token))
print(" oauth_token_secret={}".format(access_token))
print("\n")


# Now you can use the access tokens with the JIRA client. Hooray!

jira = JIRA(options={'server': JIRA_SERVER,'verify':True}, oauth={
'access_token': access_token['oauth_token'],
'access_token_secret': access_token['oauth_token_secret'],
'consumer_key': CONSUMER_KEY,
'key_cert': RSA_KEY
})

# print all of the project keys just as an exmaple
for project in jira.projects():
print(project.key)

 

 

Steps to generate token:

  1. Download the rsa.pem file
  2. Replace the path with the location of rsa.pem in generate_token.py script
  3. Execute the script
  4. It will print URL in prompt
  5. Enter that URL in browser where you’re already logged into JIRA (padijira)
  6. Click on ALLOW
  7. Go back to script and press ENTER
  8. It will print the oauth_token and oauth_token_secret.
  9. Use the oauth_token and oauth_token_secret to connect to Jira

NOTE: Replace the path (line 17 of the mentioned script) with the location of rsa.pem file in your system

Suggest an answer

Log in or Sign up to answer