如何在应用重启时重用社交api的认证令牌
本文关键字:api 令牌 认证 应用 重启 | 更新日期: 2023-09-27 18:05:38
在使用Tweetings Janetter等twitter应用程序时。您验证一次并输入您的验证pin码,从那时起,我至少3周以上不必再次验证
在使用。net api如Spring时。Net Social Twitter的例子ConsoleQuickStart,跳过行
Process.Start(authenticateUrl);
和从以前的应用运行时输入代码不工作,并抛出一个异常
而关闭其他twitter应用程序(如twitter)不需要重新认证!!
关于如何重用令牌和重用良好行为的一般规则有什么帮助吗?
感谢示例在https://github.com/SpringSource/spring-net-social-twitter/blob/master/examples/Spring.ConsoleQuickStart/src/Spring.ConsoleQuickStart/Program.cs
UPDATE -
现在得到一个HTTP 401未授权更改代码后,这-
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:
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一起重用。