def authorize(request): next = request.GET.get('next') gh = GitHub(client_id=settings.GITHUB_CLIENT_ID, client_secret=settings.GITHUB_CLIENT_SECRET) url = gh.authorize_url(state='thedoclub') response = HttpResponseRedirect(url) if next: response.set_cookie('oauth_next', next, domain=settings.SESSION_COOKIE_DOMAIN) return response
def callback(request): gh = GitHub(client_id=settings.GITHUB_CLIENT_ID, client_secret=settings.GITHUB_CLIENT_SECRET) code = request.GET['code'] state = request.GET['state'] try: access_token = gh.get_access_token(code, state) except github.ApiAuthError: return HttpResponseRedirect(reverse('oauth-authorize')) user, _ = GitHubUser.objects.get_or_create(access_token=access_token) for repo in user.repos.all(): repo.clear_languages() github_fetcher.delay(access_token=access_token) response = HttpResponseRedirect(reverse("oauth-status")) response.set_cookie('doclub_sessionid', user.secret_token, domain=settings.SESSION_COOKIE_DOMAIN) return response