gpt4 book ai didi

java - Fortnite Tracker API 似乎无法正常工作或者我做错了什么?

转载 作者:太空宇宙 更新时间:2023-11-04 10:12:15 25 4
gpt4 key购买 nike

我遇到了一个问题。我想从 fortnite tracker API 获取数据,我从该网站获得了自己的 API key ,并且我非常确定我做的一切都很好,但该网站向我返回了 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");

关于java - Fortnite Tracker API 似乎无法正常工作或者我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52167710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com