Pythontr

husonet | Tarih: 15.12.2010

python twitter api, duvara yazma, python-oauth2 paketi, twitter api örnek kodlama

Sosyal medya artık günümüzün konusu oldu bu örneğimizde python ile nasıl twitter la etkileşimde bulunacağımızın kısa bir örneğini yapacaz önce kurulumdan başlayalım.

Not:Uygulama Tipi Desktop Client'dir.

# Kurulum için gerekli paketleri indirelim
wget https://download.github.com/simplegeo-python-oauth2-hudson-python-oauth2-205-0-g1fcc1a6.tar.gz
# indirdiğimiz paketi açalım
tar xzfv simplegeo-python-oauth2-hudson-python-oauth2-205-0-g1fcc1a6.tar.gz
# açılan paketin dizinine gidelim
cd simplegeo-python-oauth2-1fcc1a6/
#kurulum yapmamız için setup tool paketini install edelim
sudo aptitude install python-setuptools
#paketimizi kuralım
sudo python setup.py install


Örnek kodumuz aşağıdadır.

#!/usr/bin/python
#-*- coding:utf-8 -*-

import urlparse
import oauth2 as oauth
import urllib

request_token_url = "http://twitter.com/oauth/request_token"
access_token_url = "http://twitter.com/oauth/access_token"
authorize_url = "http://twitter.com/oauth/authorize"

post_url = "http://twitter.com/statuses/update.xml"

def getOAuth(sConsumerKey, sConsumerSecret):
consumer = oauth.Consumer(sConsumerKey,sConsumerSecret)
client = oauth.Client(consumer)

#adım 1
resp, content = client.request(request_token_url,'GET')
if resp['status'] != '200':
raise Exception("Invalid response %s." % resp['status'])
request_token = dict(urlparse.parse_qsl(content))

print "Request Token:"
print " - oauth_token = %s" % request_token['oauth_token']
print " - oauth_token_secret = %s" % request_token['oauth_token_secret']
print ""

#adım 2
print "Tarayıcınızda aşağıdaki bağlantıya gidin"
print "%s?oauth_token=%s" % (authorize_url, request_token['oauth_token'])
print ""

accepted = "h"
while accepted.lower() == "h":
accepted = raw_input("Kullanım için yetki verirmisiniz? (e/h) ")
oauth_verifier = raw_input("PIN nedir?:")

#adım 3
token = oauth.Token(request_token['oauth_token'],request_token['oauth_token_secret'])
token.set_verifier(oauth_verifier)
client = oauth.Client(consumer, token)

resp, content = client.request(access_token_url, "POST")
access_token = dict(urlparse.parse_qsl(content))

print "Access Token:"
print " - oauth_token = %s" % access_token['oauth_token']
print " - oauth_token_secret = %s" % access_token['oauth_token_secret']
print ""
print "Şimdi kaynaklara erişim sağlayabilirsiniz"
print ""
return access_token

def postOAuth(sMessage, sConsumerKey, sConsumerSecret):
c = getOAuth(sConsumerKey, sConsumerSecret)
consumer = oauth.Consumer(key=sConsumerKey, secret=sConsumerSecret)
token = oauth.Token(c['oauth_token'],c['oauth_token_secret'])
client = oauth.Client(consumer, token)
i = 0
while i < 2:
vMessage = sMessage + """-Mesaj= %s""" % (i)
params = urllib.urlencode({'status': vMessage})
ret = client.request(post_url, "POST",params)
i += 1

vConsumerKey = "consumer_key"
vConsumerSecret = "consumer_secret"
vMesaj = "Consoldan deneme"
postOAuth(vMesaj, vConsumerKey, vConsumerSecret)


İlgili Konular
twitter api, direk mesaj (direct message)
python twitter arama (search)

Fluent Trails

Oynayarak İngilizce öğren

Kahve dükkanı, otel, market... Günlük hayattan hikâyelerle İngilizce pratik yap.