使用Hammock实现Tumblr xAuth

纠结与Tumblr 的xAuth好几天了,最终发现还是Hammock的rest类库好用,oauth签名什么的都帮你做了


xAuth代码:

Hammock.RestClient restClient = new Hammock.RestClient
            {
                Authority = "https://www.tumblr.com/",
                Credentials = new Hammock.Authentication.OAuth.OAuthCredentials
                {
                    ConsumerKey = consumerKey,
                    ConsumerSecret = consumerSecret,
                    SignatureMethod = Hammock.Authentication.OAuth.OAuthSignatureMethod.HmacSha1,
                    ParameterHandling = Hammock.Authentication.OAuth.OAuthParameterHandling.HttpAuthorizationHeader,
                    Version = "1.0"
                }
            };
 
            restClient.AddHeader("content-type""application/x-www-form-urlencoded");
 
            Hammock.RestRequest restRequest = new Hammock.RestRequest
            {
                Path = "oauth/access_token",
                Method = Hammock.Web.WebMethod.Post
            };
            restRequest.AddParameter("x_auth_mode""client_auth");
            restRequest.AddParameter("x_auth_username", userName);
            restRequest.AddParameter("x_auth_password", password);
 
            restClient.BeginRequest(restRequest, (request, response, userstate)=>
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string[] content = response.Content.Split(new char[] { '=''&' });
                        OAuth_Token = content[1];
                        OAuth_Secret = content[3];
 
                        callbackSuccess();
                    }
                    else
                    {
                        callbackFailure(response.StatusCode.ToString() + ": " + response.Content);
                    }
                }) ;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RabbitMQ是一个开源的消息代理,它允许你通过发布/订阅模型来处理消息传递。动态创建队列和监听是RabbitMQ的一种高级功能,通常用于处理异步任务和事件驱动的应用场景。 在Java中,我们可以使用`AMQP`客户端库(如`Spring AMQP`、`Pika`或`Hammock`)来动态创建队列并监听。以下是一个简单的示例,展示了如何使用`Spring AMQP`: ```java import org.springframework.amqp.core.*; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.listener.ChannelAwareMessageListener; public class DynamicQueueExample { private final ConnectionFactory connectionFactory; public DynamicQueueExample(ConnectionFactory connectionFactory) { this.connectionFactory = connectionFactory; } // 创建并绑定动态队列 public void createAndBind(String queueName) { try (Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel()) { // 使用交换机创建并绑定队列 String exchangeName = "myExchange"; channel.queueDeclare(queueName, false, false, true, null); channel.queueBind(queueName, exchangeName, routingKey(queueName)); } catch (Exception e) { // 处理异常 e.printStackTrace(); } } // 使用消费者监听队列 public void startListening(ChannelAwareMessageListener listener) { try (Connection connection = connectionFactory.newConnection()) { // 创建消费者并监听指定队列 Channel channel = connection.createChannel(); channel.basicConsume(queueName, true, listener, consumerTag -> {}); } catch (Exception e) { e.printStackTrace(); } } // 动态生成路由键(queueName) private String routingKey(String queueName) { return "#" + queueName; } } // 实现ChannelAwareMessageListener public class MyMessageHandler implements ChannelAwareMessageListener { @Override public void onMessage(Message message, Channel channel) throws Exception { // 处理接收到的消息 String body = new String(message.getBody()); System.out.println("Received message: " + body); } } ``` 在这个例子中,我们首先创建了一个连接工厂,然后创建并绑定动态队列。当需要消费队列中的消息时,我们创建一个监听器,并启动监听。`MyMessageHandler`实现了`ChannelAwareMessageListener`,以便在接收到消息时可以处理它们。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值