Posting Tweets on behalf of Users

Aug 29, 2013 at 5:57 PM

We have an application which saves our User's Twitter credentials locally and then uses the credentials to post Tweets for scheduled events in the future.

I have been pulling hair out for a month trying to get LinqToTwitter to use saved User credentials to post on the User's behalf.

Here is how I built my code:

I use SessionStateCredentials with a WebAuthorizer calling BeginAuthorization(). My page navigates to Twitter to authorize the User and navigates back to my page where I have the oauth_token and oauth_verifier and an authorized TwitterContext.

At this point, if I call CompleteAuthorization() and TwitterContext.UpdateStatus(), I can successfully post to Twitter on behalf of the User.

Now, I save the oauth_token and oauth_verifier locally.

Here is where things start to go wrong:
I use SingleUserInMemoryCredentials with a SingleUserAuthorizer. Using the saved oauth_token and oauth_verifier , I get the following error:

System.Net.WebException: The remote server returned an error: (401) Unauthorized. at LinqToTwitter.TwitterExecute.PostToTwitter[T](String url, IDictionary2 postData, Func2 getResult)

Am I attempting a workflow LinqToTwitter is not capable of?

Here is my attempted SingleUserAuthorizer code implementation:
Dim objSingleUserAuthorizer As New SingleUserAuthorizer
Dim objCredentials As New SingleUserInMemoryCredentials

objCredentials.ConsumerKey = strSavedConsumerKey
objCredentials.ConsumerSecret = strSavedConsumerSecret
objCredentials.TwitterAccessToken = strSavedTwitterAccessToken
objCredentials.TwitterAccessTokenSecret = strSavedTwitterAccessTokenSecret
objSingleUserAuthorizer.Credentials = objCredentials

mobj_TwitterContext = New TwitterContext(objSingleUserAuthorizer)
mobj_TwitterContext.UpdateStatus("My Tweet " & Date.Now.Millisecond.ToString)
Sep 3, 2013 at 5:49 PM
Your code looks okay. You mentioned that you're saving verifier - maybe that's your problem. After CompleteAuthorization, pull out the OAuthToken and AccessToken. Maybe hit a breakpoint to make sure you're getting them. Then, if you use SingleUserAuthorizer, assign the saved OAuthToken in TwitterAccessToken and the saved AccessToken in TwitterAccessTokenSecret. Alternatively, you can use the WebAuthorizer and fill in all 4 credentials, which will bypass the authorization process and you can query right away.

Sep 3, 2013 at 8:13 PM
If I use the 4 static credentials from our site (Consumer Token, Consumer Secret, Access Token, Access Token Secret), I can post Tweets to our own Twitter page with a WebAuthorizer and/or a SingleUserAuthorizer.

If I use the WebAuthorizer to retrieve the two credentials (Access Token, Access Token Secret) for the App User, as long as I have the page open, I can post to that User's Twitter page.

However, if I store the two User credentials in our database, close the page, open the page again and use the User's credentials as two of the 4 credentials, I get 401 unauthorized errors when using the WebAuthorizer and/or the SingleUserAuthorizer. My code is in the first post of this thread.

Is there something preventing the use of stored User credentials? Or, Is there something else in addition to the two User credentials that gets created from the WebAuthorizer, I would need to store as well?
Sep 22, 2013 at 1:59 AM
Thanks for your help, Joe.

My OAuthToken was not starting with [TwitterAccnt#]-xxxxx. I threw some validation in to my method and immediately saw the error in my ways.
    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
                Throw New Exception("Incorrect credentials<br/><br/>")
            End If
            If Me.DevAccountToken.Contains("-") = True And Me.DevAccountTokenSecret.Contains("-") = False Then
                objCredentials.OAuthToken = Me.DevAccountToken
                objCredentials.AccessToken = Me.DevAccountTokenSecret
                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