gpt4 book ai didi

java - 使用仅应用程序身份验证获取推文的 Twitter API 返回 HTTP 状态代码 401

转载 作者:行者123 更新时间:2023-12-02 03:00:47 25 4
gpt4 key购买 nike

我正在使用 Java 尝试 Twitter API。我正在尝试使用 Twitter 文档 Twitter Application-only authentication 仅使用应用程序身份验证来访问 API

这是我的代码。我也尝试过不将 access_token 编码为 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);
}

}

}

最佳答案

如果你看这一行;

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

您正在调用toString()你的方法oaat对象(我怀疑你已经实现了)。只有access_token值(value)本身是需要的。这就是为什么您会收到代码 401对应于文档中指定的无效 token 。因此,将其更改为 oaat.getAccessToken()它会起作用的。

关于java - 使用仅应用程序身份验证获取推文的 Twitter API 返回 HTTP 状态代码 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42403454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com