def oauth2_callback(request):
if '<STR_LIT:error>' in request.GET:<EOL><INDENT>reason = request.GET.get(<EOL>'<STR_LIT>', request.GET.get('<STR_LIT:error>', '<STR_LIT>'))<EOL>reason = html.escape(reason)<EOL>return http.HttpResponseBadRequest(<EOL>'<STR_LIT>'.format(reason))<EOL><DEDENT>try:<EOL><INDENT>encoded_state = request.GET['<STR_LIT:state>']<EOL>code = request.GET['<STR_LIT:code>']<EOL><DEDENT>except KeyError:<EOL><INDENT>return http.HttpResponseBadRequest(<EOL>'<STR_LIT>')<EOL><DEDENT>try:<EOL><INDENT>server_csrf = request.session[_CSRF_KEY]<EOL><DEDENT>except KeyError:<EOL><INDENT>return http.HttpResponseBadRequest(<EOL>'<STR_LIT>')<EOL><DEDENT>try:<EOL><INDENT>state = json.loads(encoded_state)<EOL>client_csrf = state['<STR_LIT>']<EOL>return_url = state['<STR_LIT>']<EOL><DEDENT>except (ValueError, KeyError):<EOL><INDENT>return http.HttpResponseBadRequest('<STR_LIT>')<EOL><DEDENT>if client_csrf != server_csrf:<EOL><INDENT>return http.HttpResponseBadRequest('<STR_LIT>')<EOL><DEDENT>flow = _get_flow_for_token(client_csrf, request)<EOL>if not flow:<EOL><INDENT>return http.HttpResponseBadRequest('<STR_LIT>')<EOL><DEDENT>try:<EOL><INDENT>credentials = flow.step2_exchange(code)<EOL><DEDENT>except client.FlowExchangeError as exchange_error:<EOL><INDENT>return http.HttpResponseBadRequest(<EOL>'<STR_LIT>'.format(exchange_error))<EOL><DEDENT>get_storage(request).put(credentials)<EOL>signals.oauth2_authorized.send(sender=signals.oauth2_authorized,<EOL>request=request, credentials=credentials)<EOL>return shortcuts.redirect(return_url)<EOL>
View that handles the user's return from OAuth2 provider.

    This view verifies the CSRF state and OAuth authorization code, and on
    success stores the credentials obtained in the storage provider,
    and redirects to the return_url specified in the authorize view and
    stored in the session.

    Args:
        request: Django request.

    Returns:
         A redirect response back to the return_url.