Example #1
0
def complete_github():
    try:
        code = request.args.get("code")
    except KeyError:
        raise  # TODO

    params = {"client_id": app.config["GITHUB_CLIENT_ID"], "client_secret": app.config["GITHUB_SECRET"], "code": code}
    token_resp = requests.post("https://github.com/login/oauth/access_token", data=params)
    token_params = parse_qs(token_resp.content)
    access_token = token_params["access_token"]

    user_resp = requests.get("https://api.github.com/user", data={"access_token": access_token})
    github_user = json.loads(user_resp.content)

    userid = u"github:%s" % github_user["login"]
    user = User.get(userid)
    if user is None:
        user = User(userid)
    user.name = github_user["name"]
    user.avatar_url = github_user["avatar_url"]
    user.profile_url = github_user["html_url"]
    user.save()

    login_user(user)

    try:
        next_url = session["signin_next_url"]
    except KeyError:
        next_url = url_for("home")
    else:
        del session["signin_next_url"]
    return redirect(next_url)
Example #2
0
def complete_linkedin():
    try:
        verifier = request.args.get('oauth_verifier')
    except KeyError:
        raise  # TODO

    request_token = session['linkedin_request_token']
    del session['linkedin_request_token']

    consumer = oauth2.Consumer(key=app.config['LINKEDIN_KEY'], secret=app.config['LINKEDIN_SECRET'])
    token = oauth2.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
    token.set_verifier(verifier)
    client = oauth2.Client(consumer, token)
    resp, content = client.request('https://api.linkedin.com/uas/oauth/accessToken', 'POST')
    assert resp.status == 200

    access_token = dict(parse_qsl(content))
    token = oauth2.Token(access_token['oauth_token'], access_token['oauth_token_secret'])
    client = oauth2.Client(consumer, token)
    resp, content = client.request('http://api.linkedin.com/v1/people/~:(id,formatted-name,picture-url,public-profile-url)', headers={
        'accept-language': 'en-US',
        'x-li-format': 'json',
    })
    assert resp.status == 200

    linkedin_user = json.loads(content)
    userid = u'linkedin:%s' % linkedin_user['id']
    user = User.get(userid)
    if user is None:
        user = User(userid)
    user.name = linkedin_user.get('formattedName')
    user.avatar_url = linkedin_user.get('pictureUrl')
    user.profile_url = linkedin_user.get('publicProfileUrl')
    user.save()

    login_user(user)

    try:
        next_url = session['signin_next_url']
    except KeyError:
        next_url = url_for('home')
    else:
        del session['signin_next_url']
    return redirect(next_url)
Example #3
0
def complete_twitter():
    try:
        verifier = request.args.get('oauth_verifier')
    except KeyError:
        raise  # TODO

    request_token = session['twitter_request_token']
    del session['twitter_request_token']

    consumer = oauth2.Consumer(key=app.config['TWITTER_KEY'], secret=app.config['TWITTER_SECRET'])
    token = oauth2.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
    token.set_verifier(verifier)
    client = oauth2.Client(consumer, token)
    resp, content = client.request('https://api.twitter.com/oauth/access_token', 'POST')
    assert resp.status == 200

    access_token = dict(parse_qsl(content))
    token = oauth2.Token(access_token['oauth_token'], access_token['oauth_token_secret'])
    client = oauth2.Client(consumer, token)
    resp, content = client.request('https://api.twitter.com/account/verify_credentials.json?skip_status=true')
    assert resp.status == 200

    twitter_user = json.loads(content)
    userid = u'twitter:%s' % twitter_user['id']
    user = User.get(userid)
    if user is None:
        user = User(userid)
    user.name = twitter_user['name']
    user.avatar_url = twitter_user['profile_image_url']
    user.profile_url = u'https://twitter.com/%s' % twitter_user['screen_name']
    user.save()

    login_user(user)

    try:
        next_url = session['signin_next_url']
    except KeyError:
        next_url = url_for('home')
    else:
        del session['signin_next_url']
    return redirect(next_url)
Example #4
0
def complete_github():
    try:
        code = request.args.get('code')
    except KeyError:
        raise  # TODO

    params = {
        'client_id': app.config['GITHUB_CLIENT_ID'],
        'client_secret': app.config['GITHUB_SECRET'],
        'code': code,
    }
    token_resp = requests.post('https://github.com/login/oauth/access_token', data=params)
    token_params = parse_qs(token_resp.content)
    access_token = token_params['access_token']

    user_resp = requests.get('https://api.github.com/user', data={'access_token': access_token})
    github_user = json.loads(user_resp.content)

    userid = u"github:%s" % github_user['login']
    user = User.get(userid)
    if user is None:
        user = User(userid)
    user.name = github_user.get('name') or github_user['login']
    user.avatar_url = github_user['avatar_url']
    user.profile_url = github_user['html_url']
    user.save()

    login_user(user)

    try:
        next_url = session['signin_next_url']
    except KeyError:
        next_url = url_for('home')
    else:
        del session['signin_next_url']
    return redirect(next_url)