Reputation: 3
I'm trying to call the Twitter API to get a JSON but cannot seem to get the JSON. If I replace the URL in my code to say "https://mashable.com", I can read the response. I am able to read the response in PHP using CURL. I'm using Python 2.7, Google App Engine. I'm very new to Python. Can you see where I'm going wrong? Here is my code:
import jinja2
import os
import webapp2
import urllib2
template_env = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.getcwd()))
class MainPage(webapp2.RequestHandler):
def get(self):
template = template_env.get_template('home.html')
self.response.out.write(template.render())
class Get_data(webapp2.RequestHandler):
def get(self):
response = urllib2.urlopen('https://api.twitter.com/1/favorites.json?count=5&screen_name=episod')
html = response.read()
print(html)
application = webapp2.WSGIApplication([('/', MainPage),('/get_data', Get_data)],debug=True)
Upvotes: 0
Views: 288
Reputation: 1133
The code in get data works but you're trying to print instead of outputing it on a page.. Use self.response.out.write(str(html)) in order to test it (btw its JSON data not html). Then create a template in which you can render the response.
from django.utils import simplejson as json
class Get_data(webapp2.RequestHandler):
def get(self):
response = urllib2.urlopen('https://api.twitter.com/1/favorites.json?count=5&screen_name=episod')
data = response.read()
json_data = json.loads(data)
template_values = {
'param1': json_data["..."],
'param2': json_data["..."],
}
template = jinja_environment.get_template(TEMPLATE)
self.response.out.write(template.render(template_values))
Upvotes: 1
Reputation: 531
What is the response you're getting? I'm guessing the appengine servers have hit the Twitter API rate limit.
You might need to look into using the full Oauth2 version of the twitter API to get your own rate limited account. Check out tweepy for a good python client: https://github.com/tweepy/tweepy
Upvotes: 0