Android OAuth Retrofit访问令牌请求

Rev*_*opi 6 android oauth-2.0 retrofit

任何人都可以告诉确切的格式将下面的代码转换为改造

curl -X POST -d "grant_type=password&username=admin&password=admin&scope=read+write" -u"clientId:clientSecret" http://myserver/o/token/
Run Code Online (Sandbox Code Playgroud)

我尝试过类似的东西,但它没有用

@FormUrlEncoded
@POST("/o/token/")
AccessTokenResponse getToken(@Field("client_id") String client_id, @Field("client_secret") String client_secret,
    @Field("grant_type") String grant_type, @Field("username") String username,
    @Field("password") String password, @Field("scope") String scope);
Run Code Online (Sandbox Code Playgroud)

Pau*_*tha 6

客户端凭据应当与被验证基本身份验证.即带标题

Authorization: Basic base64encode(clientId:clientSecret)
Run Code Online (Sandbox Code Playgroud)

其中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);    
}
Run Code Online (Sandbox Code Playgroud)

然后设置标题,做类似的事情

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

注意上面使用Java 8作为java.util.Base64类.您可能没有使用Java 8,在这种情况下,您需要找到不同的编码器.

我也使用Jackson进行转换,只是因为我不使用Gson.以上内容已经过测试,也适用于您.


小智 5

使用 OkHttp 拦截器,这变得更容易了。

Interceptor interceptor = chain -> {
        Request original = chain.request();
        Request request = original.newBuilder()
                .header("Authorization", Credentials.basic(CLIENT_ID, CLIENT_SECRET))
                .method(original.method(), original.body())
                .build();
        return chain.proceed(request);
    };

OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .build();

return new Retrofit.Builder()
                .baseUrl(baseURL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
Run Code Online (Sandbox Code Playgroud)

Credentials.basic 方法将基于 64 位编码您的客户端 ID 和客户端密码。然后将拦截器附加到OkHttpClient客户端并添加到Retrofit对象中。