簡體   English   中英

Fortnite Tracker API似乎無法正常運行,或者我做錯了什么?

[英]Fortnite Tracker API seems not to be working or I am doing something wrong?

我遇到了一個問題。 我想從Fortnite Tracker API中獲取數據,我從網站上擁有自己的API密鑰,並且我可以肯定我做得很好,但是該網站向我返回了ErrorInputStream並顯示了以下消息:

{"message":"Invalid authentication credentials"}

100%確信我給該網站提供了正確的憑據。

private RadioGroup radioGroup;
private EditText editText;

private static String APIKEY = "some-api-key";
private static String REQUEST_URL = "https://api.fortnitetracker.com/v1/profile/%platform%/%epic-nickname%";
private static String LOG_TAG = "INFO";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    radioGroup = findViewById(R.id.radioGroup);
    editText = findViewById(R.id.editText);
}

public void searchStats(View view) {
    if(editText.getText().toString().isEmpty()) {
        Toast.makeText(getApplicationContext(), "Please enter a nickname !", Toast.LENGTH_LONG).show();
        editText.requestFocus();
        return;
    }
    if(radioGroup.getCheckedRadioButtonId() == -1) {
        Toast.makeText(getApplicationContext(), "Please select a platform !", Toast.LENGTH_LONG).show();
        radioGroup.requestFocus();
        return;
    }

    DownloadTask dt = new DownloadTask();
    dt.execute();
}

private String getSelectedPlatformName(RadioGroup group) {
    RadioButton button =  findViewById(group.getCheckedRadioButtonId());
    return button.getText().toString().toLowerCase();
}

public class DownloadTask extends AsyncTask<String,Void,String> {

    @Override
    protected String doInBackground(String... strings) {
        URL url = null;
        try {
            url = new URL(REQUEST_URL.replaceAll("%platform%", getSelectedPlatformName(radioGroup)).replaceAll("%epic-nickname%", editText.getText().toString()));
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
            con.setRequestProperty("TRN-Api-Key", "some-api-key");
            InputStream inputStream;
            int responseStatusCode = con.getResponseCode();
            if( responseStatusCode != HttpURLConnection.HTTP_OK ) {
                inputStream = con.getErrorStream();
            } else {
                inputStream = con.getInputStream();
            }
            String data = IOUtils.toString(inputStream, con.getContentEncoding());
            System.out.println(data);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "done";
    }
}

}

您需要先設置用戶代理,然后再設置TRN-Api-Key!

con.setRequestProperty(“ User-Agent”,“ Mozilla / 5.0”);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM