LinqToTwitter BeginAuthorizationAsync throws NullReferenceException
I am trying to add twitter integration to 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 the final line throws an exception System.NullReferenceException: Object reference not set to an instance of an object.
as the error is being output from the plugin. I'm not sure how to do this.
Stack trace here
source to share
I found the problem. The main reason is that 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 the call to BeginAuthorizationAsync.
auth.GoToTwitterAuthorization = (authUrl) => {
ControllerContext.HttpContext.Response.Redirect(authUrl);
};
source to share