예제 #1
0
def youku_oauth2callback_view(request):
    """
    将认证后的返回的授权码,用于获取access token
    :param request:
    :return:
    """
    # 获取 http://127.0.0.1:8000/oauth2/youku_authenticate 授权后返回的auth code
    code = request.GET.get('code')
    code = code.strip()
    client = Client(CLIENT_ID,
                    CLIENT_SECRET,
                    site='https://openapi.youku.com/v2',
                    authorize_url=YOUKU_AUTHORIZE_URL,
                    token_url=YOUKU_TOKEN_URL)

    access_token = client.auth_code.get_token(
        code,
        redirect_uri=REDIRECT_URL,
        #cert=certifi.where(),
        verify=False)

    access_token_string = access_token.token
    django_settings.set('String', 'youku_access_token', access_token_string,
                        False)

    # 由youku认证页面转来的,没有request.META.HTTP_REFERER变量
    # 所以无法设置其返回前面的页面
    # return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
    return render_to_response('result.html', {
        'text':
        "Access Token: %s , 已保存到django setting 数据库里" % access_token_string
    })
예제 #2
0
파일: client.py 프로젝트: bt404/shaDou
 def __init__(self, key, secret, redirect='', scope=''):
     self.redirect_uri = redirect
     self.scope = scope
     self.client = Client(key, secret,
                          site=self.API_HOST,
                          authorize_url=self.AUTHORIZE_URL,
                          token_url=self.TOKEN_URL)
     self.access_token = AccessToken(self.client, '')
예제 #3
0
파일: stash.py 프로젝트: flowbish/stashpy
 def __init__(self):
     self.client = Client(self.client_id, self.client_secret, site='https://www.deviantart.com',
             authorize_url='/oauth2/authorize', token_url='/oauth2/access_token')
     self.auth_code = None
     self.access_token = None
     self.refresh_token = None
     self.expires = None
     self.redirect_uri = None
     self.list_cursor = None
     self.items = []
     self.connected = False
     self = list()
예제 #4
0
    def GET(self):
        code = web.input().get('code')
        client = Client(
            KEY,
            SECRET,
            site='https://api.douban.com',
            authorize_url='https://www.douban.com/service/auth2/auth',
            token_url='https://www.douban.com/service/auth2/token')

        #拒绝
        error = web.input().get('error')
        if error == 'access_denied':
            raise web.seeother(r'/')

        if not code:
            try:
                authorize_url = client.auth_code.authorize_url(
                    redirect_uri=CALLBACK,
                    scope='shuo_basic_w,douban_basic_common')
                web.seeother(authorize_url)
            except:
                raise web.seeother(r'/')
        else:
            try:
                #get code
                access_token = client.auth_code.get_token(
                    code, redirect_uri=CALLBACK)
                print 'get @me info'
                ret = access_token.get('/v2/user/~me')
                #检查有否已经存在
                #name = ret.parsed['name']+'('+ret.parsed['id']+')'
                name = ret.parsed['id']
            except:
                raise web.seeother(r'/')
            #存在,登录
            if model.isuser(name, 'douban') == 1:
                session.login = 1
                session.username = name
                #Login_time
                model.update_logintime(local_time(), name)
                raise web.seeother(r'/')
            else:
                #不存在,注册,登录返回
                #注册
                model.input_user(name, 'douban')

                if model.isuser(name, 'douban') == 1:
                    session.login = 1
                    session.username = name
                    raise web.seeother(r'/my')
                else:
                    return jjenv.get_template("register.html").render(
                        nickname='', title='Register', tips="")
예제 #5
0
async def github_login(req, resp):
    '''github login'''
    global client
    global access_token
    client = Client(secrets.KEY,
                    secrets.SECRET,
                    site=config.SITE,
                    authorize_url=config.AUTHORIZE_URL,
                    token_url=config.TOKEN_URL)
    authorize_url = client.auth_code.authorize_url(
        redirect_uri=config.CALLBACK, scope=config.SCOPE)
    response = 'Go to the following link in your browser:\n\n'
    response += '\t' + authorize_url + '\n\n'
    resp.text = banner(ip, time.time()) + response
 def configure_authorization_flow(
         self,
         client_id,
         client_secret,
         redirect_uri,
         oauth_base_url='https://account-d.docusign.com',
         scope='signature'):
     self.redirect_uri = redirect_uri
     self.scope = scope
     self.oauth_client = Client(
         client_id,
         client_secret,
         site=self.host,
         authorize_url=(oauth_base_url + self.AUTHORIZE_PATH),
         token_url=(oauth_base_url + self.TOKEN_PATH))
