장고에서 깃허브를 통한 로그인 연동 구현시 발생 에러 질문

조회수 581회

로그인 화면과 회원가입화면에서 깃헙 로그인을 클릭하면 "NOT NULL constrain failed: users_user.username"아라고 출력됩니다.

깃헙에 가서 제 이메일을 public으로 설정했는데도 에러 메시지에 이메일이 none으로 나오더라구요,, 혹시 이유를 아시는 분 계실까요

제 이메일 공개로 설정했는데, 혹시 방법이 잘못되었을까요? settings -> Emails로 가서 Visible in emails로 설정했습니다.

def github_login(request):
    client_id = os.environ.get("GH_ID")
    redirect_uri = "http://127.0.0.1:9000/users/login/github/callback"
    return redirect(
        f"https://github.com/login/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&scope=read:user"
    )


class GithubException(Exception):
    pass


def github_callback(request):
    try:
        client_id = os.environ.get("GH_ID")
        client_secret = os.environ.get("GH_SECRET")
        code = request.GET.get("code", None)
        if code is not None:
            result = requests.post(
                f"https://github.com/login/oauth/access_token?client_id={client_id}&client_secret={client_secret}&code={code}",
                headers={"Accept": "application/json"},
            )
            result_json = result.json()
            error = result_json.get("error", None)
            if error is not None:  # 에러가 있다면,
                raise GithubException()
            else:
                access_token = result_json.get("access_token")
                profile_request = requests.get(
                    f"https://api.github.com/user",
                    headers={
                        "Authorization": f"token {access_token}",
                        "Accept": "application/json",
                    },
                )
                profile_json = profile_request.json()
                username = profile_json.get("login", None)
                if username is not None:
                    name = profile_json.get("name")
                    if name is None:
                        name = "None"
                    email = profile_json.get("email")
                    if email is None:
                        email = "None"
                    bio = profile_json.get("bio")
                    if bio is None:
                        bio = "None"
                    try:
                        user = models.User.objects.get(email=email)
                        if user.login_method != models.User.LOGIN_GITHUB:
                            raise GithubException()
                    except models.User.DoesNotExist:
                        user = models.User.objects.create(
                            email=email,
                            first_name=name,
                            username=email,
                            bio=bio,
                            login_method=models.User.LOGIN_GITHUB,
                            email_verified=True,
                        )
                        user.set_unusable_password()
                        user.save()
                    login(request, user)  # 로그인 시킴
                    return redirect(reverse("core:home"))

                else:
                    raise GithubException()
                # print(api_request.json())
        else:
            raise GithubException()
    except GithubException:
        return redirect(reverse("users:login"))

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)