Last active
April 27, 2024 14:44
-
-
Save attrib/ea446d5cd25617cf14a2 to your computer and use it in GitHub Desktop.
Get access_token with oauth for jira cloud (OAuth with RSA-SHA1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var | |
fs = require('fs'), | |
// Generate key using for example: | |
// openssl genrsa -out ~/.ssh/jira_rsa 2048 | |
// openssl rsa -in ~/.ssh/jira_rsa -pubout > ~/.ssh/jira_rsa.pub | |
privateKeyData = fs.readFileSync(process.env['HOME'] + '/.ssh/jira_rsa', "utf8"), | |
consumerKey = 'XXXXXXXXXXXX', // just create any string | |
OAuth = require('oauth').OAuth, | |
readline = require('readline'), | |
host = "https://xxxx.atlassian.net"; | |
var consumer = | |
new OAuth( | |
host + "/plugins/servlet/oauth/request-token", | |
host + "/plugins/servlet/oauth/access-token", | |
consumerKey, | |
privateKeyData, | |
"1.0", | |
"https://dummy.com/", // Should be a either a non existing url or a you domain, you will be redirect here with the oauth_verifier | |
"RSA-SHA1", | |
null, | |
{ | |
"Accept" : "application/json", | |
"Connection" : "close", | |
"User-Agent" : "Node authentication", | |
"Content-Type" : "application/json" // This part doesn't worked for me, when doing a request for resource, but for the auth process it doesn't matter | |
// if you want to use consumter.get with an atlassian resource only solution I found till now is to comment following line in oauth/lib/oauth.js (arond 363) | |
// headers["Content-Type"]= post_content_type; | |
} | |
); | |
consumer.getOAuthRequestToken( | |
function (error, oauthToken, oauthTokenSecret, results) { | |
if (error) { | |
console.log('Error: ', error); | |
} | |
else { | |
console.log(results); | |
console.log('oauthRequestToken: ' + oauthToken); | |
console.log('oauthRequestTokenSecret: ' + oauthTokenSecret); | |
console.log('Open url: ' + host + '/plugins/servlet/oauth/authorize?oauth_token=' + oauthToken); | |
rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}), | |
rl.question('Type in oauth_verifier parameter from the redirected url: ', (oauth_verifier) => { | |
oauth_verifier = oauth_verifier.trim(); | |
consumer.getOAuthAccessToken ( | |
oauthToken, | |
oauthTokenSecret, | |
oauth_verifier, | |
function(error, oauthAccessToken, oauthAccessTokenSecret, results){ | |
if (error) { | |
console.log('Error: ', error); | |
rl.close(); | |
} | |
else { | |
console.log('Result: ', results); | |
console.log('oauthAccessToken: ' + oauthAccessToken); | |
console.log('oauthAccessTokenSecret: ' + oauthAccessTokenSecret); | |
rl.close(); | |
} | |
} | |
); | |
}); | |
} | |
} | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Retrieving the access token is the "hard" part if you application has no web interface. My description assumes this case and a "hack" to get the access token either way.
Generate a key
openssl genrsa -out ~/.ssh/jira_rsa 2048
openssl rsa -in ~/.ssh/jira_rsa -pubout > ~/.ssh/jira_rsa.pub
Setup Application in Jira (Preferences > Applications > Select Application Links /plugins/servlet/applinks/listApplicationLinks)
Application Name:
Application Type: General Application
Enable input link
Click continue
Consumer Key:
Consumer Name:
Public Key: <content from ~/.ssh/jira_rsa.pub>
Get access token:
Create temp folder with oauth.js (https://gist.github.com/attrib/ea446d5cd25617cf14a2)
npm install oauth
Replace XXXXXX with consumer key
node oauth.js
Follow instructions and at the end iy will output access_token and access_token_secret
Now you have everything you need to make oauth calls, for example when using https://github.com/jira-node/node-jira-client