gpt4 book ai didi

java - 使用 oauth_token 和 oauth_verifier 转换 Twitter 的请求 token 时收到过期的 access_token

转载 作者:行者123 更新时间:2023-12-01 10:27:24 32 4
gpt4 key购买 nike

我正在尝试在 Twitter 上使用 3 条腿的标志,因为我的步骤如下:

  1. 创建应用
  2. 获取 API 和 secret
  3. 获取请求 token
  4. 获取访问 token
  5. 如果需要,请获取个人资料

我能够获取请求 token :

    private String getRequestToken() throws Exception {
String url = "https://api.twitter.com/oauth/request_token";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
String get_or_post = "POST";
String oauth_signature_method = "HMAC-SHA1";
String uuid_string = UUID.randomUUID().toString();
uuid_string = uuid_string.replaceAll("-", "");
String oauth_nonce = uuid_string;
Calendar tempcal = Calendar.getInstance();
long ts = tempcal.getTimeInMillis();// get current time in milliseconds
String oauth_timestamp = (new Long(ts / 1000)).toString();
String parameter_string = "oauth_consumer_key=" + twitter_consumer_key + "&oauth_nonce=" + oauth_nonce
+ "&oauth_signature_method=" + oauth_signature_method + "&oauth_timestamp=" + oauth_timestamp
+ "&oauth_version=1.0";
System.out.println("parameter_string=" + parameter_string);
String twitter_endpoint = "https://api.twitter.com/oauth/request_token";
String signature_base_string = get_or_post + "&" + encode(twitter_endpoint) + "&" + encode(parameter_string);
String oauth_signature = "";
try {
oauth_signature = computeSignature(signature_base_string, twitter_consumer_secret + "&");
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String authorization_header_string = "OAuth oauth_consumer_key=\"" + twitter_consumer_key
+ "\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" + oauth_timestamp + "\",oauth_nonce=\""
+ oauth_nonce + "\",oauth_version=\"1.0\",oauth_signature=\"" + encode(oauth_signature) + "\"";
System.out.println("authorization_header_string=" + authorization_header_string);

// add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Authorization", authorization_header_string);
System.out.println("---------------------------------------");
System.out.println(authorization_header_string);
System.out.println("---------------------------------------");
// Send post request
con.setDoOutput(true);

int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

// print result
System.out.println(response.toString());
String[] twitterResponse = response.toString().split("&");
oauth_token = twitterResponse[0].substring(twitterResponse[0].indexOf("=") + 1);
oauth_token_secret = twitterResponse[1].substring(twitterResponse[1].indexOf("=") + 1);
oauth_callback_confirmed = twitterResponse[2].substring(twitterResponse[2].indexOf("=") + 1);
return response.toString();
}

它的 react 很好;第五步是提供额外信息,即 x_auth_expires=0 这意味着收到的 token 已过期。因此我的下一个无法正常工作这是我获取 access_token 的代码

    public String getAccessToken(HttpServletRequest req) throws Exception {
String url = "https://api.twitter.com/oauth/access_token";
String oauthToken = (String) req.getParameter("oauth_token");
String oauth_verifier = (String) req.getParameter("oauth_verifier");
System.out.println("oauthToken----------"+oauthToken);
System.out.println("oauth_verifier-----------"+oauth_verifier);
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
String get_or_post = "POST";
String oauth_signature_method = "HMAC-SHA1";
String uuid_string = UUID.randomUUID().toString();
uuid_string = uuid_string.replaceAll("-", "");
String oauth_nonce = uuid_string;
Calendar tempcal = Calendar.getInstance();
long ts = tempcal.getTimeInMillis();
String oauth_timestamp = (new Long(ts / 1000)).toString();
String parameter_string = "oauth_consumer_key=" + twitter_consumer_key + "&oauth_nonce=" + oauth_nonce
+ "&oauth_signature_method=" + oauth_signature_method + "&oauth_timestamp=" + oauth_timestamp
+ "&oauth_token=" + oauthToken + "&oauth_version=1.0";
System.out.println("parameter_string=" + parameter_string);
String twitter_endpoint = "https://api.twitter.com/oauth/request_token";
String signature_base_string = get_or_post + "&" + encode(twitter_endpoint) + "&" + encode(parameter_string);
String oauth_signature = "";
try {
oauth_signature = computeSignature(signature_base_string, twitter_consumer_secret + "&");
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String authorization_header_string = "OAuth oauth_consumer_key=\"" + twitter_consumer_key
+ "\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" + oauth_timestamp + "\",oauth_nonce=\""
+ oauth_nonce + "\",oauth_token=\"" + oauthToken + "\",oauth_version=\"1.0\",oauth_signature=\""
+ encode(oauth_signature) + "\"";
System.out.println("authorization_header_string=" + authorization_header_string);
String urlParameters = "oauth_verifier=" + oauth_verifier;

// add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Authorization", authorization_header_string);
System.out.println("---------------------------------------");
System.out.println(authorization_header_string);
System.out.println("---------------------------------------");
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();

int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

// print result
System.out.println(response.toString());
String[] responseOauthToken=response.toString().split("&");
String oauth_token=responseOauthToken[0].substring(responseOauthToken[0].indexOf("=")+1);
String screenname=responseOauthToken[3].substring(responseOauthToken[3].indexOf("=")+1);
System.out.println("oauth_token---------"+oauth_token);
System.out.println("screenname---------"+screenname);
getProfile(oauth_token,screenname);
return "";
}

验证配置文件的代码:

    public void getProfile(String oauthToken,String screenname) throws Exception {
String url = "https://api.twitter.com/1.1/account/verify_credentials.json";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
String get_or_post = "GET";
String oauth_signature_method = "HMAC-SHA1";
String uuid_string = UUID.randomUUID().toString();
uuid_string = uuid_string.replaceAll("-", "");
String oauth_nonce = uuid_string;

Calendar tempcal = Calendar.getInstance();
long ts = tempcal.getTimeInMillis();
String oauth_timestamp = (new Long(ts / 1000)).toString();
String parameter_string = "oauth_consumer_key=" + twitter_consumer_key + "&oauth_nonce=" + oauth_nonce
+ "&oauth_signature_method=" + oauth_signature_method + "&oauth_timestamp=" + oauth_timestamp
+"&screen_name="+screenname+"&oauth_token="+oauthToken
+ "&oauth_version=1.0";
System.out.println("parameter_string=" + parameter_string);
String twitter_endpoint = "https://api.twitter.com/1.1/account/verify_credentials.json";
String signature_base_string = get_or_post + "&" + encode(twitter_endpoint) + "&" + encode(parameter_string);
String oauth_signature = "";
try {
oauth_signature = computeSignature(signature_base_string, twitter_consumer_secret + "&");
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String authorization_header_string = "OAuth oauth_consumer_key=\"" + twitter_consumer_key
+ "\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"" + oauth_timestamp + "\",oauth_nonce=\""
+ oauth_nonce +"\",oauth_token=\""+oauthToken+ "\",oauth_version=\"1.0\",oauth_signature=\"" + encode(oauth_signature) + "\"";
System.out.println("authorization_header_string=" + authorization_header_string);

// add reuqest header
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestProperty("Authorization", authorization_header_string);
System.out.println("---------------------------------------");
System.out.println(authorization_header_string);
System.out.println("---------------------------------------");
// Send post request
con.setDoOutput(true);

int responseCode = con.getResponseCode();
System.out.println("\nSending '"+get_or_post+"' request to URL : " + url);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

// print result
System.out.println(response.toString());

}

最佳答案

x_auth_expires为0时,这意味着您的访问 token 没有过期,所以我不认为这是您的问题。

您的问题(希望是唯一的问题)似乎出现在 getProfile() 中的这一行:

oauth_signature = computeSignature(signature_base_string, twitter_consumer_secret + "&");

您应该使用消费者 secret 加上访问 token secret (通过“&”连接)进行签名,但您仅使用消费者 secret 。当您接收并提取访问 token 时,您似乎甚至没有提取oauth_token_secret(访问oauth_token_secret与请求oauth_token_secret不同) >).

我还注意到,您当前正在按索引从响应中提取 oauth_tokenscreen_name。虽然属性的顺序看起来是一致的,但按名称获取它们是最安全的。

实际上我确实看到了另一个问题。在 getProfile() 中,screen_name 不应包含在参数字符串中,因为它不是参数,如果是参数,则需要按字典顺序排列。

关于java - 使用 oauth_token 和 oauth_verifier 转换 Twitter 的请求 token 时收到过期的 access_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35291677/

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