在我的项目中,我正在尝试使用twitter4j集成 twitter 。我尝试使用示例代码登录办公室,我可以使用它成功发布推文,但是回到家我在 JCE.Jar 中遇到了奇怪的问题,上面写着
Failed to get HmacSHA1 "Message Authentication Code" (MAC)
java.security.NoSuchAlgorithmException: Algorithm HmacSHA1 not available
at javax.crypto.Mac.getInstance(DashoA13*..)
at twitter4j.auth.OAuthAuthorization.generateSignature(OAuthAuthorization.java:317)
at twitter4j.auth.OAuthAuthorization.generateAuthorizationHeader(OAuthAuthorization.java:227)
at twitter4j.auth.OAuthAuthorization.generateAuthorizationHeader(OAuthAuthorization.java:273)
at twitter4j.auth.OAuthAuthorization.getAuthorizationHeader(OAuthAuthorization.java:75)
at twitter4j.internal.http.HttpClientImpl.setHeaders(HttpClientImpl.java:237)
at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:113)
at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102)
at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:121)
at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:104)
at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:276)
at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:269)
at org.twitterstats.App.main(App.java:23)
Exception in thread "main" java.lang.AssertionError: java.security.NoSuchAlgorithmException: Algorithm HmacSHA1 not available
at twitter4j.auth.OAuthAuthorization.generateSignature(OAuthAuthorization.java:337)
at twitter4j.auth.OAuthAuthorization.generateAuthorizationHeader(OAuthAuthorization.java:227)
at twitter4j.auth.OAuthAuthorization.generateAuthorizationHeader(OAuthAuthorization.java:273)
at twitter4j.auth.OAuthAuthorization.getAuthorizationHeader(OAuthAuthorization.java:75)
at twitter4j.internal.http.HttpClientImpl.setHeaders(HttpClientImpl.java:237)
at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:113)
at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:102)
at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:121)
at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:104)
at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:276)
at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:269)
at org.twitterstats.App.main(App.java:23)
Caused by: java.security.NoSuchAlgorithmException: Algorithm HmacSHA1 not available
at javax.crypto.Mac.getInstance(DashoA13*..)
at twitter4j.auth.OAuthAuthorization.generateSignature(OAuthAuthorization.java:317)
... 12 more
<dependencies>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>[2.2,)</version>
</dependency>
登录 twitter 的代码有点像这样。
public static void main(String args[]) thrwos Exception{
// The factory instance is re-useable and thread safe.
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer("[consumer key]", "[consumer secret]");
RequestToken requestToken = twitter.getOAuthRequestToken();
AccessToken accessToken = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (null == accessToken) {
System.out.println("Open the following URL and grant access to your account:");
System.out.println(requestToken.getAuthorizationURL());
System.out.print("Enter the PIN(if aviailable) or just hit enter.[PIN]:");
String pin = br.readLine();
try{
if(pin.length() > 0){
accessToken = twitter.getOAuthAccessToken(requestToken, pin);
}else{
accessToken = twitter.getOAuthAccessToken();
}
} catch (TwitterException te) {
if(401 == te.getStatusCode()){
System.out.println("Unable to get the access token.");
}else{
te.printStackTrace();
}
}
}
//persist to the accessToken for future reference.
storeAccessToken(twitter.verifyCredentials().getId() , accessToken);
Status status = twitter.updateStatus(args[0]);
System.out.println("Successfully updated the status to [" + status.getText() + "].");
System.exit(0);
}
private static void storeAccessToken(int useId, AccessToken accessToken){
//store accessToken.getToken()
//store accessToken.getTokenSecret()
}