无法使用已保存的Oauth凭据将Linq的推文发布到Twitter

时间:2013-09-19 23:27:08

标签: vb.net linq twitter linq-to-twitter

如果我的最终用户被定向到Twitter的Oauth页面并返回我们的网站,那么使用Linq to Twitter WebAuthorizer将推文发布到Twitter是没有问题的。以这种方式使用WebAuthorizer效果很好。

但是,如果我尝试存储最终用户的Oauth凭据并将其用于Tweet,我会收到401 Unauthorized错误:

  

LinqToTwitter.TwitterQueryException:无效或过期的令牌---> System.Net.WebException:远程服务器返回错误:(401)未经授权。 at LinqToTwitter.TwitterExecute.PostToTwitter [T](String url,IDictionary 2 postData, Func 2 getResult)---内部异常堆栈跟踪结束---在LinqToTwitter.TwitterExecute.PostToTwitter [T](String url,IDictionary {{ 1}} 2 getResult)LinqToTwitter.StatusExtensions.UpdateStatus(TwitterContext ctx,String status,Decimal latitude,Decimal经度,String placeID,Boolean displayCoordinates,String inReplyToStatusID,Boolean trimUser,Action`1 callback)at LinqToTwitter.StatusExtensions.UpdateStatus(TwitterContext) ctx,String status)位于External_WCPages_v12_Test_TwitterTest.Page_Load(对象发送者,EventArgs)中的外部_WCPages_v12_Test_TwitterTest.StoredCredentialsSingleUserAuthorizer()位于C:\ TFS \ SC \ Weblink_Connect-Development \ CWT \ External \ WCPages \ v12 \ Test \ TwitterTest.aspx.vb:第177行e)在C:\ TFS \ SC \ Weblink_Connect-Development \ CWT \ External \ WCPages \ v12 \ Test \ TwitterTest.aspx.vb:第65行

我包括处理Linq到Twitter请求的主要功能。请帮忙,我做错了什么?我已尝试发布2.106,2.107,2.108和截至2013年9月19日的最新变更集。

2 postData, Func

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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