代码之家  ›  专栏  ›  技术社区  ›  Kaidul

仅限Twitter应用程序身份验证-错误代码86此方法需要GET或HEAD

  •  0
  • Kaidul  · 技术社区  · 6 年前

    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'
    

    它变得非常令人沮丧。提前谢谢!

    1 回复  |  直到 6 年前
        1
  •  1
  •   Kaidul    6 年前

    here ,的 httpCon.setDoOutput(true) 隐式地将请求方法设置为 POST 因为当你想发送一个请求主体时,这是默认的方法。

    如果你想用 GET ,删除该行。