Beispiel #1
0
def get_auth_qrcode():
    '''微信授权的URL'''
    # rep = oauth_service.authorize(
            # callback = url_for(
                # 'sso_authorized', next=request.args.get('next'),
                # uuid=uuid,_external = True
                # )
            # )
    uuid = request.args.get('uuid')
    if uuid is None:
        return 'uuid not found',400

    scope = ("snsapi_userinfo",)
    api = WeixinMpAPI(appid=app.config.get('WEIXIN_CONSUMER_KEY'),
            app_secret=app.config.get('MER_SECRET'),
            redirect_uri=url_for('sso_authorized',
                client_id=request.args.get('client_id'),
                response_type=request.args.get('response_type'),
                scope=request.args.get('scope'),
                redirect_uri=request.args.get('redirect_uri'),
                uuid= uuid,
                _external = True))
    authorize_url = api.get_authorize_url(scope=scope) +'#wechat_redirect'
    log.debug('authorize_url: %s',authorize_url)
    #创建二维码
    set_uuid_true_url(uuid,authorize_url)
    img=create_qrcode(url_for('true_url',uuid=uuid,_external=True))
    out=BytesIO()
    img.save(out, "PNG")

    #返回二维码字节流
    return out.getvalue(),200
Beispiel #2
0
def unauthorized():
    '''没有登录过,通过微信登录'''
    scope = ("snsapi_userinfo",)
    api = WeixinMpAPI(appid=app.config.get('WEIXIN_CONSUMER_KEY'),
            app_secret=app.config.get('WEIXIN_CONSUMER_SECRET'),
            redirect_uri=url_for('authorized',
                client_id=request.args.get('client_id'),
                response_type=request.args.get('response_type'),
                scope=request.args.get('scope'),
                redirect_uri=request.args.get('redirect_uri'),
                _external = True))
    authorize_url = api.get_authorize_url(scope=scope) +'#wechat_redirect'
    log.debug('authorize_url: %s',authorize_url)
    return redirect(authorize_url)
Beispiel #3
0
class WechatAuth():
    def __init__(self):
        self.PC_api = WeixinAPI(appid = PC_APP_ID,
                                app_secret   = PC_APP_SECRET,
                                redirect_uri = REDIRECT_URL)

        self.Mobile_api = WeixinMpAPI(appid = WX_APP_ID,
                                    app_secret   = WX_APP_SECRET,
                                    redirect_uri = REDIRECT_URL)

        self.pc_auth_url     = self.PC_api.get_authorize_url(scope=(PC_SCOPE,))
        self.mobile_auth_url = self.Mobile_api.get_authorize_url(scope=(WX_SCOPE,))


    def get_authorize_url(self, request):
        if checkMobile(request) == True:
            print self.mobile_auth_url
            return self.mobile_auth_url
        else:
            print self.pc_auth_url
            return self.pc_auth_url

    def get_user(self, request):
        try:
            code = ""
            if request.GET.has_key('code'):
                code = request.GET['code']
            else:
                return None

            if checkMobile(request) == True:
                auth_info = self.Mobile_api.exchange_code_for_access_token(code=code)
                userApi = WeixinMpAPI(access_token=auth_info['access_token'])
                user_info = userApi.user(openid=auth_info['openid'])
                print auth_info, user_info
                return user_info
            else:
                auth_info = self.PC_api.exchange_code_for_access_token(code=code)
                userApi = WeixinAPI(access_token=auth_info['access_token'])
                user_info = userApi.user(openid=auth_info['openid'])
                return user_info
        except Exception, e:
            printError(e)

        return None