Не работает YobitApi
Пытаюсь реализовать метод getInfo в API криптобиржи https://yobit.net/ru/api/
Приходит результат
{"success":0,"error":"invalid key, sign, method or nonce"}
Апи работает, есть примеры Python. На джаве примеров не густо(
Бьюсь уже несколько месяцев. В чём ошибка? код прикладываю:
Main activity
public class MainActivity extends AppCompatActivity {
public static final String SHARED_PREFS = "sharedPrefs";
public static SharedPreferences sharedPreferences;
public static SharedPreferences.Editor editor;
public static String publicApi = "https://yobit.net/api/3/";
public static String tradeApi = "https://yobit.net/";
private static final String HMAC_SHA512 = "HmacSHA512";
public static final String key = "";
public static final String secret = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
editor = sharedPreferences.edit();
int nonce = sharedPreferences.getInt("nonce", 1);
editor.putInt("nonce", ++nonce);
editor.apply();
String params = "method=getInfo&" + "nonce=" + nonce ;
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
okHttpClient.addInterceptor(httpLoggingInterceptor);
OkHttpClient client = okHttpClient.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(tradeApi)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client)
.build();
try {
String signParams = "";
try {
signParams = calculateHMAC(params, secret);
String finalSign = signParams;
okHttpClient.addInterceptor(chain -> {
Request request = chain.request().newBuilder()
.addHeader("Key", key)
.addHeader("Sign", finalSign)
.build();
return chain.proceed(request);
});
Api api = retrofit.create(Api.class);
Call<ResponseBody> call = api.getInfo();
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
Log.e("onResponse", response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("onFailure", String.valueOf(t.getMessage()));
}
});
} catch (SignatureException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
} finally {
}
}
private static String toHexString(byte[] bytes) {
Formatter formatter = new Formatter();
for (byte b : bytes) {
formatter.format("%02x", b);
}
return formatter.toString();
}
public static String calculateHMAC(String data, String key)
throws SignatureException, NoSuchAlgorithmException, InvalidKeyException
{
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), HMAC_SHA512);
Mac mac = Mac.getInstance(HMAC_SHA512);
mac.init(secretKeySpec);
return toHexString(mac.doFinal(data.getBytes()));
}
}
Api
public interface Api {
@POST("info")
Call<Info> info();
@POST("tapi/")
Call<ResponseBody> getInfo();
}