1

I'm trying to use Twitter's friends list api and was successful to do so without any parameters.

However whenever I add a parameter, I would get the error "Could not authenticate you." and I have no choice but to add a cursor parameter when the friend list is too long.

The fact that I get a list of users of friends when I call the api without any parameters makes me think that authenticating the request works properly.

I have tried to change the request url to https://api.twitter.com/1.1/friends/list.json?cursor=-1 which gives me the authentication error.

I tried using both https://api.twitter.com/1.1/friends/list.json and https://api.twitter.com/1.1/friends/list.json?cursor=-1 to make oauth_signature and they both failed me.

I tried using different parameters such as screen_name or user_id and they all will give me the same error.

I even tried to add cursor: -1 header like a POST request and that didn't work either.

Right now my code looks like this

public String getFriendList() {
    String baseUrl = "https://api.twitter.com/1.1/friends/list.json";

    // Creates a map with all necessary headers
    Map<String, String> headers = createMap(); 
    headers.put("oauth_token", <OAuth token of user>);
    String signature = createSignature("GET", baseUrl, headers, <OAuth secret of user>);

    // Add oauth_signature to header
    headers.put("oauth_signature", signature);

    String body = sendGetRequest(baseUrl, headers);
    return body;
}

public String sendGetRequest(String baseUrl, Map<String, String> parameters) throws AuthException, IOException {
    try (CloseableHttpClient client = CloseableHttpClientFactory.getHttpClient()) {
        HttpGet httpGet = new HttpGet(baseUrl);

        if (parameters != null) {
            httpGet.setHeader("Authorization", createHeader(parameters));
        }

        CloseableHttpResponse response = client.execute(httpGet);

        if (response.getStatusLine().getStatusCode() != 200) {
            LOGGER.info("GET Request Failed : " + EntityUtils.toString(response.getEntity()));
            throw new Exception();
        }

        String responseBody = EntityUtils.toString(response.getEntity());
        return responseBody;
    }
}

which is the working code.

Could anyone tell me where to add parameters and what I have missed to authenticate the request?

EDIT : Added code of sendGetRequest. Making the signature and adding the header was made by following the documentations from twitter

  • 1
    Are you using a library to create the authorization header? If not, you'll need to manually create this and not the parameters must be sorted in order https://developer.twitter.com/en/docs/basics/authentication/guides/creating-a-signature – osowskit Jun 17 '19 at 14:11
  • I'm creating the authorization header inside the method `sendGetRequest`. Also I'm using a treemap for the parameter headers so that it is sorted. – Felixfelicis Jun 18 '19 at 01:00
  • Does this help? https://stackoverflow.com/a/59765764/1776132 – Smile Jan 17 '20 at 02:37

0 Answers0