How to read JSON data return by the Facebook access_token - django

I'm trying to get the facebook username of a logged in user in my site. I have this code in my view (on Django) :
code = request.GET.get('code')
url = 'https://graph.facebook.com/oauth/access_token?client_id=%(id)s&redirect_uri=http://127.0.0.1:8000/skempi/home&client_secret=%(secret)s&code=%(code)s'%{'id':fb_id,'secret':fb_s,'code':code}
response = urllib2.urlopen(url)
html = response.read()
dic = dict(urlparse.parse_qsl(html))
graph_url = 'https://graph.facebook.com/me?access_token='+dic.get('access_token')
when I do return HttpResponseRedirect(graph_url) I can see the JSON data. However, I'm not able to read that data using
import simplejson
d = simplejson.load(graph_url)
context ={'user':d}
return render_to_response('home.html', context, context_instance=RequestContext(request))
I get this error :
'str' object has no attribute 'read'

You have to actually download the JSON before simplejson can decode it.
response = urllib2.urlopen(graph_url)
data = simplejson.load(response)

Related

Django freezes when reading data from request.body

Let's say we have a simple Django view:
def my_view(request):
content = request.body
# some actions with content varible
response = HttpResponse('<h1>It work!</h1>')
And a simple api client, let's say based on the requests library, sending malformed Django view data:
headers = dict()
headers['Accept'] = '*/*'
headers['Content-Length'] = '13409'
headers['Content-Type'] = 'application/x-compressed'
headers['Expect'] = '100-continue'
headers['Host'] = '127.0.0.1:8000'
headers['User-Agent'] = 'Api client'
headers['content-encoding'] = 'gzip'
url = 'http://127.0.0.1:8000/api'
request_body = ''
r = requests.post(
url,
data=request_body,
headers=headers
)
As you can see, request_body contains an empty string, but the Content-Length header stores the value 13409. When such a request arrives, Django hangs on the line reading request.body. No exceptions occur. How to solve this problem? I cannot influence the client, so the only thing I can do is rewrite the Django view. Django version 3.2.15 is used.

Cannot download html (entire web page)

I am trying to download the entire html code from
http://www.ivolatility.com/options/AMZN/NASDAQ/
The output does not include the data in the tables.
This is the code I am using
url = 'http://www.ivolatility.com/options/AMZN/NASDAQ/'
r = requests.get(url, allow_redirects=True)
open('C:.../Downloads/amzn.html', 'wb').write(r.content)
I think it might be related to registration issues.
Anything I can do?
Thanks
Your request returns a login form, which means you'll have to login in order to access the data.
The login process is relatively easy - all we have to do is submit the form data to the login page (and use a session object to store the cookies).
Then we can use that authenticated session to retrieve the table contents.
The code,
import requests
url = 'http://www.ivolatility.com/options/AMZN/NASDAQ/'
login_url = 'https://www.ivolatility.com/login.j'
usr = 'my username'
pwd = 'my password'
data = {
'username':usr, 'password':pwd,
'ref_url':login_url, 'service_name':'Home Page',
'step':1, 'login__is__sent':1
}
s = requests.session()
s.post(login_url, data)
r = s.get(url)
with open('my file', 'wb') as f:
f.write(r.content)

fetching various data corresponding to a tweet

I am trying to fetch data from twitter for processing. Please see the code I want various data corresponding to a particular tweet corresponding to a given topic. I am able to fetch data (created_at, text, username, user_id). It shows error when i try to fetch(location, followers_count, friends_count, retweet_count).
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import time
import json
ckey = '***********************'
csecret = '************************'
atoken ='*************************'
asecret = '**********************'
class listener(StreamListener):
def on_data(self,data):
try:
all_data = json.loads(data)
tweet = all_data["text"]
username = all_data["user"]["screen_name"]
timestamp = all_data["created_at"]
user_id = all_data["id_str"]
location = all_data["location"]
followers_count = all_data["followers_count"]
friends_count = all_data["friends_count"]
retweet_count = all_data["retweet_count"]
saveThis = str(time.time())+'::'+timestamp+'::'+username+'::'+user_id+'::'+tweet+'::'+followers_count+'::'+friends_count+'::'+retweet_count+'::'+location
saveFile = open('clean2.txt','a')
saveFile.write(saveThis)
saveFile.write('\n')
saveFile.close
return True
except BaseException, e:
print 'failed on data,',str(e)
time.sleep(5)
def on_error(self, status):
print status
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["tweepy"])#topic
The reason it fails on all_data["location"] is that tweets don't have such a property: https://dev.twitter.com/overview/api/tweets
same with friends_count, followers_count - they are properties of users, not tweets.
The code should not be failing on all_date["retweet_count"] as tweets have such a property.
P.S. please include the error message (even if you skip the full error trackback) when reporting errors. makes it's easier to help you, otherwise one has to guess what the error might be.

