Apollo-android: how to initiate a request with authorization header?

Created on 16 May 2017  Â·  7Comments  Â·  Source: apollographql/apollo-android

Most helpful comment

OkHttpClient httpClient = 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()
      .okHttpClient(httpClient)
      ....

All 7 comments

Instantiate okhttp with an interceptor. Add your Auth header in the
interceptor. Instantiate apollo client by passing the okhttp instance to
apollo client builder.

On May 16, 2017 1:11 PM, "aaron" notifications@github.com wrote:

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/apollographql/apollo-android/issues/497, or mute the
thread
https://github.com/notifications/unsubscribe-auth/AEUX3LjqrhVoGUH-Xb6dXfpCwn6-axJsks5r6XZCgaJpZM4NcRgi
.

OkHttpClient httpClient = 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()
      .okHttpClient(httpClient)
      ....

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

public static ApolloClient getApolloClient(String authHeader){
    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);
                Log.d("AUTH_TAG", authHeader);
                return chain.proceed(builder.build());
            })
            .build();

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

    return apolloClient;
}

}

added authHeader but still cant query the current user's data.

i solved it.
changing builder.header("Authorization", authHeader);

to

builder.header("Authorization", "JWT" + " " + authHeader);

On Wed, Sep 26, 2018 at 12:42 PM das_vicky notifications@github.com wrote:

in this case doesn't the getApolloClient always returns a client with a
constant old authHeader?

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/apollographql/apollo-android/issues/497#issuecomment-424609801,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AVNAqcqeJrLTNQ9FiHsyovLrUYxEh9bFks5ueyjBgaJpZM4NcRgi
.

In your code change the line

builder.header("Authorization", authHeader);

to

builder = builder.header("Authorization", authHeader);

Putting this example here for reference. Once you create an ApolloClient, you can also pass additional headers each time you do a call.

apolloClient.query(query)
        .requestHeaders(
            RequestHeaders.builder()
                .addHeader("Authorization", authHeader)
                .build()
        )
        .enqueue(...);

Or with RxJava in Kotlin

apolloClient.rxQuery(query) {
    requestHeaders(
        RequestHeaders.builder()
            .addHeader("Authorization", authHeader)
            .build()
    )
}

In the latest version of Apollo client, requestHeaders is marked as deprecated. If anyone is looking for the correct way to do it in the latest version, you can do this:

apolloClient.query(query)
.toBuilder()
.requestHeaders(
RequestHeaders.builder()
.addHeader("Authorization", authHeader)
.build()
)
.build()
.enqueue(...);

Was this page helpful?
0 / 5 - 0 ratings