Java OAuth 2.0 get access token
I want to get access token OAuth 2.0 from REST API via Java code, the thing is that I've managed to successfully get it back from the server with Bash script (curl command)
Bash script (working):
#!/usr/bin/env bash
# Base URL of TeamForge site.
site_url="https://teamforge.example.com"
# TeamForge authentication credentials.
username="foo"
password="bar"
# Requested scope (all)
scope="urn:ctf:services:ctf
curl -d "grant_type=password&client_id=api-client&scope=$scope&username=$username&password=$password" $site_url/sf/auth/token
With that code snippet I'got this response:
{
"access_token": "eyJraWQiOiIxIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJhZG1pbiIsImF1ZCI...",
"token_type": "Bearer"
}
When I've tried to translate it to Java code using Unirest :
HttpResponse<JsonNode> jsonResponse = Unirest.post(""https://teamforge.example.com/sf/auth/token")
.header("accept", "application/json")
.body("{"grant_type":"password"," +
""client_id":"api-client", " +
""scope":"urn:ctf:services:ctf"," +
""username":"foo"," +
""password":"bar"}")
.asJson();
System.out.println(jsonResponse.getBody());
Response was:
{"error_description":"Invalid grant","error":"invalid_grant"}
After a couple of researches and tries, I still don't know what am I missing in my Java code request. Can someone help me to add missing stuff or guide me to right directions?
CollabNet docs:
Saso
java rest curl oauth-2.0 collabnet
add a comment |
I want to get access token OAuth 2.0 from REST API via Java code, the thing is that I've managed to successfully get it back from the server with Bash script (curl command)
Bash script (working):
#!/usr/bin/env bash
# Base URL of TeamForge site.
site_url="https://teamforge.example.com"
# TeamForge authentication credentials.
username="foo"
password="bar"
# Requested scope (all)
scope="urn:ctf:services:ctf
curl -d "grant_type=password&client_id=api-client&scope=$scope&username=$username&password=$password" $site_url/sf/auth/token
With that code snippet I'got this response:
{
"access_token": "eyJraWQiOiIxIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJhZG1pbiIsImF1ZCI...",
"token_type": "Bearer"
}
When I've tried to translate it to Java code using Unirest :
HttpResponse<JsonNode> jsonResponse = Unirest.post(""https://teamforge.example.com/sf/auth/token")
.header("accept", "application/json")
.body("{"grant_type":"password"," +
""client_id":"api-client", " +
""scope":"urn:ctf:services:ctf"," +
""username":"foo"," +
""password":"bar"}")
.asJson();
System.out.println(jsonResponse.getBody());
Response was:
{"error_description":"Invalid grant","error":"invalid_grant"}
After a couple of researches and tries, I still don't know what am I missing in my Java code request. Can someone help me to add missing stuff or guide me to right directions?
CollabNet docs:
Saso
java rest curl oauth-2.0 collabnet
Your Java code is using a JSON body, but the bash script has the parameters in the post request directly, not in a JSON body. Have you tried calling it the same way?
– JPinzon01
Nov 23 '18 at 15:55
@JPinzon01 You mean creating a POST request with parameters in the URL? Example:POST: www.myapi.com?grant_type=password&client_id=api-client&scope=$scope&username=$username&password=$password
– Sašo Pavlič
Nov 28 '18 at 19:28
Not in the URL, but in the body. You can try putting the same string you use for the bash script instead of the JSON. The thing is the Oauth2 server is not expecting a JSON request, is expecting a regular POST with HTTP parameters in the body. The response is a JSON object, but the request doesn't have to be the same format.
– JPinzon01
Nov 29 '18 at 21:06
Thank you for the comment, but I tried like you suggested and it is still now working. Error message "{"error_description":"Invalid request","error":"invalid_request"}"
– Sašo Pavlič
Dec 5 '18 at 8:50
add a comment |
I want to get access token OAuth 2.0 from REST API via Java code, the thing is that I've managed to successfully get it back from the server with Bash script (curl command)
Bash script (working):
#!/usr/bin/env bash
# Base URL of TeamForge site.
site_url="https://teamforge.example.com"
# TeamForge authentication credentials.
username="foo"
password="bar"
# Requested scope (all)
scope="urn:ctf:services:ctf
curl -d "grant_type=password&client_id=api-client&scope=$scope&username=$username&password=$password" $site_url/sf/auth/token
With that code snippet I'got this response:
{
"access_token": "eyJraWQiOiIxIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJhZG1pbiIsImF1ZCI...",
"token_type": "Bearer"
}
When I've tried to translate it to Java code using Unirest :
HttpResponse<JsonNode> jsonResponse = Unirest.post(""https://teamforge.example.com/sf/auth/token")
.header("accept", "application/json")
.body("{"grant_type":"password"," +
""client_id":"api-client", " +
""scope":"urn:ctf:services:ctf"," +
""username":"foo"," +
""password":"bar"}")
.asJson();
System.out.println(jsonResponse.getBody());
Response was:
{"error_description":"Invalid grant","error":"invalid_grant"}
After a couple of researches and tries, I still don't know what am I missing in my Java code request. Can someone help me to add missing stuff or guide me to right directions?
CollabNet docs:
Saso
java rest curl oauth-2.0 collabnet
I want to get access token OAuth 2.0 from REST API via Java code, the thing is that I've managed to successfully get it back from the server with Bash script (curl command)
Bash script (working):
#!/usr/bin/env bash
# Base URL of TeamForge site.
site_url="https://teamforge.example.com"
# TeamForge authentication credentials.
username="foo"
password="bar"
# Requested scope (all)
scope="urn:ctf:services:ctf
curl -d "grant_type=password&client_id=api-client&scope=$scope&username=$username&password=$password" $site_url/sf/auth/token
With that code snippet I'got this response:
{
"access_token": "eyJraWQiOiIxIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJhZG1pbiIsImF1ZCI...",
"token_type": "Bearer"
}
When I've tried to translate it to Java code using Unirest :
HttpResponse<JsonNode> jsonResponse = Unirest.post(""https://teamforge.example.com/sf/auth/token")
.header("accept", "application/json")
.body("{"grant_type":"password"," +
""client_id":"api-client", " +
""scope":"urn:ctf:services:ctf"," +
""username":"foo"," +
""password":"bar"}")
.asJson();
System.out.println(jsonResponse.getBody());
Response was:
{"error_description":"Invalid grant","error":"invalid_grant"}
After a couple of researches and tries, I still don't know what am I missing in my Java code request. Can someone help me to add missing stuff or guide me to right directions?
CollabNet docs:
Saso
java rest curl oauth-2.0 collabnet
java rest curl oauth-2.0 collabnet
edited Nov 27 '18 at 12:35
RakihthaRR
18511
18511
asked Nov 23 '18 at 13:40
Sašo Pavlič
11
11
Your Java code is using a JSON body, but the bash script has the parameters in the post request directly, not in a JSON body. Have you tried calling it the same way?
– JPinzon01
Nov 23 '18 at 15:55
@JPinzon01 You mean creating a POST request with parameters in the URL? Example:POST: www.myapi.com?grant_type=password&client_id=api-client&scope=$scope&username=$username&password=$password
– Sašo Pavlič
Nov 28 '18 at 19:28
Not in the URL, but in the body. You can try putting the same string you use for the bash script instead of the JSON. The thing is the Oauth2 server is not expecting a JSON request, is expecting a regular POST with HTTP parameters in the body. The response is a JSON object, but the request doesn't have to be the same format.
– JPinzon01
Nov 29 '18 at 21:06
Thank you for the comment, but I tried like you suggested and it is still now working. Error message "{"error_description":"Invalid request","error":"invalid_request"}"
– Sašo Pavlič
Dec 5 '18 at 8:50
add a comment |
Your Java code is using a JSON body, but the bash script has the parameters in the post request directly, not in a JSON body. Have you tried calling it the same way?
– JPinzon01
Nov 23 '18 at 15:55
@JPinzon01 You mean creating a POST request with parameters in the URL? Example:POST: www.myapi.com?grant_type=password&client_id=api-client&scope=$scope&username=$username&password=$password
– Sašo Pavlič
Nov 28 '18 at 19:28
Not in the URL, but in the body. You can try putting the same string you use for the bash script instead of the JSON. The thing is the Oauth2 server is not expecting a JSON request, is expecting a regular POST with HTTP parameters in the body. The response is a JSON object, but the request doesn't have to be the same format.
– JPinzon01
Nov 29 '18 at 21:06
Thank you for the comment, but I tried like you suggested and it is still now working. Error message "{"error_description":"Invalid request","error":"invalid_request"}"
– Sašo Pavlič
Dec 5 '18 at 8:50
Your Java code is using a JSON body, but the bash script has the parameters in the post request directly, not in a JSON body. Have you tried calling it the same way?
– JPinzon01
Nov 23 '18 at 15:55
Your Java code is using a JSON body, but the bash script has the parameters in the post request directly, not in a JSON body. Have you tried calling it the same way?
– JPinzon01
Nov 23 '18 at 15:55
@JPinzon01 You mean creating a POST request with parameters in the URL? Example:POST: www.myapi.com?grant_type=password&client_id=api-client&scope=$scope&username=$username&password=$password
– Sašo Pavlič
Nov 28 '18 at 19:28
@JPinzon01 You mean creating a POST request with parameters in the URL? Example:POST: www.myapi.com?grant_type=password&client_id=api-client&scope=$scope&username=$username&password=$password
– Sašo Pavlič
Nov 28 '18 at 19:28
Not in the URL, but in the body. You can try putting the same string you use for the bash script instead of the JSON. The thing is the Oauth2 server is not expecting a JSON request, is expecting a regular POST with HTTP parameters in the body. The response is a JSON object, but the request doesn't have to be the same format.
– JPinzon01
Nov 29 '18 at 21:06
Not in the URL, but in the body. You can try putting the same string you use for the bash script instead of the JSON. The thing is the Oauth2 server is not expecting a JSON request, is expecting a regular POST with HTTP parameters in the body. The response is a JSON object, but the request doesn't have to be the same format.
– JPinzon01
Nov 29 '18 at 21:06
Thank you for the comment, but I tried like you suggested and it is still now working. Error message "{"error_description":"Invalid request","error":"invalid_request"}"
– Sašo Pavlič
Dec 5 '18 at 8:50
Thank you for the comment, but I tried like you suggested and it is still now working. Error message "{"error_description":"Invalid request","error":"invalid_request"}"
– Sašo Pavlič
Dec 5 '18 at 8:50
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447784%2fjava-oauth-2-0-get-access-token%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447784%2fjava-oauth-2-0-get-access-token%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your Java code is using a JSON body, but the bash script has the parameters in the post request directly, not in a JSON body. Have you tried calling it the same way?
– JPinzon01
Nov 23 '18 at 15:55
@JPinzon01 You mean creating a POST request with parameters in the URL? Example:POST: www.myapi.com?grant_type=password&client_id=api-client&scope=$scope&username=$username&password=$password
– Sašo Pavlič
Nov 28 '18 at 19:28
Not in the URL, but in the body. You can try putting the same string you use for the bash script instead of the JSON. The thing is the Oauth2 server is not expecting a JSON request, is expecting a regular POST with HTTP parameters in the body. The response is a JSON object, but the request doesn't have to be the same format.
– JPinzon01
Nov 29 '18 at 21:06
Thank you for the comment, but I tried like you suggested and it is still now working. Error message "{"error_description":"Invalid request","error":"invalid_request"}"
– Sašo Pavlič
Dec 5 '18 at 8:50