def token_retrieve(request): if 'error' in request.GET: return HttpResponseServerError( 'Failed to create Canvas API oauth token: ' \ + request.GET['error']) oauth_code = request.GET.get('code') oauth_state = request.GET.get('state') if oauth_state != request.session['CANVAS_API_OAUTH_INITIAL_STATE']: return HttpResponseBadRequest("Oauth state mismatch") client_cred = get_client_credentials(request) # this needs to be the same url as in the token_init view because # canvas uses it for validation purposes; not actually used for redirection redirect_uri = absolute_uri(request, 'token_retrieve') token_retrieve_params = { 'client_id': client_cred['client_id'], 'client_secret': client_cred['client_secret'], 'code': oauth_code, 'redirect_uri': redirect_uri, } token_retrieve_uri = canvas_uri(request, '/login/oauth2/token') try: token = token_request(token_retrieve_uri, token_retrieve_params) except Exception, e: return HttpResponseServerError("Failed to fetch generate token: " \ + str(e))
def get_facebook_access_token(request): if 'error' in request.GET: log_exception( 'Error in get_facebook_access_token', extra={'error': request.GET.get('error'), 'error_reason': request.GET.get('error_reason'), 'error_description': request.GET.get('error_description')}) raise FacebookError(request.GET['error']) if not 'code' in request.GET: raise BadRequestError code = request.GET.get('code', '') if not code: raise Exception params = { 'client_id': settings.FACEBOOK_APP_ID, 'client_secret': settings.FACEBOOK_SECRET_KEY, 'redirect_uri': absolute_uri(request.path, request), 'code': code, } response = urlopen('https://graph.facebook.com/oauth/access_token?' + urlencode(params)).read() return parse_qs(response)['access_token'][-1]
def request_facebook_permissions(request, permissions, redirect_url): params = { 'client_id': settings.FACEBOOK_APP_ID, 'redirect_uri': absolute_uri(redirect_url, request), 'scope': ','.join(permissions), } return HttpResponseRedirect('https://graph.facebook.com/oauth/authorize?' + urlencode(params))
def get_content(state): path = state.data_dir + time.strftime('%Y%m%d-%H:%M:%S', time.gmtime(time.time())) dom = lxml.html.fromstring(state.response.text) childs = dom.xpath(utils.xpath_for_class(state.element) + '//h3/a/@href') if len(childs) > 0: os.makedirs(path) logging.info("Saving page content in %s/", path) utils.save_page_in(path, "main.html", state.response.text) absolute_uris = [utils.absolute_uri(state.uri, u) for u in childs] return [(process_pages, state, path, absolute_uris)] return [(sleep, state)]
def token_init(request): # generate & store a random 'state' value that can be validated in the next step state = str(uuid4()) request.session['CANVAS_API_OAUTH_INITIAL_STATE'] = state redirect_uri = absolute_uri(request, 'token_retrieve') client_cred = get_client_credentials(request) # params to generate the user's api token token_gen_params = { 'client_id': client_cred['client_id'], 'response_type': 'code', 'redirect_uri': redirect_uri, 'state': state, } # redirect to canvas to initiate the token creation token_gen_uri = canvas_uri(request, '/login/oauth2/auth', token_gen_params) return redirect(token_gen_uri)
def oauth_client(app, *args): if not OAUTH_APP_SETTINGS['oauth_callback']: OAUTH_APP_SETTINGS['oauth_callback'] = utils.absolute_uri('twitter', action='callback') return oauth.OAuthClient(app, OAUTH_APP_SETTINGS, *args)
def oauth_client(app, *args): if not OAUTH_APP_SETTINGS['oauth_callback']: OAUTH_APP_SETTINGS['oauth_callback'] = utils.absolute_uri( 'twitter', action='callback') return oauth.OAuthClient(app, OAUTH_APP_SETTINGS, *args)