Twitter plugin error

Searched the new forum and looked in the old forum but I’m not finding a definitive answer to my problem/question. Does the twitter plugin still work? If so how do I get around the “Error: Invalid status code 401 while obtaining request token from https://api.twitter.com/oauth/request_token: Failed to validate oauth signature and token” error?

I can say the twitter plugin still works and works great. Seems like your having a problem with the twitter side of things. I presume you signed up for a dev account under the twitter handle you plan on using for your kegbot?

Thanks
Jeff

I’m having the same problem. Has anyone had success? The full error message is
Error: Invalid status code 401 while obtaining request token from https://api.twitter.com/oauth/request_token: <?xml version="1.0" encoding="UTF-8"?> <hash> <error>Desktop applications only support the oauth_callback value 'oob'</error> <request>/oauth/request_token</request> </hash>

Fixed! You need to add the callback URL in the Twitter App settings. I set it to http://kegbot.example.com/kegadmin/plugin/twitter/ and it worked

1 Like

Okay so I’m bringing this up as Twitter has changed it’s policy in the last month on this and is now enforcing the callback URL whitelist. I can’t for the life of me figure out how to know what callback URL twitter is getting from my kegbot server so I can properly whitelist it…??? Here is what I’m now getting…:

Error: Invalid status code 403 while obtaining request token from https://api.twitter.com/oauth/request_token: <?xml version="1.0" encoding="UTF-8"?>Callback URL not approved for this client application. Approved callback URLs can be adjusted in your application settings

Thanks,
Jeff

@jeff200883,

Checkout my post here: Android Tablet Always Logs Pours as Guest regarding Twitter integration.

I’m not sure about your setup but I use a dedicated domain purchased from Google Domains as my Kegbot URL, so my call back URL looks like this in the twitter app configuration:

http://[MY PUBLIC DOMAIN NAME]/kegadmin/plugin/twitter/

Appreciate the reply. Unfortunately that doesn’t seem to be my issue as my callback URL already matches my setup from a domain perspective and full path. So either my call back URL isn’t what I think it is, which I really think is the issue, or twitter is messed up. I can’t seem to figure out what my actual call back URL is so I’m going to keep digging until I find it as it’s clearly not what I/we “think” it should be.

FWIW, I do have access from the internet over port 80 and can access my setup from anywhere without issue using the same domain I’m using in my Twitter application config.

Thanks,
Jeff

Sorry @jeff200883, didn’t read through the history to see you already had this all working!

After I tried to re-link my account, I got the same error as you did. Tried a lot of settings within the Twitter app settings pages with no luck, so it seemed like there was an issue with the way the Kegbot Server was requesting the OAuth Token.

I dug through the code and found the file

/home/kegbot/kegbot-server.venv/lib/python2.7/site-packages/pykeg/contrib/twitter/plugin.py

In that file you will see the following lines:

class TwitterClient(Twitter):
def set_callback_url(self, url):
self.callback_url = url
def get_callback_url(self):
return self.callback_url

I’m trying to come up with a more elegant solution to this, but in the meantime I just updated this code with my hard-coded callback URL:

class TwitterClient(Twitter):
def set_callback_url(self, url):
self.callback_url = 'https://[MY PUBLIC DOMAIN]/kegadmin/plugin/twitter/'
def get_callback_url(self):
return self.callback_url

After restarting the Kegbot Server, I’m now able to successfully link my account.

I really should do more testing before I proclaim “fixed”… LOL :slight_smile:

I’m no longer getting the callback URL error so there’s that… Now when I try to link I get kicked over to twitter briefly to get the successful redirect back to the callback url with the oauth token. Only problem is I’m getting redirected back to the login screen for the account that’s trying to link with twitter and it just loops. Trying to debug what’s going on but manually setting the URL seems to only partially work.

Thoughts?

Thanks,
Jeff

This is the URL that comes back to the looping login screen for reference (edits made for privacy):

http://KEGBOT/accounts/login/?next=/kegadmin/plugin/twitter/%3Foauth_token%TOKEN%26oauth_verifier%VERIFIER

Thanks,
Jeff

Alright, so I think I came up with a better solution that does not involve hard-coding values in source files, and also solves the issue when linking from the User Account tab vs. the Admin page.

Take a look at the updates I’ve made to my fork of the Kegbot Server Github project, specifically this change HERE which includes updates to the way the callback URL is created. You should just be able to make the same modifications to your ~pykeg/contrib/twitter/views.py file.

It appears that the old way was grabbing the local IP address as the root of the URL, so I’ve updated it to leverage the KEGBOT_BASE_URL that should be configured in the local_settings.py file.

Also, here’s a snapshot of how I’ve configured the Twitter App Callback URLs to function with both the Admin screen as well as the User Account screen.

twitter_callbacks

Let me know if this resolves the issues you’re having.

No bueno… Back to the invalid callback URL issue… I removed the static entry and updated my views.py to reflect your changes and…

Error: Invalid status code 403 while obtaining request token from https://api.twitter.com/oauth/request_token: <?xml version=“1.0” encoding=“UTF-8”?><errors><error code=“415”>Callback URL not approved for this client application. Approved callback URLs can be adjusted in your application settings</error></errors>

So if I define the URL manually, I get a login loop from the kegbot server, if I make the edits proposed I am back to the invalid callback URL. :slight_smile:

Thanks,
Jeff

EDIT: To be clear, I removed the static URL entry from plugin.py, updated views.py per your edits, and made sure my local_settings.py has the desired KEGBOT_BASE_URL value. Is there some log or something I can look at to know what URL is actually being sent?

I think I made a mistake in testing since my account was already linked and I was just using the “Re-link” account rather than fully removing the link to Twitter and Rebuilding it. When I tested the full remove/add I was not getting the URL error, but the account was never linking.

It turns out you do need the “callback” on the end of the URL for it to succeed. I’ve checked in an update HERE where I removed the code to strip off the “callback” path at the end of the URL.

You will also need to update your Twitter App configuration to this:

twitter_callbacks

NOTE: make sure you include the trailing ‘/’ at the end of the URL, it turns out Twitter is picky!

Make sure after any changes to the Python source files you’re either rebooting the server or restarting the Kegbot Service. The command:

sudo supervisorctl restart kegbot:*

Should restart the services, at least it does on my Kegberry installation.

Finally, as far as logging what URL is actually being sent, I have not found anything by default, so I had to write some of my own debug statements in the code and test it out. Here’s the steps I performed:

  1. Create a new directory `/public’
  2. Create a text file in that directory called info.txt
  3. Run the following commands to give full permission to the directory and text file

sudo chmod a+rw /public
sudo chmod a+rw /public/info.txt

  1. In the python file, where you want to “log” something, add the following lines. In this case, I’m logging the url parameter, but you can replace the content of the f.write method with whatever you want logged to that text file. I’ve also included a “new line” character. And again, after making any changes you will need to restart Kegbot.

f = open("/public/info.txt", “a”)
f.write("Twitter URL: " + url + “\n”)

Hopefully this helps push things over the edge to get things working, but let me know if this continues to fail.

1 Like

You. Are. The. Man… LOL :slight_smile:

We’re all green and good to go here, twitter is working as expected for all account types (admin and users).

At this point I’m 100% happy with this setup. Sure there are some things I wish I could add but am still working on my coding skills so… :). I did steal one of your other changes with regards to adding the keg temp to the fullscreen view. So thank you for that as well… :slight_smile:

Overall THANK YOU!!!

Thanks,
Jeff

YES!! Glad we got it working.

And feel free to steal anything you find out there!! Hopefully more updates and improvements to come!

1 Like