gpt4 book ai didi

用于安卓的身份验证 Apollo Graphql

转载 作者:行者123 更新时间:2023-12-04 04:27:23 27 4
gpt4 key购买 nike

我正在开发一个使用 GraphQL 作为后端的 android 应用程序。我让查询和变异部分完美地工作。但是我找不到任何用于身份验证的文件。

那么如何将用户名和密码传递给服务器并对其进行身份验证?

LocalApolloClient.java :

public class LocalApolloClient {
private static final String URL = "http://192.168.1.100/graphql/";
private static ApolloClient apolloClient;
private static String authHeader = "";


public static ApolloClient getApolloClient(){
//HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
//loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(chain -> {
Request original = chain.request();
Request.Builder builder = original.newBuilder().method(original.method(), original.body());
builder.header("Authorization", authHeader);
return chain.proceed(builder.build());
})
.build();

apolloClient = ApolloClient.builder()
.serverUrl(URL)
.okHttpClient(okHttpClient)
.build();

return apolloClient;
}
}

请注意:
  • 这不是重复的问题
  • graphQl
  • 没有适当的文档

    所以,如果你投反对票,请为自己辩解。

    最佳答案

    您需要在 ApolloClient 上设置 header ,而不是在 OkHttpClient 上。
    像这样的东西:

    ApolloClient.builder()
    .serverUrl(context.getString(R.string.graphql_base_url))
    .addApplicationInterceptor(object: ApolloInterceptor {
    override fun interceptAsync(
    request: ApolloInterceptor.InterceptorRequest,
    chain: ApolloInterceptorChain,
    dispatcher: Executor,
    callBack: ApolloInterceptor.CallBack
    ) {
    val newRequest = request.toBuilder().requestHeaders(RequestHeaders.builder().addHeader("Authorization", "Basic ...").build()).build()
    chain.proceedAsync(newRequest, dispatcher, callBack)
    }

    override fun dispose() {
    }
    }).build()

    关于用于安卓的身份验证 Apollo Graphql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52441693/

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