我想通过REST获得访问令牌OAuth 2.0,问题是我已经通过Bash脚本(curl命令)成功地从服务器上获得了它。
Bash脚本(工作):
#!/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使用该代码片段,我得到了以下响应:
{
"access_token": "eyJraWQiOiIxIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJhZG1pbiIsImF1ZCI...",
"token_type": "Bearer"
}当我尝试使用Unirest将其转换为Java时:
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());答复如下:
{"error_description":"Invalid grant","error":"invalid_grant"}经过几次研究和尝试,我仍然不知道我在Java代码请求中遗漏了什么。有人能帮我增加缺失的东西或者引导我找到正确的方向吗?
CollabNet文档:
萨索
发布于 2019-01-09 09:40:28
请尝试:
JsonNode jsonResponse = Unirest.post("https://teamforge.example.com/sf/auth/token")
.header("Content-Type", "application/json")
.field("scope", "urn:ctf:services:ctf")
.field("client_id", "api-client")
.field("grant_type", "password")
.field("username", "foo")
.field("password", "bar")
.asJson()
.getBody();还有一个关于格兰特类型的问题你确定吗?
grant_type = client_credentials,也许你需要这样的东西。
https://stackoverflow.com/questions/53447784
复制相似问题