Exemplo n.º 1
0
def oauth(request):
    # Get the code from URI callback on this endpoint ( ?code='XXXXXXXXX...')
    OAUTH_CODE = request.GET['code']

    if OAUTH_CODE != None:
        CLIENT_ID = os.environ.get('GOOGLE_CLIENT_ID')
        CLIENT_SECRET = os.environ.get('GOOGLE_CLIENT_SECRET')
        REDIRECT_URI = os.environ.get('REDIRECT_OAUTH2')

        # Your application requests an access token and refresh token from Google
        data = urlencode({
            'code': OAUTH_CODE,
            'client_id': CLIENT_ID,
            'client_secret': CLIENT_SECRET,
            'redirect_uri': REDIRECT_URI,
            'grant_type': 'authorization_code'
        })

        request = Request(url='https://www.googleapis.com/oauth2/v4/token',
                          data=data)
        request_open = requests.get(request)

        # Google returns access token, refresh token, and expiration of
        # access token
        response = request_open.read()
        request_open.close()
        tokens = json.loads(response)

        # print(tokens)

    return HttpResponse(OAUTH_CODE)
Exemplo n.º 2
0
def build_request(url, data=None, headers={}):
    headers["User-Agent"] = "Dynamsoft"
    return Request(url, data=data, headers=headers)