簡體   English   中英

Twitter API使用僅應用程序身份驗證獲取Tweet返回HTTP狀態代碼401

[英]Twitter API to get Tweets using Application Only Authentication returns HTTP Status Code 401

我正在試驗使用Java的Twitter API。 我正在嘗試使用Twitter文檔的僅應用程序身份驗證來訪問API Twitter僅應用程序身份驗證

這是我的代碼。 我也曾嘗試不對Base64編碼access_token進行編碼,如該問題所示

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);
        }

    }

}

您的代碼有點笨拙,但是,如果您看一下這一行;

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

您正在調用oaat對象的toString()方法(我懷疑您已經實現了該方法)。 僅需要access_token值本身。 這就是為什么您要獲得與文檔中指定的無效令牌相對應的代碼401原因。 因此,將其更改為oaat.getAccessToken()使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM