我遇到了一个问题。我想从堡垒追踪器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";
}
}
}
答案 0 :(得分:0)
在设置TRN-Api-Key之前,您需要设置一个User-Agent!
con.setRequestProperty(“ User-Agent”,“ Mozilla / 5.0”);