Question

In using some of the twitter apps such as Tweetings Janetter etc. You authenticate once and enter your auth pincode and since then i haven't had to authenticate again for atleast 3+ weeks

In using the .net api's such as Spring.Net Social Twitter example ConsoleQuickStart, skipping the line

       Process.Start(authenticateUrl);

and entering the code from previous app runtime does not work and throws and exception

whereas shutting down other twitter apps such as tweetings does not require re-authentication !!

Any help on how to reuse tokens and general rules on good behavior for such reuse ?

thanks

The example is at https://github.com/SpringSource/spring-net-social-twitter/blob/master/examples/Spring.ConsoleQuickStart/src/Spring.ConsoleQuickStart/Program.cs

UPDATE -

Now getting a HTTP 401 Unauthorized after changing the code to this -

if ( ! haveOAuthAndPin )
{
                    var oauthToken = twitterServiceProvider.OAuthOperations.FetchRequestTokenAsync("oob", null).Result;



                    string authenticateUrl = twitterServiceProvider.OAuthOperations.BuildAuthorizeUrl(oauthToken.Value, null);
                    Console.WriteLine("Redirect user for authentication: " + authenticateUrl);
                    Process.Start(authenticateUrl);
                    Console.WriteLine("Enter PIN Code from Twitter authorization page:");
                    pinCode = Console.ReadLine();

                    StoreAuthTokenAndPin(oAuthToken, pinCode);
}
else
{
      var oAuthToken = new oAuthToken(storedValue, storedSecret)
      pinCode = storedPinCode ;
}

the complete error stack is -

System.AggregateException was unhandled
  HResult=-2146233088
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
       at System.AggregateException.Handle(Func`2 predicate)
       at Spring.ConsoleQuickStart.Program.Main(String[] args) in C:\downloads\_source-code\Spring.net\Spring.Social.Twitter-2.0.0-M1\examples\Spring.ConsoleQuickStart\src\Spring.ConsoleQuickStart\Program.cs:line 168
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.AggregateException
       HResult=-2146233088
       Message=One or more errors occurred.
       Source=mscorlib
       StackTrace:
            at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
            at Spring.Social.OAuth1.OAuth1Template.<ExchangeForAccessTokenAsync>b__1(Task`1 task) in f:\bamboo-home\xml-data\build-dir\SPRNETSOCIAL-RELEASE-JOB1\src\Spring.Social.Core\Social\OAuth1\OAuth1Template.cs:line 233
            at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
            at System.Threading.Tasks.Task.Execute()
       InnerException: Spring.Rest.Client.HttpClientErrorException
            HResult=-2146233088
            Message=POST request for 'https://api.twitter.com/oauth/access_token' resulted in 401 - Unauthorized (Unauthorized).
            Source=Spring.Rest
            StackTrace:
                 at Spring.Rest.Client.Support.DefaultResponseErrorHandler.HandleError(Uri requestUri, HttpMethod requestMethod, HttpResponseMessage`1 response) in f:\bamboo-home\xml-data\build-dir\SPRNETREST-RELEASE-JOB1\src\Spring.Rest\Rest\Client\Support\DefaultResponseErrorHandler.cs:line 121
                 at Spring.Rest.Client.Support.DefaultResponseErrorHandler.HandleError(Uri requestUri, HttpMethod requestMethod, IClientHttpResponse response) in f:\bamboo-home\xml-data\build-dir\SPRNETREST-RELEASE-JOB1\src\Spring.Rest\Rest\Client\Support\DefaultResponseErrorHandler.cs:line 86
                 at Spring.Rest.Client.RestTemplate.HandleResponseError(Uri uri, HttpMethod method, IClientHttpResponse response, IResponseErrorHandler errorHandler) in f:\bamboo-home\xml-data\build-dir\SPRNETREST-RELEASE-JOB1\src\Spring.Rest\Rest\Client\RestTemplate.cs:line 2959
                 at Spring.Rest.Client.RestTemplate.ResponseReceivedCallback[T](ClientHttpRequestCompletedEventArgs responseReceived) in f:\bamboo-home\xml-data\build-dir\SPRNETREST-RELEASE-JOB1\src\Spring.Rest\Rest\Client\RestTemplate.cs:line 2777
            InnerException: 
Was it helpful?

Solution

Twitter doesn't expire ACCESS tokens: https://dev.twitter.com/docs/auth/oauth/faq

It is important to understand how OAuth works to make sure you aren't making a simple mistake. Your application uses it's application ID to request a "Request_Token" from twitter, which it then provides to the authentication page. The user then logs in to grant access to this application associated with the Request_Token, if the user logs in successfully your callback location will receive an OAuth_Access_Token and OAuth_Access_Token_Secret (and some other things).

Are you trying to re-use the OAuth_Access_Tokens and not just the Request_Token? Have you changed your application's ID? Did you keep both OAuth_Access_Tokens?

EDIT:

Just looked at the sample code.

AuthorizedRequestToken requestToken = new AuthorizedRequestToken(oauthToken, pinCode);

Your oauthToken will be incorrect if you skip the line you mentioned.

The oauthToken and the pinCode are related. Originally you're using one oauthToken to get a pinCode. When you get a new oauthToken from Twitter the second time around, it isn't related to your pinCode that you're trying to use.

Solution is to store the oauthToken too and re-use that with the pinCode the second time running the program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top