0

エンドユーザーが Twitter の Oauth ページに誘導され、私たちのサイトに戻った場合、Linq to Twitter WebAuthorizer を使用してツイートを Twitter に投稿しても問題ありません。このように WebAuthorizer を使用するとうまくいきます。

ただし、エンドユーザーの Oauth 資格情報を保存してツイートに使用しようとすると、401 Unauthorized エラーが発生します。

LinqToTwitter.TwitterQueryException: 無効または期限切れのトークン ---> System.Net.WebException: リモート サーバーがエラーを返しました: (401) Unauthorized. LinqToTwitter.TwitterExecute.PostToTwitter[T](String url, IDictionary 2 postData, Func2 getResult) --- 内部例外スタック トレースの終了 --- LinqToTwitter.TwitterExecute.PostToTwitter[T](String url, IDictionary)2 postData, Func2 getResult) at LinqToTwitter.StatusExtensions.UpdateStatus(TwitterContext ctx, String status, Decimal latitude, Decimal longitude, String placeID, Boolean displayCoordinates, String inReplyToStatusID, Boolean trimUser, Action`1 callback) at LinqToTwitter.StatusExtensions.UpdateStatus(TwitterContext ctx, String C:\TFS\SC\Weblink_Connect-Development\CWT\External\WCPages\v12\Test\TwitterTest.aspx.vb: の External_WCPages_v12_Test_TwitterTest.Page_Load(Object sender, EventArgs e) の 177 行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 に答える