예제 #7
0
def youku_authenticate_view(request):
    """
    先通过django_settings,在数据库中查找之前获取到的access token
    如果本地没有,就到优酷上去获取,会跳转到youku的认证页面
    :param request:
    :return:
    """
    client = Client(CLIENT_ID,
                    CLIENT_SECRET,
                    site='https://openapi.youku.com/v2',
                    authorize_url=YOUKU_AUTHORIZE_URL,
                    token_url=YOUKU_TOKEN_URL)

    authorize_url = client.auth_code.authorize_url(redirect_uri=REDIRECT_URL,
                                                   scope=SCOPE)
    return HttpResponseRedirect(authorize_url)
예제 #8
0
async def github_callback(req, resp):
    '''github oauth2 callback'''
    code = req.params['code']
    code = code.strip()
    client = Client(secrets.KEY,
                    secrets.SECRET,
                    site=config.SITE,
                    authorize_url=config.AUTHORIZE_URL,
                    token_url=config.TOKEN_URL)
    access_token = client.auth_code.get_token(code,
                                              redirect_uri=config.CALLBACK,
                                              parse='query')
    data = access_token.get('/user')
    session_id = gen_session()
    cache_write('sessions:' + session_id,
                access_token.headers['Authorization'])
    cookie = '<br>run this to save a cookie & verify your login:<br><textarea style="margin: 0px; width: 569px; height: 90px;">echo "oneliner.sh FALSE / FALSE 0 session ' + session_id + '" | sed -e "s/\\s/\\t/g" > ~/.oneliner.sh.cookie.txt && curl -L -b ~/.oneliner.sh.cookie.txt oneliner.sh/me</textarea><br>'
    resp.text = '<html>Welcome, ' + data.parsed[
        'login'] + '!<br><br>' + cookie + '</html>'
예제 #9
0
from pyoauth2 import Client

KEY = ''
SECRET = ''
CALLBACK = ''

client = Client(KEY,
                SECRET,
                site='https://graph.qq.com',
                authorize_url='https://graph.qq.com/oauth2.0/authorize',
                token_url='https://graph.qq.com/oauth2.0/token')

print '-' * 80
authorize_url = client.auth_code.authorize_url(
    redirect_uri=CALLBACK, scope='get_user_info,list_album,upload_pic,do_like')
print 'Go to the following link in your browser:'
print authorize_url
print '-' * 80

code = raw_input(
    'Enter the verification code and hit ENTER when you\'re done:')
code = code.strip()
access_token = client.auth_code.get_token(code,
                                          redirect_uri=CALLBACK,
                                          parse='query')

print 'token', access_token.headers
print access_token.expires_at
예제 #10
0
    def GET(self):
        code = web.input().get('code')
        client = Client(
            KEY_Q,
            SECRET_Q,
            site='https://graph.qq.com',
            authorize_url='https://graph.qq.com/oauth2.0/authorize',
            token_url='https://graph.qq.com/oauth2.0/token')

        if not code:
            try:
                authorize_url = client.auth_code.authorize_url(
                    redirect_uri=CALLBACK_Q, scope='get_user_info')
                web.seeother(authorize_url)
            except:
                raise web.seeother(r'/')
        else:
            try:
                access_token = client.auth_code.get_token(
                    code, redirect_uri=CALLBACK_Q, parse='query')
                url = "https://graph.qq.com/oauth2.0/me?access_token=%s" % access_token.token
                opener = URLOpener()
                result = opener.open(url)
                r_code, content = result.code, result.content
            except:
                raise web.seeother(r'/')
            if content.find('error') == 0:
                raise web.seeother(r'/')

            if content.find("callback") == 0:
                lp = content.find('(')
                rp = content.find(')')
                con = content[lp + 1:rp - 1]

                try:
                    data = json.loads(con)

                    openid = data['openid']
                    clientid = data['client_id']

                    url2 = "https://graph.qq.com/user/get_user_info?oauth_consumer_key=%s&access_token=%s&openid=%s&format=json" % (
                        KEY_Q, access_token.token, openid)

                    r2 = opener.open(url2)
                    content2 = r2.content
                    data2 = json.loads(content2)
                    ret = data2['ret']
                except:
                    raise web.seeother(r'/')
                if ret == 0:
                    #name = data2['nickname']+'('+openid[2:6]+')'
                    name = openid[2:6]
                    #存在,登录
                    if model.isuser(name, 'qq') == 1:
                        session.login = 1
                        session.username = name
                        model.update_logintime(local_time(), name)
                        raise web.seeother(r'/')
                    else:
                        #不存在,注册,登录返回
                        #注册
                        model.input_user(name, 'qq')
                        if model.isuser(name, 'qq') == 1:
                            session.login = 1
                            session.username = name
                            raise web.seeother(r'/my')
                        else:
                            return jjenv.get_template("register.html").render(
                                nickname='', title='Register', tips="")
                else:
                    raise web.seeother(r'/')
            else:
                raise web.seeother(r'/')
