Spring Social Twitter Oauth

I want to use spring social to develop a Twitter application that will update the status and upload photos. I can't figure out how to do Oauth authentication using spring social. All the examples I saw talk about hardcoding accesstoken which will work only for this particular user. I do not want to make hard codes other than application keys.

Please explain to me how to make Twitter Oauth using spring social.I went through the official documentation of the spring framework, but got confused when I saw other examples.

thanks

+6
source share
2 answers
  • I have seen talk about the hardcoded accesstoken, which will only work for this particular user. I do not want to make hard codes other than application keys.

"application keys" aka consumer { key, secret } pair allows your application to use the Twitter API, which does not require user authentication. Think about it when you browse the twitter website without logging in. Consequently, you will have the opportunity to seek, receive dates, etc. => Read only.

If you want to post something back, you will need to have the application do it on behalf of a real Twitter account / user. Think about someone writing a Twitter client => it can be downloaded by many different users, so it needs two functions to work properly:

  • Register Twitter app => have consumer { key, secret } pair
  • Ability to send tweets / images as a user => have access { token, secret } pair

To get this access pair (token, secret), you will have to have “OK” from this user / account.

This is where OAuth comes in => it sends the user to the confirmation page, where he clicks "OK, I allow this application to send messages on my behalf." Then this “OK” is converted to an OAuthToken that your application can use.

If you want to post updates on your behalf, you need to approve your own Twitter application and keep the OAuthToken be used by your application.

Unfortunately, it does not yet support OAuth 2.0, so you will have to do more ... You will need to do OAuth 1.0a.

Spring Social documentation describes the OAuth 1.0a stream here , where you can see the stream visually.

To "encode" this stream using the Spring social APIs, you must first request an access pair (token, value) (there is a convenient ConnectController for this btw):

 TwitterConnectionFactory connectionFactory = new TwitterConnectionFactory( "consumerKey", "consumerSecret" ); OAuth1Operations oauthOperations = connectionFactory.getOAuthOperations(); OAuthToken requestToken = oauthOperations.fetchRequestToken( "https://my-callback-url", null ); String authorizeUrl = oauthOperations.buildAuthorizeUrl( requestToken, OAuth1Parameters.NONE ); response.sendRedirect( authorizeUrl ); 

And as soon as it returns (to the callback URL), you can use OAuth1Operations to get the OAuthToken , which is exactly that pair.

 // upon receiving the callback from the provider: OAuthToken accessToken = oauthOperations.exchangeForAccessToken( new AuthorizedRequestToken(requestToken, oauthVerifier), null); 

Now that you have everything you need, you have a choice:

Create a TwitterTemplate from this OAuthToken :

 String consumerKey = "..."; // The application consumer key String consumerSecret = "..."; // The application consumer secret String accessToken = accessToken.getValue(); String accessTokenSecret = accessToken.getSecret(); Twitter twitter = new TwitterTemplate( consumerKey, consumerSecret, accessToken, accessTokenSecret ); 

Create Twitter Connection Object

 Connection<Twitter> connection = connectionFactory.createConnection( accessToken ); 

Once you get Connection , you may need to save it through ConnectionRepository as shown here , so you do not need to access the token again.

Here is the connection API .

+11
source

The previous answer is good, but this is only part of the story ...

There are at least 3 levels at which you can work with Spring Social: (1) use TwitterTemplate directly, in which case you will need to get the access token and secret through your own means (2) use OAuth1Template, possibly through TwitterConnectionFactory, as showed the previous answer in order to get the access token and create a TwitterTemplate from it, in which case you will have to handle the forwarding and callbacks yourself or (3) use the Spring Social ConnectController to handle everything for you.

Using ConnectController includes the least amount of OAuth operations on your part. You just configure the appropriate fragments in Spring, and ConnectController takes care of the rest. See http://static.springsource.org/spring-social/docs/1.0.x/reference/html/connecting.html for details.

I recommend you check out the Spring Social Showcase sample https://github.com/SpringSource/spring-social-samples . It uses ConnectController to process via Twitter and connect to Facebook. And, of course, you can ask questions on the Spring social forum at http://forum.springsource.org/forumdisplay.php?82-Social .

+3
source

Source: https://habr.com/ru/post/900528/


All Articles