Issue retrieving direct messages via a SharePoint 2016 timer job


#1

I am currently developing a timer job that executes on intervals and captures tweets and direct messages from specific twitter handles used by our organization principals for records retention purposes. My timer job is currently able to capture and archive my tweets from my own personal twitter handle which I use it to test with for now. However, I’m not able to capture and archive any direct messages due to the error, “Client is not permitted to perform this action”. Based on my research on the web, I found out that my application has to be whitelisted first before my application can receive my direct messages. Is this correct? If so, then how do I get whitelisted?

This works for capturing my tweets:

var auth = new SingleUserAuthorizer
{
    CredentialStore = new SingleUserInMemoryCredentialStore
    {
        ConsumerKey = oauth_consumer_key,
        ConsumerSecret = oauth_consumer_secret,
        AccessToken = oauth_token,
        AccessTokenSecret = oauth_token_secret
    }
}

And this does not work:

var auth = new XAuthAuthorizer
{
     CredentialStore = new XAuthCredentials
     {
         ConsumerKey = oauth_consumer_key,
         ConsumerSecret = oauth_consumer_secret,
         UserName = "....._._......",	//  <--- Obviously I have masked the twitter handle and password for the purpose of posting it here....
         Password = "............"
     }
};

Task authTask = auth.AuthorizeAsync();
authTask.Wait();

With this, I get the following exception…

  InnerException: 
       ErrorCode=0
       HResult=-2146233088
       Message=<?xml version="1.0" encoding="UTF-8"?><errors><error code="87">Client is not permitted to perform this action</error></errors>
       ReasonPhrase=Forbidden
       Source=LinqToTwitter.net
       StackTrace:
            at LinqToTwitter.Net.TwitterErrorHandler.BuildAndThrowTwitterQueryException(String responseStr, HttpResponseMessage msg)
            at LinqToTwitter.Net.TwitterErrorHandler.<HandleGenericErrorAsync>d__1.MoveNext()

#2

xAuth is no longer supported for applications. You need to implement OAuth (and not pass the username and password).


#3

Hi Andy, thanks for your response.

So, I went back to implementing OAuth and now I’m back to getting the following error again:

  InnerException: 
       ErrorCode=93
       HResult=-2146233088
       Message=This application is not allowed to access or delete your direct messages.
       ReasonPhrase=Forbidden
       Source=LinqToTwitter.net
       StackTrace:
            at LinqToTwitter.Net.TwitterErrorHandler.BuildAndThrowTwitterQueryException(String responseStr, HttpResponseMessage msg)
            at LinqToTwitter.Net.TwitterErrorHandler.<HandleGenericErrorAsync>d__1.MoveNext()

even though I have “Read, Write and Access direct messages” configured with new consumer key/secret… Any thoughts?

Thanks again!


#4

Did you regenerate the access tokens after you added the Direct Message permission?


#5

That was the issue. I regenerated a new set of consumer tokens, but didn’t realize I had to also regenerate the access tokens. Thanks so much for your assistance as my timer job is retrieving direct messages now. Best regards.