我敢肯定,对于知道自己在做什么的人来说,这不是问题,但这是我第一次使用 MailChimp 的 api 和改造。基本上,我的 api 密钥总是丢失。当您包含 api 密钥时,我不知道 MailChimp 的正确 url 是什么样的。
我正在尝试的网址是:
/3.0/lists/613cd953b2/members?user=email@gmail.com:apikey
和其他种类的
卷曲——请求获取\
--url 'https://.api.mailchimp.com/3.0/' \
--user 'anystring:your_apikey'
这说 GET 但我正在尝试发帖。
这是我的 android / java 代码...
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@gmail.com: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("hi@hi.hi");
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());
}
});
以及我如何定义我的接口方法
@POST("/3.0/lists/613cd953b2/members")
Call<Email> addUser(@Body Email email);
还有我的改装日志...
D/OkHttp: --> POST /3.0/lists/613cd953b2/members HTTP/1.1
D/OkHttp: {"email_address":"hi@hi.hi","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)