我正在尝试实施OAuth2。我需要将它用于我的学校项目 - 学校提供一些文档,它说我需要调用这个
POST /oauth/token HTTP/1.1
Host: oaas.example.org
Authorization: Basic ZHVtbXktY2xpZW50OnRvcC1zZWNyZXQ=
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=l1kSf2&redirect_uri=https://client.example.org/auth
或有卷曲样本
curl --data "grant_type=authorization_code&code=l1kSf2&redirect_uri=https://client.example.org/auth" \
--user dummy-client:top-secret https://oaas.example.org/oauth/token
但我不知道如何将其转移到Android调用。我目前正在使用Retrofit,并且reguest就像JSON:
POST /oauth/oauth/token/DeviceAndroid HTTP/1.1
Authorization: Basic OGRkOGJiZGMtOGI2NC00NTdlLTgwYWMtZmE5NDZjNzY4Njgzc0owY3RKV2VWNHFLY0dwWHNIOGEzcDVYYUl0NDRGN2o=
Content-Type: application/x-www-form-urlencoded
Content-Length: 85
Host: auth.fit.cvut.cz
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/2.5.0
{"code":"p1P3os","grant_type":"authorization_code","redirect_uri":"http://localhost"}
我的网络界面
public interface NetworkInterface {
String TAG = NetworkInterface.class.getName();
String URL = "https://auth.fit.cvut.cz";
@Headers("Content-Type: application/x-www-form-urlencoded")
@POST("/oauth/oauth/token/DeviceAndroid")
Call<Object> sendInformationToServer(@Body RequestToken requestToken);
}
RequestToken
public class RequestToken {
public static final String TAG = RequestToken.class.getName();
public String grant_type;
public String code;
public String redirect_uri;
...
服务生成器
public class ServiceGenerator {
private static OkHttpClient httpClient = new OkHttpClient();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(NetworkInterface.URL)
.addConverterFactory(GsonConverterFactory.create());
public static <T> T createService(Class<T> serviceClass , final String autorizationString) {
httpClient = new OkHttpClient();
if (autorizationString != null) {
httpClient.interceptors().clear();
httpClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Authorization", "Basic " + RequestToken.returnBase64(autorizationString))
.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
}
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);
}
}
有没有办法用Retrofit(或采用不同方法)调用它,提前致谢
答案 0 :(得分:1)
您的通话似乎应如下所示
public interface NetworkInterface {
private String TAG = NetworkInterface.class.getName();
public statsic final String ROOT_URL = "https://auth.fit.cvut.cz";
@FormUrlEncoded
@POST("/oauth/token")
Call<Object> sendInformationToServer(
@Field("code") String code,
@Field("grant_type") String grant,
@Field("redirect_uri") String redirect,
);
}
像这样使用:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(NetworkInterface.ROOT_URL)
...
.build();
NetworkInterface service = retrofit.create(NetworkInterface .class);
service.sendInformationToServer("p1P3os", "authorization_code", "http://localhost"})