Monday, June 29, 2015

Re: Canvas OAuth2 From Django View

First let me say that I'm probably making this harder than it needs to be, but I must be missing something in this process.

I'm trying to use the oauth2  process in the Canvas API, and I started working on this back in January, had other priorities that pulled me away from it, so I'm only now able to pursue working on this again.

I seem to be able to successfully send the original oauth2 request to the Canvas API, because I can get back a status code 200 from it.
But at that point everything seems to stop, so I'm not sure exactly what I'm doing wrong.

If after I send the successful response I get back from Canvas (and sending it the redirect_uri, etc.), back to my client, then I don't see any subsequent requests, coming back to the redirect_uri from Canvas, and don't understand why, or can I not normally see them as or when they happen?

If I don't send the successful response back to the client then I also don't see any subsequent requests coming to the view associated with the redirect_uri that I sent to Canvas and my application just fails with a status_code of "500".

So I think I'm probably not grasping exactly how a properly functioning "oauth2" process is supposed to work. 

I would appreciate any help anyone can offer in better explaining how the process works, how django view processing fits into it, and when to send and/or not send a response back to my client.

Thanks for the help.

Henry


On Wednesday, January 21, 2015 at 9:36:08 PM UTC-6, Collin Anderson wrote:
Hi Henry,

You need to send some kind of response back to the browser and the user. Maybe a message? or a maybe redirect to another page?

def final_step(request):
    code
= request.GET['code']
    r
= requests.post('https://<canvas-install-url>/login/oauth2/token', {'code': code, 'client_id': 'etc'}
    access_code
= r.json()['access_code']
   
return HttpResponse('Successfully Logged in! Here's your access_code: %s' % access_code)

Collin

On Sunday, January 18, 2015 at 7:01:12 PM UTC-5, Henry Versemann wrote:
Collin, thanks for the help. My django application as it is already has the "requests" library installed within it and I have already registered it with the API which I'm trying to authenticate to currently using that API's oauth2 web application flow, which is a three step process (see the oauth2 tab for the  Canvas API). I'm able so far to send a redirect-uri to the Canvas Oauth2 api and have it respond back to me with all of the other information I need to get my final access token from then. My application currently uses Python 2.7.8 and Django 1.7 and the piece of the process that I'm having the problem with is sending the final POST request back to the Canvas Oauth2 flow API. The response that up to now I've tried to return back from my django view  has been an HttpResponse which apparently does not allow the sending of a POST request. I've also taken a look at all of the shortcut functions that django offers as options to return back from django views, and none of them appear at least not obviously to offer any options for sending POST requests. Your response below seems to show how I might get the access token back from the final POST request which I need to make to the Canvas Oauth2 flow. Can you show me how to do that POST, and what I need my view to return, to avoid getting and error which says that my view did  not return a valid HttpResponse but instead returned "None" like I've gotten up until now?
Thanks again for the help.

Henry 

On Saturday, January 17, 2015 at 7:33:40 AM UTC-6, Collin Anderson wrote:
Hi,

Use urllib/urllib2 or requests to POST to other websites. Python can do it natively.

try:  # Python 3
   
from urllib import request as urllib_request
except ImportError:  # Python 2
   
import urllib2 as urllib_request
from django.utils.http import urlencode

def my_view(request):
    response
= urllib_request.urlopen('https://endpoint/', urlencode({'mytoken': '12345'}))
    data
= response.read()
   
# etc


or install requests, which is friendlier:

Collin

On Thursday, January 15, 2015 at 11:51:45 AM UTC-5, Henry Versemann wrote:
First let me say that I haven't done a lot of stuff with either Python or Django, but I think I understand most of the basics. 
I am trying to get an access token back from the OAuth2 Web Application Flow of the Canvas' LMS API ( https://canvas.instructure.com/doc/api/file.oauth.html ).
I have successfully sent the request in step 1 of the flow, and received back and extracted out of the response all of the data needed for step 3, which came back in step 2 of the flow.
So now in step 3 of the flow my problem is how to send a POST of the request needed, as described in step 3 of the flow, from a Django View.
I've not found anything definitive saying that I absolutely can't do a POST of a request, from a Django View, and have seen some  items which seem to indicate that I can do a POST from a view, but none of them seem to have detailed code examples or explanations of how to do it if it is possible.
So far I've tried several ways of sending the POST within a returned HttpResponse and have not been successful, and if I understand things correctly HttpResponse doesn't allow it. 
I have also looked at all of the other Django Shortcut Functions documentation, and none of them seem to offer any obvious way of POSTing a request either. 
Can someone please point me to a good example of how to do it if it is possible, and if it is not maybe point me to one or more examples of how I might be able to successfully go through the Canvas OAuth2 Web Application Flow using some other module or package, that I can integrate into my Django application?
Thanks for the help.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/35093689-74ef-4d52-b0df-8a1dd89bc60c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment