File size: 957 Bytes
9c5c050
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import requests
from . import settings

def validate_redirect(params):
    data = {
        'code': params["code"],
        'redirect_uri': settings.OAUTH_REDIRECT,
        'client_id': settings.GOOGLE_CLIENT_ID,
        'client_secret': settings.GOOGLE_CLIENT_SECRET,
        'scope': params["scope"],
        'grant_type': 'authorization_code'
    }

    google_auth_rq = requests.post("https://oauth2.googleapis.com/token", data=data, timeout=30)
    google_auth = google_auth_rq.json()
    
    try:
        headers = {
            'Authorization': google_auth["token_type"]+" "+google_auth["access_token"]
        }
    except Exception as e:
        print(e)
        print(google_auth_rq.content)
        return str(e)+" "+google_auth_rq.content
    google_userinfo_rq = requests.get("https://www.googleapis.com/oauth2/v2/userinfo", headers=headers, timeout=30)
    ret = google_userinfo_rq.json()
    ret["gid"] = ret.get("id","")
    return ret