我正在 Android 中使用 Retrofit(不是新的)来进行一些 OAuth 授权。 我已经完成了获取要使用的代码的第一步,现在规范中的下一步就是执行此操作:
curl -u : http://example.com/admin/oauth/token -d 'grant_type=authorization_code&code='
我从来没有做过curl请求,我不知道该怎么做,尤其是Retrofit及其接口(interface)。
我看了一下这个
How to make a CURL request with retrofit?
但这家伙没有-d东西
最佳答案
curl 的 -d
参数添加 POST 参数。在本例中,为 grant_type
和 code
。我们可以使用 @Field
注释对改造中的每一个进行编码。
public interface AuthApi {
@FormUrlEncoded
@POST("/admin/oauth/token")
void getImage(@Header("Authorization") String authorization,
@Field(("grant_type"))String grantType,
@Field("code") String code,
Callback<Object> callback);
}
授权字段使用针对链接问题给出的@Header
解决方案。
用法就像--
RestAdapter authAdapter = new RestAdapter.Builder().setEndpoint("http://example.com/").build();
AuthApi authApi = authAdapter.create(AuthApi.class);
try {
final String auth = "Basic " + getBase64String(":");
authApi.getImage(auth, "authorization_code", "", new Callback<Object>() {
@Override
public void success(Object o, Response response) {
// handle success
}
@Override
public void failure(RetrofitError error) {
// handle failure
}
});
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
其中 getBase64String
是链接答案中的辅助方法。为了完整起见,复制如下 -
public static String getBase64String(String value) throws UnsupportedEncodingException {
return Base64.encodeToString(value.getBytes("UTF-8"), Base64.NO_WRAP);
}
关于android - 带 Retrofit 的 CURL -u 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33580568/