SV Madhava Reddy
SV Madhava Reddy

Reputation: 2018

jira-ruby plugin : Pass API Token instead of username and password

I am using jira-ruby Gem.

require 'jira-ruby'

options = {
:username     => 'xxxxxxxx',
:password     => '********',
:site         => 'https://xxx.yyyy.com',
:context_path => '',
:auth_type    => :basic,
:use_ssl => true
}

client = JIRA::Client.new(options)
project = client.Project.find('P-NAME')

project.issues.each do |issue|
  puts "#{issue.id} - #{issue.summary}"
end

Here instead of passing the username and password, i want to pass API Token. How can i do that?????

Normal curl command which is working fine is :

curl -X GET -H "Authorization: Basic <TOKEN>" "https://<URL HERE>/rest/api/2/issue/<ID>"

Upvotes: 4

Views: 1555

Answers (3)

Scott Davies
Scott Davies

Reputation: 117

So the GEM constructs the basic auth headers. All you need to do is use your Jira email as :username and then the API token as :password. Then it will authenticate.

The Oauth way doesn't work with Jira Cloud REST API. Only Jira Server is compatible.

Upvotes: 1

SV Madhava Reddy
SV Madhava Reddy

Reputation: 2018

There is no direct solution I found for this one. Instead I've followed REST approach. It will be simple. Atlasssian has good api documentation.

request_payload = {
  body: body, # Body as JSON
  query: params, # URL Parameters
  headers: {
    'Authorization' => "Basic #{auth_token}",
    'Content-Type' => 'application/json'
  }
}
response = HTTParty.send(:post, url, request_payload)
puts response

Please replace #{auth_token} with your API key. Hope this will help somebody.

Upvotes: 0

kali
kali

Reputation: 101

I found the documentation really unclear in this aspect. Here is what worked for me so far:

options = {
  :site             => 'https://my.jira.site',
  :context_path     => '/my_jira_path',
  :auth_type        => :oauth,
  :consumer_key     => 'jira_consumer_key_name',
  :consumer_secret  => 'jira_consumer_key_secret',
  :access_token     => 'jira_oauth_access_token',
  :access_secret    => 'jira_oauth_access_secret',
  :private_key_file => 'path/to/private_key_file',
}

In my case I had manually pre-authorized my app with Jira via oauth authentication, because this is a script that doesn't enable for a callback to obtain the token auth back, therefore I used the access_token and access_secret obtained this way.

After creating the client with JIRA::Client.new(options) I had to also set the token manually (this might not be necessary if you are able to get the callback, but I haven't explored that way):

client.set_access_token(options[:access_token],options[:access_secret])

Hope this works for you too.

Upvotes: 0

Related Questions