如何在应用程序重启时重用Auth Tokens for Social Api

时间:2013-03-23 06:56:53

标签: c# .net twitter spring.net

使用某些Twitter应用程序,如Tweetings Janetter等。您进行一次身份验证并输入您的身份验证密码,从那时起我就不必再进行至少3周以上的身份验证了

在使用.net api之类的Spring.Net社交Twitter示例ConsoleQuickStart时,跳过了 行

       Process.Start(authenticateUrl);

并从之前的应用运行时输入代码不起作用并抛出和异常

关闭其他Twitter应用程序(如推文)不需要重新验证!!

关于如何重用令牌和有关此类重用的良好行为的一般规则的任何帮助?

感谢

示例是 https://github.com/SpringSource/spring-net-social-twitter/blob/master/examples/Spring.ConsoleQuickStart/src/Spring.ConsoleQuickStart/Program.cs

更新 -

现在在将代码更改为此后获取HTTP 401 Unauthorized -

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 ;
}

完整的错误堆栈是 -

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: 

1 个答案:

答案 0 :(得分:0)

Twitter不会使ACCESS令牌失效:https://dev.twitter.com/docs/auth/oauth/faq

了解OAuth如何确保您不会犯一个简单的错误非常重要。您的应用程序使用它的应用程序ID从twitter请求“Request_Token”,然后将其提供给身份验证页面。然后,用户登录以授予与Request_Token关联的此应用程序的访问权限,如果用户成功登录,您的回调位置将收到OAuth_Access_Token和OAuth_Access_Token_Secret(以及其他一些内容)。

您是否尝试重新使用OAuth_Access_Tokens而不仅仅是Request_Token? 你有没有更改你的应用程序的ID? 你有两个OAuth_Access_Tokens吗?

编辑:

只是查看示例代码。

AuthorizedRequestToken requestToken = new AuthorizedRequestToken(oauthToken, pinCode);

如果您跳过上述行,您的oauthToken将不正确。

oauthToken和pinCode是相关的。最初你使用一个oauthToken来获得pinCode。当您第二次从Twitter获得新的oauthToken时,它与您尝试使用的pinCode无关。

解决方法是存储oauthToken,并在第二次运行程序时使用pinCode重新使用它。