简体   繁体   中英

Error 86 This method requires a GET or HEAD using Bearer token on Twitter Rest API 1.1 using Java in AppEngine

I have my bearerToken and userID as per Twitter instructions https://dev.twitter.com/docs/auth/application-only-auth and I want t get a list of followers.

I'm getting error 86, which isn't on the list of error codes https://dev.twitter.com/docs/error-codes-responses

Any pointers would be appreciated.

public String getTwitterFriends(String userID, String bearerToken) {
    // Use App Bearer token to get public friends
    String answer = "";
    String param = "count=5000&cursor=-1&user_id=" + userID;
    HttpURLConnection connection = null;
    try {
        // String request =
        // "https://api.twitter.com:443/1.1/friends/ids.json?" + param;
        String request = "https://api.twitter.com/1.1/friends/ids.json?"
                + param;
        URL url = new URL(request);
        connection = (HttpURLConnection) url.openConnection();
        System.setProperty("http.keepAlive", false ? "true" : "false");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("GET");
        // connection.setRequestProperty("Host", "api.twitter.com" +
        // ":443");
        connection.setRequestProperty("Host", "api.twitter.com");
        connection.setRequestProperty("Accept", "*/*");
        connection.setRequestProperty("Authorization", "Bearer "
                + bearerToken);
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded;charset=utf-8");
        connection.setRequestProperty("User-Agent", "UnhappyChappy");
        // connection.setRequestProperty("Accept-Encoding", "gzip");
        // connection.setRequestProperty("Content-Length", "" +
        // Integer.toString(param.getBytes().length));
        connection.setUseCaches(false);
        DataOutputStream wr = new DataOutputStream(
                connection.getOutputStream());
        // wr.writeBytes(param);
        wr.flush();
        wr.close();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String line;
        StringBuilder str = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
            str.append(line);
        }
        reader.close();
        connection.disconnect();
        answer = str.toString();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println(answer);
    return answer;
}   

It was the way I issued the GET. I had to go to a lower level on App Engine and use FetchOptions This worked for me, hopefully it will help someone else.

        URL url = new URL(request);

        HTTPRequest req = new HTTPRequest(url, HTTPMethod.GET);
        req.addHeader(new HTTPHeader("Authorization", "Bearer " + bearerToken));

        HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(req);
        System.out.println(new String(response.getContent()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM