当我尝试发送privatemessage时,LinqToTwitter会生成异常

时间:2012-06-13 15:37:47

标签: asp.net-mvc-3 c#-4.0 linq-to-twitter

我尝试向已经使用my_app进行身份验证的用户的关注者发送私信,这里是代码:

    var authent = new MvcAuthorizer
        {
            Credentials = new SessionStateCredentials()
            {
                ConsumerKey = this.client.ConsumerKey,
                ConsumerSecret = this.client.ConsumerSecret,
                OAuthToken = identity.Token.Token
            }
        };

        var twitterCtx = new TwitterContext(authent);
        list_friend.ToList().ForEach(x => twitterCtx.NewDirectMessage(x.InvitedFriendID, messageWithPlaceHolders.Replace("[FRIEND_NAME]", x.Name)));

list_friend是经过身份验证的用户的关注者列表。

Pleaaaase我需要你的帮助。

1 个答案:

答案 0 :(得分:0)

解决方案是使用InMemoryCrendentials而不是SessionStateCredentials并将令牌秘密添加到crendential,之后我们应该将DateTime.Now添加到消息中,因为twitter不允许重复消息,这里是解决它的解决方案的代码好吧:

  var authent = new MvcAuthorizer
        {
            Credentials = new InMemoryCredentials()
            {
                ConsumerKey = this.client.ConsumerKey,
                ConsumerSecret = this.client.ConsumerSecret,
                OAuthToken = identity.Token.Token,
                AccessToken = identity.Token.Secret
            }
        };
  var twitterCtx = new TwitterContext(authent);            
  list_friend.ToList().ForEach(x => twitterCtx.NewDirectMessage(x.SocialId, messageWithPlaceHolders.Replace("[FRIEND_NAME]", x.Name) +DateTime.Now.ToString()));

谢谢