如何使用参数检索访问令牌:Client_ID,Client_Secret,Auth_Url,Access_token_URL?

时间:2018-01-18 10:53:21

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

我收到以下代码:

{"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);  
    }
}

1 个答案:

答案 0 :(得分:0)

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

请参阅specification

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

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