Spring 社交 Twitter Oauth

发布于 2024-12-13 06:33:16 字数 274 浏览 4 评论 0原文

我想使用 Spring Social 开发一个 Twitter 应用程序,它将更新状态并上传照片。我无法理解如何使用 Spring Social 进行 Oauth 身份验证。我看到的所有示例都讨论了对 accesstoken 进行硬编码,该 accesstoken 仅适用于特定的情况用户。我不想对除应用程序密钥之外的任何内容进行硬编码。

请有人解释一下如何使用 Spring Social 进行 Twitter Oauth。我浏览了 Spring 框架的官方文档,但当我看到其他示例时感到困惑。

谢谢

I want to use spring social to develop an twitter app which will update status and upload photos.I am not able to understand how to do Oauth authentication using Spring social.All examples I saw talks about hardcoding the accesstoken which would work only for that particular user.I dont want to hardcode anything except the app keys.

Kindly some one explain me how to do Twitter Oauth using spring social.I went through the official documentation of spring framework but got confused when I saw the other examples..

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归属感 2024-12-20 06:33:16
  • 我看到有关对访问令牌进行硬编码的讨论,该访问令牌仅适用于该特定用户。我不想对除应用程序密钥之外的任何内容进行硬编码。

“应用程序密钥”又名消费者{密钥,秘密} 对授权您的应用使用不需要用户身份验证的 Twitter API。想象一下,当您在未登录的情况下浏览 Twitter 网站时,您的应用程序将具有搜索、获取时间表等的能力。 =>只读。

如果您想发布某些内容,则必须让您的应用代表真实的 Twitter 帐户/用户执行此操作。。想想有人写 Twitter 客户端=>它可以被许多不同用户下载,因此它需要两件事才能正常运行:

  • 是一个注册的 Twitter 应用程序 =>拥有 consumer { key, Secret }
  • 对 能够代表用户发布推文/图像 =>拥有 access { token, Secret }

对 为了获得该 access { token, Secret } 对,您必须获得该用户/帐户的“OK”。

这就是 OAuth 的用武之地 =>它将用户发送到确认页面,他在其中单击“确定,我允许此应用代表我发布”。然后,此“OK”将转换为您的应用可以使用的 OAuthToken

如果您只想代表自己发布更新,那么您需要批准自己的 Twitter 应用程序,并保留该 OAuthToken 供您的应用程序使用。

不幸的是 Twitter 尚不支持 OAuth 2.0,因此您必须做更多...您必须做OAuth 1.0a。

Spring Social 文档描述了 OAuth 1.0a 流程此处,您可以在其中直观地看到流程。

为了使用 Spring Social API“编码”此流程,您应该首先请求访问 {token, value} 对(有一个方便的 ConnectController 顺便说一句):

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 );

一旦它返回(到您的回调 URL),您可以使用 OAuth1Operations 来获取 OAuthToken 正是这一对。

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

现在,您已拥有所需的一切,您可以选择:

OAuthToken 创建 TwitterTemplate:

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

创建 Twitter Connection 对象

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

获得 Connection 后,您可能想要保留它通过 ConnectionRepository 作为 显示此处,这样您就不必再次获取访问令牌。

这是连接API

  • I saw talks about hardcoding the accesstoken which would work only for that particular user.I dont want to hardcode anything except the app keys.

"app keys" a.k.a. consumer { key, secret } pair authorizes your app to use Twitter APIs that do not require user authentication. Think about it as you app browsing a twitter website without being logged in. Hence you'd have an ability to search, get timelines, etc.. => read only.

In case you'd like to post something back, you'd have to make you app do that on behalf of a real Twitter account / user. Think about someone writing a Twitter client => it can be downloaded by many different users, hence it needs two things to function properly:

  • Be a registered Twitter application => have consumer { key, secret } pair
  • Be able to post tweets / images on behalf of the user => have access { token, secret } pair

In order to get that access { token, secret } pair, you'd have to have an "OK" from that user/account.

That is where OAuth comes in => it sends the user to the confirmation page, where he clicks "OK, I allow this app to post on my behalf". This "OK" then gets converted to the OAuthToken that your app can use.

If all you want is to post updates on behalf of yourself, then you need to approve your own Twitter app, and persist that OAuthToken to be used by your app.

Unfortunately Twitter does not yet support OAuth 2.0, hence you'd have to do more... You'd have to do OAuth 1.0a.

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

On order to "code" this flow using Spring Social APIs, you should first request access {token, value} pair ( there is a convenience ConnectController for it 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 once it comes back (to your callback URL) you can use OAuth1Operations to get OAuthToken which is exactly that pair.

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

Now, as you have all you need, you have choices:

Create a TwitterTemplate from that OAuthToken:

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

Create a Twitter Connection object

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

Once you get the Connection, you might want to persist it via ConnectionRepository as shown here, so you don't have to go through obtaining access token again.

Here is Connection API.

草莓酥 2024-12-20 06:33:16

前面的答案很好,但只是故事的一部分......

您可以在至少3个级别上使用Spring Social:(1)直接使用TwitterTemplate,在这种情况下您需要获得访问权限通过您自己的某种方式获取令牌和秘密,(2) 使用 OAuth1Template,也许通过 TwitterConnectionFactory,如前面的答案所示,获取访问令牌并从中创建 TwitterTemplate,在这种情况下,您必须处理重定向和回调你自己或(3)使用Spring Social的ConnectController为你处理一切。

使用 ConnectController 涉及的 OAuth 工作量最少。您只需在 Spring 中配置适当的部分,ConnectController 就会处理其余的事情。请参阅http://static.springsource.org/ spring-social/docs/1.0.x/reference/html/connecting.html 了解详细信息。

我鼓励您查看 Spring Social Showcase 示例,网址为 https://github.com/SpringSource /spring-social-samples。它使用 ConnectController 来处理 Twitter 和 Facebook 连接。当然,欢迎您在 Spring Social 论坛上提问,网址为 http:// /forum.springsource.org/forumdisplay.php?82-社交

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

There are at least 3 levels at which you may work with Spring Social: (1) Using the TwitterTemplate directly, in which case you'd need to obtain the access token and secret through some means of your own, (2) use OAuth1Template, perhaps through TwitterConnectionFactory as the previous answer showed, to get the access token and from that create the TwitterTemplate, in which case you'd have to handle the redirects and callbacks yourself or (3) use Spring Social's ConnectController to handle everything for you.

Using ConnectController involves the least amount of OAuth work on your part. You just configure the appropriate pieces 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 encourage you to have a look at the Spring Social Showcase sample at https://github.com/SpringSource/spring-social-samples. It uses ConnectController to handle by Twitter and Facebook connections. And, of course, you're welcome to ask questions on the Spring Social forum at http://forum.springsource.org/forumdisplay.php?82-Social.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文