1
2from __future__ import unicode_literals
3import requests
4from requests_oauthlib import OAuth1
5from urlparse import parse_qs
6import sys
7
8REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token"
9AUTHORIZE_URL = "https://api.twitter.com/oauth/authorize?oauth_token="
10ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token"
11
12CONSUMER_KEY = "****"
13CONSUMER_SECRET = "****"
14
15OAUTH_TOKEN = ""
16OAUTH_TOKEN_SECRET = ""
17
18
19def setup_oauth():
20 """Authorize your app via identifier."""
21
22 oauth = OAuth1(CONSUMER_KEY, client_secret=CONSUMER_SECRET)
23 r = requests.post(url=REQUEST_TOKEN_URL, auth=oauth)
24 credentials = parse_qs(r.content)
25
26 resource_owner_key = credentials.get('oauth_token')[0]
27 resource_owner_secret = credentials.get('oauth_token_secret')[0]
28
29
30 authorize_url = AUTHORIZE_URL + resource_owner_key
31 print 'Please go here and authorize: ' + authorize_url
32
33 verifier = raw_input('Please input the verifier: ')
34 oauth = OAuth1(CONSUMER_KEY,
35 client_secret=CONSUMER_SECRET,
36 resource_owner_key=resource_owner_key,
37 resource_owner_secret=resource_owner_secret,
38 verifier=verifier)
39
40
41 r = requests.post(url=ACCESS_TOKEN_URL, auth=oauth)
42 credentials = parse_qs(r.content)
43 token = credentials.get('oauth_token')[0]
44 secret = credentials.get('oauth_token_secret')[0]
45
46 return token, secret
47
48
49def get_oauth():
50 oauth = OAuth1(CONSUMER_KEY,
51 client_secret=CONSUMER_SECRET,
52 resource_owner_key=OAUTH_TOKEN,
53 resource_owner_secret=OAUTH_TOKEN_SECRET)
54 return oauth
55
56if __name__ == "__main__":
57 ashtag = str(sys.argv[1])
58 oauth = get_oauth()
59
60 part1 = "https://api.twitter.com/1.1/search/tweets.json?q=%23"
61 part2 = "&count=1"
62 r = requests.get(url=part1+ashtag+part2, auth=oauth)
63
64 message_id = r.json()["statuses"][0]["id"]
65 text = r.json()["statuses"][0]["text"]
66
67 print message_id
68 print text
69
Anonymous user
8 years ago
Working on the Yun is totally new to me. I was able to get a bunch of things figured out, but I'm stumped when I run the YunTwitter.py on the board as I keep getting an error. "Traceback (most recent call last): File "YunTwitter.py", line 3, in <module> import requests ImportError: No module named requests" If I run the same program through terminal locally it returns a tweet exactly as expected. Have I managed to not install something correctly? If anyone knows I would love the help. Otherwise, this instructional project has taught me a ton and I'm really enjoying it!