URL: https://a.klaviyo.com/api/profile-subsc ... eate-jobs/
Это мой метод запроса публикации:
Код: Выделить всё
Header : revision:2024-07-15
content-type:application/json
accept:application/json
Authorization:Klaviyo-API-Key android89459832432sdskjha
request body raw type :
{"data":{"type":"profile-subscription-bulk-create-job","attributes":{"profiles":{"data":[{"type":"profile","attributes":{"email":"anilkt87@gmail.com"}}]}},"relationships":{"list":{"data":{"type":"list","id":"YXOSus"}}}}}
когда я пытаюсь выполнить тот же код с помощью Retrofit в Android используя приведенный ниже код:
Код: Выделить всё
public void createSubscription() {
klaviyoService = KalvioRetrofitClient.getKlaviyoService();
String data = "{\"data\":{\"type\":\"profile-subscription-bulk-create-job\",\"attributes\":{\"profiles\":{\"data\":[{\"type\":\"profile\",\"attributes\":{\"email\":\"anilkt87@gmail.com\"}}]}},\"relationships\":{\"list\":{\"data\":{\"type\":\"list\",\"id\":\"YXOSus\"}}}}}";
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(data);
} catch (JSONException e) {
throw new RuntimeException(e);
}
Call call = klaviyoService.createSubscription(jsonObject);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.isSuccessful()) {
Log.d("Klaviyo", "Subscription status: " + response);
} else {
Log.e("Klaviyo", "Failed to create subscription: " + response.errorBody());
}
}
@Override
public void onFailure(Call call, Throwable t) {
Log.e("Klaviyo", "Error creating subscription", t);
}
});
}
public class KalvioRetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit == null) {
// Set up logging interceptor (optional, for debugging)
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@NonNull
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
Request request = chain.request().newBuilder().addHeader("Content-Type", "application/json")
.addHeader("accept", "application/json").addHeader("revision", "2024-07-15").addHeader("Authorization", "Klaviyo-API-Key android89459832432sdskjha").build();
return chain.proceed(request);
}
})
.build();
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
public static KlaviyoService getKlaviyoService() {
return getClient("https://a.klaviyo.com/").create(KlaviyoService.class);
}
}
public interface KlaviyoService {
@POST("api/profile-subscription-bulk-create-jobs")
Call createSubscription(@Body JSONObject klaviyoRequest);
}
Я не знаю, почему возникает эта ошибка, хотя я указал правильный ключ и идентификатор списка. То же самое работает нормально в Post Man, но не используется модификация. позвоните.
может ли кто-нибудь мне помочь, что я делаю неправильно?
Спасибо
Подробнее здесь: https://stackoverflow.com/questions/788 ... in-android
Мобильная версия