SOCIALACCOUNT_PROVIDERS = {
"google": {
"SCOPE": ["profile", "email"],
"AUTH_PARAMS": {
"access_type": "online",
},
},
"github": {
"SCOPE": [
"read:user", "user:email"
],
"AUTH_PARAMS": {
"allow_signup": "true", # 회원가입 허용
},
}
}
import requests
from urllib.parse import urlencode
from django.shortcuts import redirect
from coding_helper.settings import (
GOOGLE_OAUTH_CLIENT_ID,
GOOGLE_OAUTH_CALLBACK_URL,
GOOGLE_OAUTH_CLIENT_SECRET,
GITHUB_CLIENT_ID,
GITHUB_REDIRECT_URI,
GITHUB_CLIENT_SECRET,
)
def google_login(request):
google_oauth_url = "https://accounts.google.com/o/oauth2/v2/auth"
params = {
"response_type": "code",
"client_id": GOOGLE_OAUTH_CLIENT_ID,
"redirect_uri": GOOGLE_OAUTH_CALLBACK_URL,
"scope": "profile email",
"state": "state_parameter",
}
auth_url = f"{google_oauth_url}?{urlencode(params)}"
return redirect(auth_url)
class GoogleLoginCallback(APIView):
permission_classes = [AllowAny]
def get(self, request, *args, **kwargs):
code = request.GET.get("code")
# 인증 코드가 없으면 400 오류 반환
if code is None:
return Response({"error": "Authorization code not provided"}, status=400)
# Google OAuth2 토큰 엔드포인트
token_endpoint_url = "https://oauth2.googleapis.com/token"
# POST 요청을 Google의 토큰 엔드포인트로 보냄
response = (
url=token_endpoint_url,
data={
"code": code,
"client_id": GOOGLE_OAUTH_CLIENT_ID, # 실제 client_id로 교체
"client_secret": GOOGLE_OAUTH_CLIENT_SECRET, # 실제 client_secret로 교체
"redirect_uri": GOOGLE_OAUTH_CALLBACK_URL, # 실제 redirect_uri로 교체
"grant_type": "authorization_code",
},
)
# 응답이 JSON 형식인지 확인하고 처리
try:
response_dict = response.json()
access_token = response_dict["access_token"]
url = "https://www.googleapis.com/oauth2/v3/userinfo"
headers = {"Authorization": f"Bearer {access_token}"}
response = requests.get(url, headers=headers)
response_data = response.json()
email = response_data["email"]
id = email.split("@")[0]
username = f"05#{id}"
try:
user = User.objects.get(username=username)
print("가입된 사용자")
except User.DoesNotExist:
print("미가입된 사용자")
user = User.objects.create(
username=username,
email=f"{id}@social.com",
first_name="Anonymous",
nickname=id,
is_active=True,
is_social=True,
)
user.save()
user.social_login = True
user.save()
username = user.username
return Response({"username": username})
except requests.exceptions.RequestException as e:
return Response({"error": f"Request error: {str(e)}"}, status=400)
except ValueError:
return Response({"error": "Invalid response from Google"}, status=400)