客户端凭据应通过身份验证基本认证 http://en.wikipedia.org/wiki/Basic_access_authentication。即带有标题
Authorization: Basic base64encode(clientId:clientSecret)
where base64encode(clientId:clientSecret)
是实际的base64编码字符串clientId:clientSecret
。因此,要更新您的界面,它可能看起来更像
public interface OAuthTokenService {
@POST("/api/token")
@FormUrlEncoded
@Headers({
"Accept: application/json"
})
AccessTokenResponse getAccessToken(@Field("grant_type") String grantType,
@Field("username") String username,
@Field("password") String password,
@Header("Authorization") String authorization);
}
然后要设置标题,请执行类似的操作
public class Main {
public static void main(String[] args) {
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint("http://localhost:8080")
.setConverter(new JacksonConverter())
.build();
OAuthTokenService service = restAdapter.create(OAuthTokenService.class);
byte[] credentials = "clientId:clientSecret".getBytes();
String basicAuth = "Basic " + Base64.getEncoder().encodeToString(credentials);
AccessTokenResponse response = service
.getAccessToken("password", "admin", "admin", basicAuth);
System.out.println(response.getAccessToken());
}
}
请注意,上面使用 Java 8java.util.Base64
班级。您可能没有使用 Java 8,在这种情况下,您将需要找到不同的编码器。
我也使用Jackson进行转换,只是因为我不使用Gson。以上内容已经过测试,应该也适合您。