Exemplo n.º 1
0
def request_access_token(code, oauth, redirect_uri=None):
    '''
    return access token as object: {"access_token":"your-access-token","expires_in":12345678,"uid":1234}, expires_in is standard unix-epoch-time
    '''

    client = APIClient(apitype='oauth2', format=None)
    redirect = redirect_uri if redirect_uri else oauth.app_callback
    if not redirect:
        raise APIError('21305', 'Parameter absent: redirect_uri', 'OAuth2 request')
    r = client.access_token(
        client_id=oauth.app_key,
        client_secret=oauth.app_secret,
        redirect_uri=redirect,
        code=code, grant_type='authorization_code',
        _method='POST'
    )

    current = int(time.time())
    expires = r.expires_in + current
    remind_in = r.get('remind_in', None)
    if remind_in:
        rtime = int(remind_in) + current
        if rtime < expires:
            expires = rtime
    jo = JsonObject(access_token=r.access_token, expires_in=expires)
    uid = r.get('uid', None)
    if uid:
        jo.uid = uid
    return jo
Exemplo n.º 2
0
def _get_priority_data(access_token):
    if not access_token:
        return {'access_token': None, 'expires_in': None, 'uid': None}
    client = APIClient('', '')
    client.access_token = access_token
    try:
        res = client.get_token_info(access_token)
        expire_time = res.get('expire_in', '') + res.get('create_at', '')
        expires_in = datetime.datetime.fromtimestamp(expire_time)
        uid = res.get('uid')
    except:
        return None
    return {'access_token': access_token, 'expires_in': expires_in, 'uid': uid}