【问题标题】:Twitter API to get Tweets using Application Only Authentication returns HTTP Status Code 401使用仅应用程序身份验证获取推文的 Twitter API 返回 HTTP 状态代码 401
【发布时间】:2022-01-22 20:32:20
【问题描述】:

我正在使用 Java 试验 Twitter API。我正在尝试使用 twitter 文档Twitter Application-only authentication

使用仅应用程序身份验证来访问 API

这是我的代码。我也尝试过不要将 access_token Base64 编码为suggested in this question

public class TwitterAppOnlyAuthenticationClass {
    private static final String consumerKey = "My Consumer Key";
    private static final String consumerSecret  = "My Consumer Secret";

    private static OAuthAccessToken getAccessToken() throws UnsupportedEncodingException, IOException {

        // Encode Consumer Key and Secret

        String encodedConsumerKeyandToken = null;

        encodedConsumerKeyandToken = Base64.getEncoder().encodeToString(
                (URLEncoder.encode(consumerKey, "UTF-8") + ":" +
                        URLEncoder.encode(consumerSecret, "UTF-8")).getBytes());


        //Get the bearer token

        String urlString = "https://api.twitter.com/oauth2/token";
        String httpMethod = "POST";

        OAuthAccessToken oaat = null;

        URL url;

        url = new URL (urlString);
        HttpsURLConnection u = (HttpsURLConnection)url.openConnection();



        u.setDoOutput(true);

        u.setRequestMethod(httpMethod);
        u.setRequestProperty("Authorization", "Basic " + encodedConsumerKeyandToken);
        u.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        OutputStream dos = u.getOutputStream();
        dos.write("grant_type=client_credentials".getBytes());
        dos.flush();
        dos.close();

        u.connect();

        JsonObject jO = (JsonObject)((Json.createReader(u.getInputStream())).read());

        System.out.println(jO.toString());

        oaat = new OAuthAccessToken();
        oaat.setTokenType(jO.getString("token_type"));
        oaat.setAccessToken(jO.getString("access_token"));

        return oaat;


    }


    public static void main(String[] args) {

        //Basic Java program to retrieve a collection of the most recent Tweets posted by the user


        try {

            OAuthAccessToken oaat = getAccessToken();
            System.out.println("Access Token : " + oaat.getAccessToken());

            //Make the call to {GET statuses/user_timeline}

            String urlString = "https://api.twitter.com/1.1/statuses/user_timeline.json?count=100&screen_name=twitterapi";
            String httpMethod = "GET";

            URL url;

            url = new URL (urlString);
            HttpsURLConnection u = (HttpsURLConnection)url.openConnection();

            u.setDoOutput(true);
            u.setRequestMethod(httpMethod);
            u.setRequestProperty("Authorization", "Bearer " + oaat);

            u.connect();

            JsonObject jO = (JsonObject)((Json.createReader(u.getInputStream())).read());
            System.out.println(jO.toString());


        } catch (IOException ioe) {
            ioe.printStackTrace();
            System.exit(-1);
        }

    }

}

【问题讨论】:

    标签: java twitter


    【解决方案1】:

    如果你看这条线;

    u.setRequestProperty("Authorization", "Bearer " + oaat);

    您正在调用 oaat 对象的 toString() 方法(我怀疑您是否已经实现)。只需要 access_token 值本身。这就是为什么您会收到与文档中指定的无效令牌相对应的代码 401。因此,将其更改为 oaat.getAccessToken() 即可。

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 2013-03-08
      • 1970-01-01
      • 2017-12-18
      • 2013-04-29
      • 1970-01-01
      • 2018-12-26
      • 1970-01-01
      • 2015-06-10
      相关资源
      最近更新 更多