sumoheavy / jira-ruby

A Ruby gem for the JIRA REST API
MIT License
654 stars 412 forks source link

Clarifying question about README on authentication using API token #405

Closed HongQiGong closed 4 months ago

HongQiGong commented 1 year ago

In the README section for "Configuring JIRA to use Personal Access Tokens Auth": https://github.com/sumoheavy/jira-ruby#configuring-jira-to-use-personal-access-tokens-auth

It says that the parameters :username and :password are not needed, and that the :default_headers is needed to contain the api_token. Yet in the code example provided in the section, the :username and :password parameters are still there and there is no :default_headers parameter.

# NOTE: the token should not be encoded
api_token = API_TOKEN_OBTAINED_FROM_JIRA_UI

options = {
  :site               => 'http://mydomain.atlassian.net:443/',
  :context_path       => '',
  :username           => '<the email you sign-in to Jira>',
  :password           => api_token,
  :auth_type          => :basic
}

client = JIRA::Client.new(options)

project = client.Project.find('SAMPLEPROJECT')

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

Can someone clarify what we actually need to pass in? Was the example incorrect?

logan-barnett-nwea commented 1 year ago

@HongQiGong I found this confusing as well. I think what happened is the work here didn't update the paragraph above: https://github.com/sumoheavy/jira-ruby/pull/396/files

My working version was to use :username and :password. When using :default_headers with the API token given as the bearer token, I got a Forbidden error from Jira. Under :username and :password I got a new error (BadRequest, unrelated to this ticket) and I could see in my access token view (https://id.atlassian.com/manage-profile/security/api-tokens) that my access token had been recently used.

It's also worth mentioning that I'm hitting Jira Cloud and not Jira Data Center (their new name for their on-prem suite). The README links to the Data Center documentation.

Perhaps we need both examples of Data Center and Cloud suites?

KingOfSpades commented 10 months ago

When using datacenter or selfhosted Jira you can use the token without username, working example:

require 'rubygems'
require 'jira-ruby'

# NOTE: the token should not be encoded
jira_site = "https://YOUR_JIRA_URL"
api_token = "YOUR_TOKEN"

options = {
  :site               => jira_site,
  :context_path       => '',
  :auth_type          => :basic,
  :default_headers    => { 'Authorization' =>  "Bearer #{api_token}" }
}

client = JIRA::Client.new(options)

issues = client.Issue.jql("assignee = currentUser()")

issues.issues.each do |issue|
  puts "#{issue.id} - #{issue.summary}"
end
bobbrodie commented 4 months ago

Hey everyone, I know this is an older message, but am doing some cleanup and preparing for a new version. I'm working on building out the wiki, but in general something important here is that Personal Access Tokens ≠ API Tokens. I believe that PAT are available in on-prem versions of Jira but in cloud they are API Tokens. You can generate them and use them in place of your user's password.

I've added a page here for clarification.

Thanks!