from pyoauth2 import Client
from pyoauth2 import AccessToken

KEY = ''
SECRET = ''
CALLBACK = ''

user_email = ''
user_password = ''

client = Client(KEY,
                SECRET,
                site='https://api.douban.com',
                authorize_url='https://www.douban.com/service/auth2/auth',
                token_url='https://www.douban.com/service/auth2/token')

access_token = client.password.get_token(user_email, user_password)

print '-' * 80
ret = access_token.get('/v2/user/~me')
print ret.parsed
예제 #12
0
from pyoauth2 import Client

KEY = ''
SECRET = ''
CALLBACK = ''

client = Client(KEY, SECRET,
                site='https://api.github.com',
                authorize_url='https://github.com/login/oauth/authorize',
                token_url='https://github.com/login/oauth/access_token')

print '-' * 80
authorize_url = client.auth_code.authorize_url(redirect_uri=CALLBACK, scope='user,public_repo')
print 'Go to the following link in your browser:'
print authorize_url
print '-' * 80

code = raw_input('Enter the verification code and hit ENTER when you\'re done:')
code = code.strip()
access_token = client.auth_code.get_token(code, redirect_uri=CALLBACK, parse='query')
print 'token', access_token.headers

print '-' * 80
print 'get user info'
ret = access_token.get('/user')
print ret.parsed

print '-' * 80
print 'create a repos'
ret = access_token.post('/user/repos', name='test_repo', headers={'content-type': 'application/json'})
print ret.parsed
예제 #13
0
from pyoauth2 import Client

CLIENT_ID = ''
CLIENT_SECRET = ''
REDIRECT_URL = ''
SCOPE = 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email'

client = Client(CLIENT_ID,
                CLIENT_SECRET,
                site='https://www.googleapis.com/oauth2/v1',
                authorize_url='https://accounts.google.com/o/oauth2/auth',
                token_url='https://accounts.google.com/o/oauth2/token')

print '-' * 80
authorize_url = client.auth_code.authorize_url(redirect_uri=REDIRECT_URL,
                                               scope=SCOPE)
print 'Go to the following link in your browser:'
print authorize_url

code = raw_input(
    'Enter the verification code and hit ENTER when you\'re done:')
code = code.strip()
access_token = client.auth_code.get_token(code, redirect_uri=REDIRECT_URL)
print 'token', access_token.headers

print '-' * 80
print 'get user info'
ret = access_token.get('/userinfo')
print ret.parsed
예제 #14
0
from pyoauth2 import Client

KEY = ''
SECRET = ''
CALLBACK = ''

client = Client(KEY,
                SECRET,
                site='https://api.box.com/2.0',
                authorize_url='https://api.box.com/oauth2/authorize',
                token_url='https://api.box.com/oauth2/token')

print '-' * 80
authorize_url = client.auth_code.authorize_url(redirect_uri=CALLBACK)
print 'Go to the following link in your browser:'
print authorize_url
print '-' * 80

code = raw_input(
    'Enter the verification code and hit ENTER when you\'re done:')
code = code.strip()
access_token = client.auth_code.get_token(code, redirect_uri=CALLBACK)
print 'token', access_token.headers

print '-' * 80
ret = access_token.get('/folders/0')
print ret.parsed
예제 #15
0
__FILENAME__ = box
from pyoauth2 import Client

KEY = ''
SECRET = ''
CALLBACK = ''

