1

In my project I am trying to integrate twitter using twitter4j.I tried sample code to login in office and I could post tweet successfully using that,but coming back to home I am facing bizarre problem in JCE.Jar which says

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>

Code to login into twitter is somewhat like like this.

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()
  }
4

1 に答える 1

1

Though I am not very sure why this problem occurred but just attempted version upgrading to Java 7 and it worked!

于 2012-09-12T13:02:16.523 に答える