albert kim
albert kim

Reputation: 333

API key is always missing when I try to access MailChimp's api using Retrofit

I'm sure this is a non issue for someone who knows what they're doing, but this is my first time using MailChimp's api and retrofit. Basically, my api key is always missing. I have no idea what MailChimp's proper url looks like when you are including an api key.

The url I am attempting is:

/3.0/lists/613cd953b2/[email protected]:apikey

And others of that variety

http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#create-post_lists_list_id_members

curl --request GET \

--url 'https://.api.mailchimp.com/3.0/' \

--user 'anystring:your_apikey'

This says GET but I am attempting a post.

Here is my android / java code...

final OkHttpClient client = new OkHttpClient();
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    client.interceptors().add(interceptor);

   
    client.interceptors().add(new Interceptor() {
        @Override
        public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
            Request original = chain.request();

            Request.Builder requestBuilder = original.newBuilder()
                   // .header("Authorization", basic)
                    .header("user", "[email protected]:apikey");

            Request request = requestBuilder.build();
            return chain.proceed(request);
        }
    });

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    //Create interface instance
    MailChimpAPI apiService = retrofit.create(MailChimpAPI.class);

    Email chimpEmail = new Email("[email protected]");

    Call<Email> call = apiService.addUser(chimpEmail);
    call.enqueue(new Callback<Email>() {
        @Override
        public void onResponse(Response<Email> response, Retrofit retrofit) {
            int statusCode = response.code();
            //Log.d("tag", "enter the void");
            Log.d("tag", statusCode + "");
            Log.d("tag", client.toString());
        }

        @Override
        public void onFailure(Throwable t) {
            Log.d("tag", t.toString());
        }
    });

And how I define my interface method

@POST("/3.0/lists/613cd953b2/members")
Call<Email> addUser(@Body Email email);

And my retrofit log...

D/OkHttp: --> POST /3.0/lists/613cd953b2/members HTTP/1.1
D/OkHttp: {"email_address":"[email protected]","merge_fields":{"FNAME":"","LNAME":""},"status":""}
D/OkHttp: --> END POST (79-byte body)
D/OkHttp: <-- HTTP/1.1 401  (299ms)
D/OkHttp: OkHttp-Selected-Protocol: h2
D/OkHttp: server: nginx
D/OkHttp: content-type: application/problem+json; charset=utf-8
D/OkHttp: x-request-id: 6b6ca6a9-c141-45fc-8de7-ec0922892bbf
D/OkHttp: link: <https://us12.api.mailchimp.com/schema/3.0/ProblemDetailDocument.json>; rel="describedBy"
D/OkHttp: vary: Accept-Encoding
D/OkHttp: date: Tue, 05 Jan 2016 21:21:45 GMT
D/OkHttp: set-cookie: _AVESTA_ENVIRONMENT=prod; path=/
D/OkHttp: OkHttp-Sent-Millis: 1452028904738
D/OkHttp: OkHttp-Received-Millis: 1452028904830
D/OkHttp: {"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"API Key Missing","status":401,"detail":"Your request did not include an API key.","instance":""}
D/OkHttp: <-- END HTTP (193-byte body)

Upvotes: 0

Views: 1096

Answers (1)

albert kim
albert kim

Reputation: 333

I was not using basic authentication properly. I followed this tutorial

https://futurestud.io/blog/android-basic-authentication-with-retrofit

and it fixed my error! Yay for new errors!

Also for future people, maybe this might help you.

BASIC AUTHORIZATION isn't just a way to describe something! It's an actual concept and implementable! haha

Upvotes: 3

Related Questions