3

I am attempting to add twitter integration into an existing MVC4 Application using the LinqToTwitter nuget plugin (v3.1.2), and following the documentation here

Imports LinqToTwitter

Public Class OAuthController
Inherits System.Web.Mvc.Controller

Async Function BeginAsync() As Threading.Tasks.Task(Of ActionResult)
    Dim auth = New MvcAuthorizer() With { _
        .CredentialStore = New SessionStateCredentialStore() With { _
            .ConsumerKey = ConfigurationManager.AppSettings("consumerKey"), _
            .ConsumerSecret = ConfigurationManager.AppSettings("consumerSecret") _
        } _
    }

    Dim twitterCallbackUrl As String = Request.Url.ToString().Replace("Begin", "Complete")
    Dim callbackUrl = New Uri(twitterCallbackUrl)
    Return Await auth.BeginAuthorizationAsync(callbackUrl)
End Function

The second to final line throws a System.NullReferenceException: Object reference not set to an instance of an object. exception as the error is thrown from the plugin i am unsure how to proceed.

Stack trace here

Zippy42
  • 81
  • 7

1 Answers1

1

I found the problem. The root cause is because the BeginAuthorizationAsync uses HttpContext.Current under the covers, which is not the correct way to access the context for MVC Controllers.

I've submitted a pull request here: https://github.com/JoeMayo/LinqToTwitter/pull/18

As a workaround you can add the following code just before calling BeginAuthorizationAsync.

auth.GoToTwitterAuthorization = (authUrl) => {
    ControllerContext.HttpContext.Response.Redirect(authUrl);
};
Ben Cull
  • 9,434
  • 7
  • 43
  • 38