首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java 2.0 OAuth访问令牌

Java 2.0 OAuth访问令牌
EN

Stack Overflow用户
提问于 2018-11-23 13:40:00
回答 1查看 13.2K关注 0票数 0

我想通过REST获得访问令牌OAuth 2.0,问题是我已经通过Bash脚本(curl命令)成功地从服务器上获得了它。

Bash脚本(工作):

代码语言:javascript
复制
#!/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

使用该代码片段,我得到了以下响应:

代码语言:javascript
复制
  {
         "access_token": "eyJraWQiOiIxIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJhZG1pbiIsImF1ZCI...",
         "token_type": "Bearer"
       }

当我尝试使用Unirest将其转换为Java时:

代码语言:javascript
复制
  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());

答复如下:

代码语言:javascript
复制
{"error_description":"Invalid grant","error":"invalid_grant"}

经过几次研究和尝试,我仍然不知道我在Java代码请求中遗漏了什么。有人能帮我增加缺失的东西或者引导我找到正确的方向吗?

CollabNet文档:

萨索

EN

回答 1

Stack Overflow用户

发布于 2019-01-09 09:40:28

请尝试:

代码语言:javascript
复制
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,也许你需要这样的东西。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53447784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档