【问题标题】:How to retrieve access token, with parameters: Client_ID, Client_Secret, Auth_Url, Access_token_URL?如何使用参数检索访问令牌:Client_ID、Client_Secret、Auth_Url、Access_token_URL?
【发布时间】:2018-06-27 10:11:16
【问题描述】:

我收到以下代码:

{"data":{"error":"Invalid grant_type parameter or parameter missing","request":"\/oauth2\/token","method":"Post"}
public class Authentication_Token {
    private String Client_ID = "9d99755c****d";
    private String Client_Secret= "dbdb21c0de1b***ab1d4cf6a4b92535ed2";
    private String Auth_URL= "https://api.imgur.com/oauth2/authorize";
    private String Access_Token_URL="https://api.imgur.com/oauth2/token";

    @Test
    public void  oauth2() {
         // Still have the same error :                                                                                                                                                   
        Response resp = given()
            .contentType("application/x-www-form-urlencoded")
            .parameters("Client ID", Client_ID,
                        "Client Secret", Client_Secret,
                        "username", "testbot@gmail.com",
                        "password", "testbota#1",
                        "grant_type", "authorization_Code",
                        "Access_Token_URL", Access_Token_URL)
            .auth()
                .preemptive().basic(Client_ID, Client_Secret)
            .post(Auth_URL);  
    }
}

【问题讨论】:

  • 你为什么在帖子中使用“Auth_URL”?您应该使用令牌 URL,即 Access_Token_URL。我错过了什么吗? oAuth 中有 2 个不同的 URL,首先是 GET,然后是 POST (/token)
  • @umeshinator:您要加载访问令牌还是授权码?第一个网址是/oauth2/token,第二个网址是/oauth2/authorize。

标签: java rest oauth-2.0 spring-security-oauth2


【解决方案1】:

在 Oauth 2.0 中,grant_type 是参数而不是标头。

见specification

我建议你使用这样的东西:

given().parameters("username", "testbot@gmail.com", "password", "testbota#1", 
                           "grant_type", "Authorizaton_Code")

【讨论】:

  • 感谢 Laurent,我确实更改了我的代码 a,但是在参数中使用了 grant_type 但我仍然有同样的错误,因为有两个 URL 的 Auth_URL 和 Access_Token_URL ,如何传递这两个 URL 令人困惑?
  • auth_url 用于获取授权码,access_token url 用于依赖方将该码换成令牌。
猜你喜欢
  • 1970-01-01
  • 2016-09-09
  • 2015-09-08
  • 2018-02-20
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多