[英]Fortnite Tracker API seems not to be working or I am doing something wrong?
I have run into one problem. 我遇到了一个问题。 I want to get data from fortnite tracker API , I have my own API key from the site and I am quite sure that I am doing everything fine but the site returns me
ErrorInputStream
with this message: 我想从Fortnite Tracker API中获取数据,我从网站上拥有自己的API密钥,并且我可以肯定我做得很好,但是该网站向我返回了
ErrorInputStream
并显示了以下消息:
{"message":"Invalid authentication credentials"}
I am 100% sure that I am giving right credintials to the site. 我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";
}
}
} }
You need to set an User-Agent before you set the TRN-Api-Key! 您需要先设置用户代理,然后再设置TRN-Api-Key!
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty(“ User-Agent”,“ Mozilla / 5.0”);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.