Twitter oAuth错误32无法使用应用程序引擎上的Java对您进行身份验证
在Google App Engine中使用Java中的Twitter API,我正在尝试验证凭据以获取id_str,出现错误32-无法对您进行身份验证 我已经完成并获得了accessToken和secret OK 如果我用oauth工具中的示例覆盖oauth_nonce和oauth_时间戳,那么我的签名基字符串和授权头与示例匹配,所以我想我的签名部分是可以的 我想知道这是否就是我从Java发布GET的方式?我一直在试验,但有点卡住了。我试图更好地理解oAuth是如何工作的,这就是为什么我没有使用库的原因。任何帮助或指点都将不胜感激Twitter oAuth错误32无法使用应用程序引擎上的Java对您进行身份验证,java,google-app-engine,twitter,oauth,Java,Google App Engine,Twitter,Oauth,在Google App Engine中使用Java中的Twitter API,我正在尝试验证凭据以获取id_str,出现错误32-无法对您进行身份验证 我已经完成并获得了accessToken和secret OK 如果我用oauth工具中的示例覆盖oauth_nonce和oauth_时间戳,那么我的签名基字符串和授权头与示例匹配,所以我想我的签名部分是可以的 我想知道这是否就是我从Java发布GET的方式?我一直在试验,但有点卡住了。我试图更好地理解oAuth是如何工作的,这就是为什么我没有使用
public String getTwitterID(String oauth_token, String oauth_token_secret){
String answer = "";
String oauth_signature_method = "HMAC-SHA1";
String oauth_consumer_key = apiKey;
String uuid_string = UUID.randomUUID().toString();
uuid_string = uuid_string.replaceAll("-", "");
String oauth_nonce = uuid_string; //random alphanumeric string
//oauth_nonce = "e1c55102427763bf3570093497b445ff";
String oauth_timestamp = (new Long(System.currentTimeMillis() / 1000))
.toString();
//The parameter string must be in alphabetical order
//oauth_timestamp = "1397181466";
String parameter_string = "oauth_consumer_key=" + oauth_consumer_key
+ "&oauth_nonce=" + oauth_nonce + "&oauth_signature_method="
+ oauth_signature_method + "&oauth_timestamp="
+ oauth_timestamp
+ "&oauth_token=" + oauth_token
+ "&oauth_version=1.0";
System.out.println("parameter_string=" + parameter_string);
String signature_base_string = "";
try {
signature_base_string = "GET&https%3A%2F%2Fapi.twitter.com%2F1.1%2Faccount%2Fverify_credentials.json&" + URLEncoder.encode(parameter_string, "UTF-8");
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
System.out.println("signature_base_string=" + signature_base_string);
String oauth_signature = "";
try {
oauth_signature = computeSignature(signature_base_string,
apiSecret + "&" + oauth_token_secret);
System.out.println("oauth_signature=" + oauth_signature);
} catch (GeneralSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oauth_signature = URLEncoder.encode(oauth_signature, "UTF-8");
System.out.println("encoded oauth_signature=" + oauth_signature);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// The parameter list must be in alphabetical order
String oauth_header = "OAuth oauth_consumer_key=\"";
oauth_header += oauth_consumer_key;
oauth_header += "\", oauth_nonce=\"";
oauth_header += oauth_nonce;
oauth_header += "\", oauth_signature=\"";
oauth_header += oauth_signature;
oauth_header += "\", oauth_signature_method=\"";
oauth_header += "HMAC-SHA1";
oauth_header += "\", oauth_timestamp=\"";
oauth_header += oauth_timestamp;
oauth_header += "\", oauth_token=\"";
oauth_header += oauth_token;
oauth_header += "\", oauth_version=\"";
oauth_header += "1.0\"";
System.out.println("oauth_header=" + oauth_header);
try {
String request = "https://api.twitter.com/1.1/account/verify_credentials.json";
//String request = "https://api.twitter.com/1.1/account/verify_credentials.json?" + parameter_string;
URL url = new URL(request);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
//connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("GET");
//connection.setRequestMethod("POST");
//connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Accept", "*/*");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Authorization", oauth_header);
connection.setRequestProperty("Content-Length", "0");
//connection.setRequestProperty("Content-Length", "" + Integer.toString(oauth_body.getBytes().length));
connection.setUseCaches (false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
//wr.writeBytes(oauth_body);
wr.flush();
wr.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder str = new StringBuilder();
while ((line = reader.readLine()) != null) {
System.out.println(line);
str.append(line);
}
reader.close();
connection.disconnect();
LOG.log(Level.WARNING, "Twitter ID JSON - " + str.toString());
try {
JSONObject jsonObject;
jsonObject = new JSONObject(answer);
answer = jsonObject.getString("id_str");
} catch (JSONException e) {
e.printStackTrace();
answer = "";
}
} catch (MalformedURLException e) {
// ...
} catch (IOException e) {
// ...
}
System.out.println(answer);
return answer;
}
我遇到了同样的问题,所以我寻找了一个可以为我提供Twitter OAuth的库。我发现了Twitter4j,并在这方面取得了很多成功 Twitter4J可从以下网址获得: 如果出于某种原因你不能使用Twitter4J,也许你可以看看它的源代码,看看他们是如何在你的部分实现OAuth的