0

如果我的最终用户被定向到 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, Func2 getResult) --- 内部异常堆栈跟踪结束 --- 在 LinqToTwitter.TwitterExecute.PostToTwitter[T](String url, IDictionary2 postData, Func2 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
4

2 回答 2

0

看起来问题在于您的 OAuthToken 和 AccessToken 倒退了。这是您应该使用的代码:

    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.OAuthToken = Me.SavedToken        ' <= here
        objCredentials.AccessToken = Me.SavedTokenSecret ' <= and here

        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
于 2013-09-20T21:12:47.073 回答
0

谢谢你的帮助,乔。

我的 OAuthToken 不是以 [TwitterAccnt#]-xxxxx 开头的。我对我的方法进行了一些验证,并立即以我的方式看到了错误。

Private Sub Authorize(Optional UseSavedUserCredentials As Boolean = True)
    'Comment: Triggered on PostBack
    Dim objCredentials As IOAuthCredentials = New InMemoryCredentials

    objCredentials.ConsumerKey = Me.ConsumerKey
    objCredentials.ConsumerSecret = Me.ConsumerSecret

    If UseSavedUserCredentials = True Then
        If Me.SavedToken.Contains("-") = True And Me.SavedTokenSecret.Contains("-") = False Then
            objCredentials.OAuthToken = Me.SavedToken
            objCredentials.AccessToken = Me.SavedTokenSecret
        Else
            Throw New Exception("Incorrect credentials<br/><br/>")
        End If
    Else
        If Me.DevAccountToken.Contains("-") = True And Me.DevAccountTokenSecret.Contains("-") = False Then
            objCredentials.OAuthToken = Me.DevAccountToken
            objCredentials.AccessToken = Me.DevAccountTokenSecret
        Else
            Throw New Exception("Incorrect credentials<br/><br/>")
        End If
    End If

    mobj_WebAuthorizer = New WebAuthorizer

    mobj_WebAuthorizer.Credentials = objCredentials

    mobj_TwitterContext = New TwitterContext(mobj_WebAuthorizer)
End Sub
于 2013-09-22T01:57:43.277 回答