How to convert QueryDict to python dictionary

I am trying to post some data from Dojo to Django application. I use postData to post the data to the server
here is the code snippet
var csrftokenval = dojo.cookie('csrftoken');
var selectedmoid1 = tree.getSelectedItemId();
var loadURL = '/calerts/';
dojo.rawXhrPost({
url : loadURL,
headers : {'X-CSRFToken':csrftokenval},
postData: dojo.toJson({'selectedmoid':selectedmoid1,'previousval':previousVal}),
handleAs: "text",
load : function(data, ioArgs){
dojo.byId('content-main').innerHTML = data;
},
error : function(data, ioArgs){
}
});
In the Django views i get the data as
def calerts(request):
user = request.user
compId = int(request.session.get('USERCOMPANY_ID','-1'))
listCount = 25
print '0000000000000000000000000000000 ',request.POST
print 'post dictionary ::: ',request.POST.dict()
I know to get the dict value from querydict using dict() method however in my case the print is
post dictionary ::: {u'{"selectedmoid":"4","previousval":"4"}': u''}
i dont undersand where that final u'' comes from. Also i would like to retrieve the values of selectedmoid and previousval
Your sending the data as a raw JSON post, not a form-encoded one. So you should access request.body, not request.POST, and decode the JSON from there.

Is it possible to return an HttpResponse in django with text & a json object?

In my view function, I'd like to return a json object (data1) and some text/html (form). Is this possible?
Here is part of my views.py:
if request.is_ajax() and request.method == 'POST':
...
if form.is_valid():
answer = form.cleaned_data['answer'] # Answer extracted from form is also a string
a1 = ques1.correct_answer
if a1 == answer:
test1 = question_list.get(id=nextid)
form = AnswerForm(test1)
ques1 = question_list.filter(id=nextid) # Filter next question as <qs>
data1 = serializers.serialize("json",ques1) # Json-ize
# ********EDITED HERE **********
variables1 = Context({
'form' : form,
'q1' : data1,
})
#response = HttpResponse()
#response['data1'] = response.write(data1)
#response['form'] = response.write(form)
if nextid <= qsnlen:
return HttpResponse(variables1, mimetype="application/json")
#return HttpResponse(response)
else:
...
I'd like to send back both the form html and the ques1 json object. How can I do this? Thanks in advance.
Just put both pieces of data in a JSON container, one key with the form data and one with the HTML as a rendered string. In the browser, you can just pull both keys out & do your thing.
In your view:
form_json_data = get_form_json_data()
rendered_html = get_the_html()
return HttpResponse(json.dumps({
"formdata": form_json,
"html": rendered_html}),
content_type="application/json")
In js:
$.post(foo, postdata, function(data){
var formdata = data.formdata
var html = data.html;
$(".html-target").replaceWith(html);
do_whatever(formdata);
})
Use JsonResponse
from django.http import JsonResponse
response_data = {put your data into a dict}
return JsonResponse(response_data, status=201)
To do this with one response; you need to send the JSON as a plain text in the context of your template response (HTML).
If you need to send JSON as as a separate JSON object, with its own mime type, then you need to write two views; one that sends back the JSON as application/json and the other that sends back the form (HTML).
EDIT:
You are not returning JSON objects, but you are turning a dictionary that has two items of two different types. As I explained in the comments, in one request/response cycle; you can only return one response which has a specific mime type that is based on the content and how you want the browser to handle it. Most of the time the content type is 'text/html'.
In your scenario, if you want to return both the HTML (which is your form), and the JSON response (which is a string), you need to return HTML.
If you want to return JSON to Jquery as a JSON object; you need to detect the request type. In your front end (the templates), you will initiate two requests - one from the browser, which will return back the form. The other from jQuery, which will return the appropriate JSON object.
Here is a possible approach to this:
def foo(request):
if request.is_ajax():
ctx = dict()
ctx['hello'] = 'world'
return HttpResponse(json.dumps(ctx),content_type='application/json')
else:
return HttpResponse('hello world')