client = Client(KEY, SECRET, 
                site='https://api.box.com/2.0', 
                authorize_url='https://api.box.com/oauth2/authorize',
                token_url='https://api.box.com/oauth2/token')

print '-' * 80
authorize_url = client.auth_code.authorize_url(redirect_uri=CALLBACK)
print 'Go to the following link in your browser:'
print authorize_url
print '-' * 80

code = raw_input('Enter the verification code and hit ENTER when you\'re done:')
code = code.strip()
access_token = client.auth_code.get_token(code, redirect_uri=CALLBACK)
print 'token', access_token.headers

print '-' * 80
ret = access_token.get('/folders/0')
print ret.parsed

########NEW FILE########
__FILENAME__ = douban
from pyoauth2 import Client
예제 #16
0
CLIENT_ID = client_id = settings.YOUKU_CLIENT_ID
CLIENT_SECRET = settings.YOUKU_CLIENT_SECRET
REDIRECT_URL = 'http://127.0.0.1:8000'
SCOPE = ''

youku_authorize_url = 'https://openapi.youku.com/v2/oauth2/authorize'
youku_token_url = 'https://openapi.youku.com/v2/oauth2/token'

#SCOPE = ['https://www.googleapis.com/auth/userinfo.profile',
#          'https://www.googleapis.com/auth/userinfo.email',]
#SCOPE = ' '.join(SCOPE)

client = Client(CLIENT_ID,
                CLIENT_SECRET,
                site='https://openapi.youku.com/v2',
                authorize_url=youku_authorize_url,
                token_url=youku_token_url)

print '-' * 80
authorize_url = client.auth_code.authorize_url(redirect_uri=REDIRECT_URL,
                                               scope=SCOPE)
print 'Go to the following link in your browser:'
print authorize_url

code = raw_input(
    'Enter the verification code and hit ENTER when you\'re done:')
code = code.strip()
access_token = client.auth_code.get_token(code, redirect_uri=REDIRECT_URL)
#youku的access_token的expires_in时间是2592000秒,也就是30天
print 'token', access_token.headers
예제 #17
0
from pyoauth2 import Client

CLIENT_ID = ''
CLIENT_SECRET = ''
REDIRECT_URL = ''

client = Client(CLIENT_ID, CLIENT_SECRET, 
                site='https://api.instagram.com',
                authorize_url='/oauth/authorize',
                token_url='/oauth/access_token')

authorize_url = client.auth_code.authorize_url(redirect_uri=REDIRECT_URL)

print 'Go to the following link in your browser:'
print authorize_url
print '-' * 80

code = raw_input('Enter the verification code and hit ENTER when you\'re done:')
code = code.strip()
access_token = client.auth_code.get_token(code, redirect_uri=REDIRECT_URL)

print 'token', access_token.headers
print 'params', access_token.params
예제 #18
0
from pyoauth2 import Client
from pyoauth2 import AccessToken

KEY = ''
SECRET = ''
CALLBACK = ''

client = Client(KEY,
                SECRET,
                site='https://api.weibo.com',
                authorize_url='/oauth2/authorize',
                token_url='/oauth2/access_token')

code = raw_input(
    'Enter the verification code and hit ENTER when you\'re done:')
client.auth_code.get_token(code, redirect_uri=CALLBACK)
access_token = AccessToken(client, code)

print access_token.get('/2/statuses/public_timeline.json',
                       access_token=access_token.token).parsed
예제 #19
0
# encoding: utf-8
from pyoauth2 import Client
from accounts.oauth2_configs import *
from accounts.models import Profiles
from django.shortcuts import redirect
from django.template.response import TemplateResponse as render
from django.http import HttpResponseRedirect
from django.contrib.auth import login, logout, authenticate
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.contrib import messages

client = Client(client_id=weibo_key,
                client_secret=weibo_secret,
                site=weibo_site_url,
                authorize_url=weibo_authorize_url,
                token_url=weibo_access_token_url)


def login_view(request):
    ### use mock login for test
    mock_login = request.GET.get('mock')
    if mock_login:
        username = request.GET.get('username')
        password = request.GET.get('password')
        account = authenticate(username=username, password=password)
        login(request, account)
        profile = Profiles.objects.get(user=account)
        request.session['profile'] = profile
        return redirect('settings_view')
    ### end mock login