仅 Twitter 应用程序身份验证 - 错误代码 86“此方法需要 GET 或 HEAD”
Twitter Application Only Authentication - Error Code 86 “This method requires a GET or HEAD”
我正在尝试使用仅应用程序不记名令牌获取 https://api.twitter.com/1.1/search/tweets.json?q=nasa&count=5
的推文,我成功地使用了消费者密钥和消费者机密。但我无法获取推文。这是我的代码:
public class SearchTweetsTask extends AsyncTask<String, Void, String> {
private static final String TWITTER_HOST = "api.twitter.com";
private static final String TWITTER_USER_AGENT = "TwitterMotion User Agent";
private Logger logger = Logger.getLogger();
@Override
protected String doInBackground(String... tokens) {
String endPointUrl = tokens[0];
HttpsURLConnection urlConnection = null;
try {
urlConnection = getHTTPSConnection("GET", endPointUrl);
urlConnection.setRequestProperty("Authorization",
"Bearer " + APPLICATION_ONLY_BEARER_TOKEN);
String jsonResponse = readResponse(urlConnection);
return responseAsJsonObject.toString();
}
catch (Exception ex) {
logger.e(ex);
return null;
}
finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
@Override
protected void onPostExecute(String asyncTaskResult) {
logger.i(asyncTaskResult);
}
public static HttpsURLConnection getHTTPSConnection(String requestMethod, String endpointUrl)
throws IOException {
URL url = new URL(endpointUrl);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod(requestMethod);
connection.setRequestProperty("Host", TWITTER_HOST);
connection.setRequestProperty("User-Agent", TWITTER_USER_AGENT);
connection.setUseCaches(false);
return connection;
}
public static String readResponse(HttpsURLConnection connection) throws IOException {
BufferedReader bufferedReader = null;
try {
StringBuilder stringBuilder = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + System.getProperty("line.separator"));
}
return stringBuilder.toString();
}
catch (IOException ex) {
throw ex;
}
finally {
if (bufferedReader != null) {
bufferedReader.close();
}
}
}
}
在 readRequest()
方法中,connection
对象未按预期工作。我检查了 connection.getResponseCode()
,它返回 400
,connection.getErrorStream()
返回 Error Code 86 “This method requires a GET or HEAD”
。
我尝试从命令行使用 cURL,它运行良好。
curl -X GET \
'https://api.twitter.com/1.1/search/tweets.json?q=nasa&result_type=popular' \
-H 'Authorization: Bearer <Application Only Bearer Token>' \
-H 'Cache-Control: no-cache' \
-H 'Host: api.twitter.com' \
-H 'User-Agent: TwitterMotion User Agent'
它变得非常令人沮丧。提前致谢!
如 here 所述,httpCon.setDoOutput(true)
将请求方法隐式设置为 POST
,因为这是您发送请求正文时的默认方法。
如果您想使用 GET
,请删除该行。
我正在尝试使用仅应用程序不记名令牌获取 https://api.twitter.com/1.1/search/tweets.json?q=nasa&count=5
的推文,我成功地使用了消费者密钥和消费者机密。但我无法获取推文。这是我的代码:
public class SearchTweetsTask extends AsyncTask<String, Void, String> {
private static final String TWITTER_HOST = "api.twitter.com";
private static final String TWITTER_USER_AGENT = "TwitterMotion User Agent";
private Logger logger = Logger.getLogger();
@Override
protected String doInBackground(String... tokens) {
String endPointUrl = tokens[0];
HttpsURLConnection urlConnection = null;
try {
urlConnection = getHTTPSConnection("GET", endPointUrl);
urlConnection.setRequestProperty("Authorization",
"Bearer " + APPLICATION_ONLY_BEARER_TOKEN);
String jsonResponse = readResponse(urlConnection);
return responseAsJsonObject.toString();
}
catch (Exception ex) {
logger.e(ex);
return null;
}
finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
@Override
protected void onPostExecute(String asyncTaskResult) {
logger.i(asyncTaskResult);
}
public static HttpsURLConnection getHTTPSConnection(String requestMethod, String endpointUrl)
throws IOException {
URL url = new URL(endpointUrl);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod(requestMethod);
connection.setRequestProperty("Host", TWITTER_HOST);
connection.setRequestProperty("User-Agent", TWITTER_USER_AGENT);
connection.setUseCaches(false);
return connection;
}
public static String readResponse(HttpsURLConnection connection) throws IOException {
BufferedReader bufferedReader = null;
try {
StringBuilder stringBuilder = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + System.getProperty("line.separator"));
}
return stringBuilder.toString();
}
catch (IOException ex) {
throw ex;
}
finally {
if (bufferedReader != null) {
bufferedReader.close();
}
}
}
}
在 readRequest()
方法中,connection
对象未按预期工作。我检查了 connection.getResponseCode()
,它返回 400
,connection.getErrorStream()
返回 Error Code 86 “This method requires a GET or HEAD”
。
我尝试从命令行使用 cURL,它运行良好。
curl -X GET \
'https://api.twitter.com/1.1/search/tweets.json?q=nasa&result_type=popular' \
-H 'Authorization: Bearer <Application Only Bearer Token>' \
-H 'Cache-Control: no-cache' \
-H 'Host: api.twitter.com' \
-H 'User-Agent: TwitterMotion User Agent'
它变得非常令人沮丧。提前致谢!
如 here 所述,httpCon.setDoOutput(true)
将请求方法隐式设置为 POST
,因为这是您发送请求正文时的默认方法。
如果您想使用 GET
,请删除该行。