Quantcast
Channel: Tweetinvi a friendly Twitter C# library
Viewing all articles
Browse latest Browse all 4126

Commented Unassigned: Encountering 'Operation is not valid due to the current state of the object.' when trying to authenticate via callback in MVC [2334]

$
0
0
I took your console example of user authentication with OAuth and it worked as expected:

```
var applicationCredentials = CredentialsCreator.GenerateApplicationCredentials(_consumerKey, _consumerSecret);
var url = CredentialsCreator.GetAuthorizationURLForCallback(applicationCredentials, "http://10.211.55.7/Map/Callback");
Debug.WriteLine("Go on : {0}", url);
Console.WriteLine("When redirected to your website copy and paste the URL: ");

var callbackURL = Console.ReadLine();

var newCredentials = CredentialsCreator.GetCredentialsFromCallbackURL(callbackURL, applicationCredentials);

Console.WriteLine("Access Token = {0}", newCredentials.AccessToken);
Console.WriteLine("Access Token Secret = {0}", newCredentials.AccessTokenSecret);

var credentials = TwitterCredentials.CreateCredentials(newCredentials.AccessToken, newCredentials.AccessTokenSecret, "Ys0AtyHwywZnHxTQncDw28Ma5", "suT4dSXQWvxrTcqmHhmH1j9gZBftAiudYgFfrOue0CZn3h611J");

TwitterCredentials.ExecuteOperationWithCredentials(credentials, () =>
{
var user = User.GetLoggedUser();
string name = user.Name;
Console.WriteLine(name);
});
Console.ReadKey();
```

However, when I adopt this code into an MVC controller (see below), I receive an 'Operation is not valid due to the current state of the object.':

```
public ActionResult Authorize()
{
var url = CredentialsCreator.GetAuthorizationURLForCallback(_credents, "http://10.211.55.7/Map/Callback");

return new RedirectResult(url, false);
}
public ActionResult Callback(string oauth_token, string oauth_verifier)
{
var newCredentials = CredentialsCreator.GetCredentialsFromCallbackURL(Request.RawUrl, _credents); // <== this is where the exception occurs

Console.WriteLine("Access Token = {0}", newCredentials.AccessToken);
Console.WriteLine("Access Token Secret = {0}", newCredentials.AccessTokenSecret);
var credentials = TwitterCredentials.CreateCredentials(newCredentials.AccessToken, newCredentials.AccessTokenSecret, "Ys0AtyHwywZnHxTQncDw28Ma5", "suT4dSXQWvxrTcqmHhmH1j9gZBftAiudYgFfrOue0CZn3h611J");
TwitterCredentials.ExecuteOperationWithCredentials(credentials, () =>
{
var user = Tweetinvi.User.GetLoggedUser();
string name = user.Name;
Debug.WriteLine(name);
});
return RedirectToAction("Index", "Map");
}
```

Stack trace:

```
[TwitterException: Operation is not valid due to the current state of the object.]
Tweetinvi.WebLogic.WebRequestExecutor.ExecuteWebRequest(HttpWebRequest httpWebRequest) +878
Tweetinvi.WebLogic.TwitterRequester.ExecuteQueryWithTemporaryCredentials(String url, HttpMethod httpMethod, ITemporaryCredentials temporaryCredentials, IEnumerable`1 parameters) +229
Tweetinvi.Credentials.CredentialsCreator.GetCredentialsFromVerifierCode(String verifierCode, ITemporaryCredentials temporaryCredentials) +360
Tweetinvi.CredentialsCreator.GetCredentialsFromVerifierCode(String verifierCode, ITemporaryCredentials temporaryCredentials) +110
Tweetinvi.CredentialsCreator.GetCredentialsFromCallbackURL(String callbackURL, ITemporaryCredentials temporaryCredentials) +144
FinalUniProject.Controllers.MapController.Callback(String oauth_token, String oauth_verifier) in z:\Dropbox\FinalUniProject\FinalUniProject\Controllers\MapController.cs:45
lambda_method(Closure , ControllerBase , Object[] ) +179
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +247
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +120
System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +452
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +15
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +31
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +230
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
```
Comments: ** Comment from web user: adaam2 **

Thank you very much Linvi, I will try these steps when I get back from work this evening.


Viewing all articles
Browse latest Browse all 4126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>