How to tweet from Django? - python

I want to create a manage.py command that takes an object from a queryset and posts it on my Twitter. I was going to use the curl method as described in here.
But Twitter has since disabled basic authentication. I don't want to install an overkill library like Tweepy that lets people authenticate to my site with oAuth, I just simply want to tweet. On a single account.

from twitter api page:
For applications with single-user use cases, we now offer the ability to issue an access token for your own account (and your own applications). You can generate these keys from your application details pages. Find out more about using a single access token.
go to https://dev.twitter.com/apps to get your keys
from there on out, python-twitter will be happy to parse these keys for you with
api = twitter.Api(consumer_key='consumer_key', consumer_secret='consumer_secret',
access_token_key='access_token', access_token_secret='access_token_secret')

Related

Flask-Dance for OAuth

I am relatively new to programming, and have been learning about OAuth2 with Python. Specifically, I have been learning how to use Flask-Dance, beginning with its implementation for Google authentication. I am wondering:
1) Which Google API does Flask-Dance make use of? I see that the default scope in F-D is 'profile', but I can't seem to figure out what other scopes are available
2) What is the difference between the Google API Explorer and the Google OAuth2 Playground? When/why would I use one over the other?
Any help would be appreciated.
Thanks!
1.) Flask dance makes use of OAuth. Oauth is used specifically for allowing users to give authorization to your app or for authenticating users with the OpenID standard. What this means is, say, you want to get data from a user's google account, e.g. you want a list of their google contacts, you'll use OAuth to get authorization from that user. Another use case is if you want to let users login to your application using google. You'd use Oauth for that. In this case you'll be dealing mostly with access tokens and authorization codes, this is what Flask Dance is for.
For more information on OAuth, here's a video that explains it and its various use cases in plain English: https://www.youtube.com/watch?v=0VWkQMr7r_c
2.) The Google API is for a completely different use case. You're not trying to get data from a user's google account and you're not trying to let users login to your application with google. You want to simply use a Google service on your application. For instance you want to use Google Maps in your app so that you can let users of your app get directions to a place. In this case, you'll be working with API keys that identify your application.

Twitter data mining for a specific user (python)

I want to setup a bot, which will read tweets from a twitter user and store. I can find several examples on search for particular key words, post ID etc.
Can any one recommend which api call is required for monitoring one particular user?
use tweepy for data mining from twitter https://github.com/tweepy/tweepy
The Twitter API call you would use is statuses/user_timeline. The documentation is here.
Some python examples to get you started with using Twitter are here.

How to tweet from GAE

I want to be able to Tweet from my app running on GAE.
Please suggest some Python library or HTTP API for the purpose.
About python-twitter: I think you can use this lib it seems to be compatible with GAE: http://code.google.com/p/python-twitter/source/browse/twitter.py
Also:
Twitter has a very great REST API https://dev.twitter.com/docs/api, You can also to simply use urlfetch and simplejson from appengine.
For authentication Twitter uses OAuth and recommend to understand how it work:
Twitter supports a few authentication methods and with a range of
OAuth authentication styles you may be wondering which method you
should be using. When choosing which authentication method to use you
should understand the way that method will affect your users
experience and the way you write your application.
Twitter share a very great how to https://dev.twitter.com/docs/auth/oauth
You can also see the part of code: http://code.google.com/p/jaikuengine/source/browse/trunk/oauth_client.py from jaikuengine.
This project worked for me: https://github.com/tav/tweetapp/blob/master/standalone/twitter_oauth_handler.py
It's only one file so it's easy to get it started and uses OAuth for authentication with twitter.
I have used twython in the past. I can't remember what made me select it over other libraries but I was using it on GAE and it is kept up-to-date.
You might find the Tweet Engine project of interest. It demonstrates quite simply how to use the Twitter REST API from App Engine.

Python Twitter streaming API - followed users only

I'm attempting to get the statuses of followed users only using the Python Twitter library. It works fine for the 'everyone' stream:
from twitter.stream import TwitterStream
from twitter.oauth import OAuth
from twitter.util import printNicely
stream = TwitterStream(
auth=OAuth(
acc_key, acc_secret,
con_key, con_secret),
domain="userstream.twitter.com",
api_version='1',
secure=True)
print stream.statuses.sample()
tweet_iter = stream.statuses.sample()
for tweet in tweet_iter:
if tweet.get('text'):
printNicely(tweet['text'])
But I want to restrict the output to users I follow only. As far as I can tell from the docs, sample() gives me everything, but I can't tell whether there's a filter() method. Should I be using a different library? I've been using Tweepy for non-streaming Twitter tasks.
Actually, twython doesn't support support the userstream live stream. See here. There doesn't seem to be much activity in its user group, either.
I recommend twython for a Python Twitter library. It is regularly maintained, a useful property considering how often Twitter changes its API.
EDIT: See tweetstream for a simple streaming API implementation which as filtering capabilities.

Is it possible to update a Google calendar from App Engine without logging in as the owner?

I'd like to be able to use the Google Data API from an AppEngine application to update a calendar while not logged in as the calendar's owner or the a user that the calendar is shared with. This is in contrast to the examples here:
http://code.google.com/appengine/articles/more_google_data.html
The login and password for the calendar's owner could be embedded in the application. Is there any way to accomplish the necessary authentication?
It should be possible using OAuth, i havent used it myself but my understanding is the user logs in and then gives your app permission to access their private data (e.g. Calendar records). Once they have authorised your app you will be able to access their data without them logging in.
Here is an article explaining oauth and the google data api.
http://code.google.com/apis/gdata/articles/oauth.html
It's possible to use ClientLogin as described here:
http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#Response
Note the section at the bottom of the document that mentions handling a CAPTCHA challenge.
There's example code included in the gdata python client in
samples/calendar/calendarExample.py
You need to call run_on_app_engine with the right arguments to make this work as described in the Appendix here:
http://code.google.com/appengine/articles/gdata.html
Note that the same document recommends against using ClientLogin for web apps. Using OAuth or AuthSub is the correct solution, but this is simpler and good enough for testing.

Categories