无法分配消费者密钥

时间:2014-05-10 08:35:00

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

这个问题与LinqToTwitter有关。我正在尝试创建一个MvcAuthorizer的新实例,但在尝试将密钥分配给凭证存储时,我一直得到一个null异常。以下是特定错误: LinqToTwitter.AspNet.dll中出现“System.NullReferenceException”类型的异常,但未在用户代码中处理

更新

这变得更加怪异。因此,在切换到StateServer后获得相同的初始错误后,我决定将SessionStateCredentialStore更改为InMemoryCredentialStore,然后它似乎工作,除了不返回数据。我可以在fiddler中看到twitter的成功请求,但数据不会返回到代码,因为它似乎停止执行。

这里是包含在构造函数中的代码,顺便说一句,它不在控制器中:

public class Twitter
{

// FIELDS
    private readonly MvcAuthorizer _auth;

    // PROPERTIES
    public TwitterContext Twit
    {
        get
        {
            return new TwitterContext(_auth);
        }
    }
public Twitter()
    {
        // all keys
        var cKey = ConfigurationManager.AppSettings[
            "twitConsumerKey"];
        var cSecKey = ConfigurationManager.AppSettings[
            "twitConsumerSecret"];
        var oToken = AuthTool.getUserTwitterTokens().token;
        var oTokenSecc = AuthTool.getUserTwitterTokens()
            .tokenSecret;

        ICredentialStore cred = new InMemoryCredentialStore();
        cred.ConsumerKey = cKey; // <--- Error here when using SessionStateCredentialStore but not when using InMemoryCredentialStore
        cred.ConsumerSecret = cSecKey;
        cred.OAuthToken = oToken;
        cred.OAuthTokenSecret = oTokenSecc;

        // get auth
        _auth = new MvcAuthorizer()
                {
                    CredentialStore = cred
                };
    }

public string RetrieveTwitterId(string twitName)
{
    // this block executes the query as verified with fiddler using InMemoryCredentialsStore 
    // but doesn't return anything to the user variable. The rest of the code does not execute.
    var user = Twit.User.SingleOrDefault
        (x => x.Type == UserType.Show &&
               x.ScreenName == twitName);

    if(user != null)
    {
       return user.UserID.ToString("F0");
    }
    return null;
 }
 }

我在web api方法中调用该方法:

[HttpPost]
public HttpResponseMessage CreateNewInterview(Model.NewInterview interview)
    {
        // find twitter Id
        var twitterId = new Twitter().RetrieveTwitterId(interview.SubjectTwitterName);// error 


        // assign model
        var model = new NewInterview
            (interview.Title, twitterId, DateTime.Now, interview.PublicQuestions,   interview.Category);

        // create the new interview
        Admin.createInterview(model);

        // create response
        return Request.CreateResponse(HttpStatusCode.OK);
    }

这是什么原因?

1 个答案:

答案 0 :(得分:0)

你在哪里使用它?我会猜测它是无效的会话。确保会话状态在您使用它的位置可用。