gpt4 book ai didi

android - 将 cURL 转换为 Retrofit

转载 作者:行者123 更新时间:2023-11-29 01:25:00 25 4
gpt4 key购买 nike

我正在尝试实现 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 的样本

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"}

我的网络接口(interface)

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

请求 token

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(或不同的方法)调用它,提前致谢

最佳答案

看起来你的调用应该如下所示

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

关于android - 将 cURL 转换为 Retrofit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34538392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com