如果我的最终用户被定向到 Twitter 的 Oauth 页面并返回到我们的网站,我使用 Linq to Twitter WebAuthorizer 将推文发布到 Twitter 没有问题。以这种方式使用 WebAuthorizer 效果很好。
但是,如果我尝试存储最终用户的 Oauth 凭据并将其用于 Tweet,则会收到 401 Unauthorized 错误:
LinqToTwitter.TwitterQueryException:无效或过期令牌---> System.Net.WebException:远程服务器返回错误:(401)未经授权。在 LinqToTwitter.TwitterExecute.PostToTwitter[T](String url, IDictionary
2 postData, Func
2 getResult) --- 内部异常堆栈跟踪结束 --- 在 LinqToTwitter.TwitterExecute.PostToTwitter[T](String url, IDictionary2 postData, Func
2 getResult)在 LinqToTwitter.StatusExtensions.UpdateStatus(TwitterContext ctx,字符串状态,十进制纬度,十进制经度,字符串 placeID,布尔显示坐标,字符串 inReplyToStatusID,布尔 trimUser,Action`1 回调)在 LinqToTwitter.StatusExtensions.UpdateStatus(TwitterContext ctx,字符串状态)在 External_WCPages_v12_Test_TwitterTest.StoredCredentialsSingleUserAuthorizer() 在 C:\TFS\SC\Weblink_Connect-Development\CWT\External\WCPages\v12\Test\TwitterTest.aspx.vb:第 177 行在 External_WCPages_v12_Test_TwitterTest.Page_Load(Object sender, EventArgs e) in C:\TFS\SC\Weblink_Connect-Development\CWT\External\WCPages\v12\Test\TwitterTest.aspx.vb:第 65 行
我包括处理 Linq to Twitter 请求的主要功能。请帮忙,我做错了什么吗?我已尝试发布 2.106、2.107、2.108 以及截至 2013 年 9 月 19 日的最新变更集。
Private Sub BeginWebAuthorizer()
'Comment: Triggered on PostBack
mobj_WebAuthorizer = New WebAuthorizer
Dim objCredentials As IOAuthCredentials = New InMemoryCredentials()
objCredentials.ConsumerKey = Me.ConsumerKey
objCredentials.ConsumerSecret = Me.ConsumerSecret
mobj_WebAuthorizer.Credentials = objCredentials
mobj_WebAuthorizer.PerformRedirect = Sub(authUrl)
Response.Redirect(authUrl)
End Sub
mobj_WebAuthorizer.BeginAuthorization(Request.Url)
End Sub
Private Sub CompleteWebAuthorizer()
'Comment: Triggered on return from the Twitter authorization page
Dim objCredentials As IOAuthCredentials = New SingleUserInMemoryCredentials
objCredentials.ConsumerKey = Me.ConsumerKey
objCredentials.ConsumerSecret = Me.ConsumerSecret
mobj_WebAuthorizer = New WebAuthorizer
mobj_WebAuthorizer.Credentials = objCredentials
Call mobj_WebAuthorizer.CompleteAuthorization(Request.Url)
mobj_TwitterContext = New TwitterContext(mobj_WebAuthorizer)
'Success: This posts a Tweet
mobj_TwitterContext.UpdateStatus("Test Tweet using LinqToTwitter WebAuthorizer ( not using stored credentials ) : " & Date.Now.Millisecond.ToString)
End Sub
Private Sub StoredCredentialsWebAuthorizer()
'Comment: Triggered on PostBack
Dim objCredentials As IOAuthCredentials = New InMemoryCredentials
'For Testing: Dim objCredentials As IOAuthCredentials = New SingleUserInMemoryCredentials
'For Testing: Dim objCredentials As IOAuthCredentials = New SessionStateCredentials
objCredentials.ConsumerKey = Me.ConsumerKey
objCredentials.ConsumerSecret = Me.ConsumerSecret
objCredentials.AccessToken = Me.SavedToken
objCredentials.OAuthToken = Me.SavedTokenSecret
mobj_WebAuthorizer = New WebAuthorizer
mobj_WebAuthorizer.Credentials = objCredentials
'For Testing: Call mobj_WebAuthorizer.CompleteAuthorization(Request.Url)
mobj_TwitterContext = New TwitterContext(mobj_WebAuthorizer)
'BUG: SingleUserAuthorizer doesn't seem to work with the saved credentials from database
mobj_TwitterContext.UpdateStatus("Test Tweet using LinqToTwitter WebAuthorizer ( using stored credentials ) : " & Date.Now.Millisecond.ToString)
End Sub