Forums

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

Where to put token in my real Java code in Eclipse

Mila Shutova February 25, 2021

I'm learning API autotesting and writing tests on Eclipse(Java). Before I had trial on localhost and the following code was working to authenticate:
 String response = given().header("Content-Type", "application/json").body("{ \"username\": \"blablabla@gmail.com\", \"password\": \"vfnvdf\" }" ).log().all().filter(session).post("/rest/auth/1/session").then().log().all().
 extract().response(). asString();


But now, it has expired and I've moved to "https://testing-api.atlassian.net" and I cannot understand what to modify.
I found that example code "curl -D- \ -X GET \ -H "Authorization: Basic ZnJlZDpmcmVk" \ -H "Content-Type: application/json" \ "https://your-domain.atlassian.net/rest/api/2/issue/QA-31"" and I generated my token on Jira, but I cannot understand how to put it into my code 
please help

If I run it, it tells me:

"


HTTP/1.1 401 Unauthorized
Server: AtlassianProxy/1.15.8.1
www-authenticate: JIRA REST POST
www-authenticate: OAuth realm="https%3A%2F%2Ftesting-api.atlassian.net"
cache-control: no-cache, no-store, no-transform
Content-Type: application/json;charset=UTF-8
Strict-Transport-Security: max-age=315360000; includeSubDomains; preload
Date: Fri, 26 Feb 2021 07:30:35 GMT
ATL-TraceId: 6d9b29cebff5e3d5
x-arequestid: 6b8c29dd-62d0-4d1d-ab00-f068e083f4bd
X-XSS-Protection: 1; mode=block
Transfer-Encoding: chunked
timing-allow-origin: *
x-envoy-upstream-service-time: 19
X-Content-Type-Options: nosniff
Connection: keep-alive
set-cookie: atlassian.xsrf.token=b8f587a6-a6a8-4887-99fe-198ec696f65e_170d7685aacb6b99e56f047eec1d73e42a90d3f2_lout; Path=/; Secure
set-cookie: jsd.portal.language.anonymous=en-US; Expires=Fri, 26-Feb-2021 07:31:35 GMT; Path=/
Expect-CT: report-uri="https://web-security-reports.services.atlassian.com/expect-ct-report/global-proxy", enforce, max-age=86400

{
"errorMessages": [
"Login failed"
],
"errors": {

}
}"

1 answer

1 accepted

0 votes
Answer accepted
Daniel Eads
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 1, 2021

Hi @Mila Shutova , welcome to the Community!

From the curl example you posted, it looks as though you're trying to use a single token (and no username) with Basic authentication. While Basic auth does work - it requires a username (your email address) with Atlassian Cloud. Consider this example:

 

curl -v https://mysite.atlassian.net --user me@example.com:my-api-token

The email address is listed for the user portion, there is a colon, and then the API token. While we say token, you could consider it a drop-in replacement for "password" in Basic auth. If you had working code with Server, the same code will likely work with Cloud by swapping out the password with the API token you generated at https://id.atlassian.com/manage-profile/security/api-tokens and retaining the username portion, which looks like it was already an email address in your Server example.

I hope that helps clarify things with how Basic authentication is used on Atlassian Cloud!

Cheers,
Daniel | Atlassian Support

Mila Shutova March 2, 2021

Thank you :) I did update my code and it worked just fine

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events