Пример #1
0
def login():

    # phase 1 - obtain authorization URL
    oauth = OAuth1Session(OAUTH_CONSUMER_KEY, client_secret=OAUTH_CONSUMER_SECRET, callback_uri='oob')
    request_token = oauth.fetch_request_token(OAUTH_REQUEST_TOKEN_URL)
    authorization_url = oauth.authorization_url(OAUTH_AUTHORIZE_TOKEN_URL)
    if dbg:
        plugin.log.info('Authorization URL: {0}'.format(authorization_url))

    # phase 2 - compress URL and show it
    expires = (datetime.now() + timedelta(hours=1)).isoformat()
    puny_params = {'url': authorization_url, 'random': '1', 'expires': expires}
    resp_puny = rsession.get(PUNY_URL, params=puny_params)
    compressed_url = parseString(resp_puny.content).getElementsByTagName('ascii')[0].childNodes[0].nodeValue

    # Show qrcode along with URL
    import tempfile
    qrcodefile = tempfile.gettempdir() + '/' + 'meocloudQRCode.jpg'

    import pyqrcode
    qr_image = pyqrcode.MakeQRImage(compressed_url, rounding = 5, fg = "black", bg = "White", br = False)
    qr_image.save(qrcodefile)

    qrwindow = QRCodePopupWindow(language(30008), compressed_url, language(30009), qrcodefile)
    qrwindow.doModal()
    del qrwindow

    # phase 3 - obtain verifier
    verifier = common.getUserInputNumbers(language(30010))
    if dbg:
        plugin.log.info('Got verifier code: {0}'.format(verifier))

    if len(verifier) == 0:
        plugin.notify(msg=language(30015), title=language(30012), delay=5000)

    else:
        try:
            # phase 4 - obtain authenticated access token
            oauth = OAuth1Session(OAUTH_CONSUMER_KEY,
                          client_secret=OAUTH_CONSUMER_SECRET,
                          resource_owner_key=request_token['oauth_token'],
                          resource_owner_secret=request_token['oauth_token_secret'],
                          verifier=verifier)
            access_token = oauth.fetch_access_token(OAUTH_ACCESS_TOKEN_URL)
            storage['oauth_token_key'] = access_token['oauth_token']
            storage['oauth_token_secret'] = access_token['oauth_token_secret']

            # We need some user info now to store on the settings pane
            auth = OAuth1(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, storage['oauth_token_key'], storage['oauth_token_secret'])
            signer = oauthlib.oauth1.Client(client_key=OAUTH_CONSUMER_KEY,
                                    client_secret=OAUTH_CONSUMER_SECRET,
                                    resource_owner_key=storage['oauth_token_key'],
                                    resource_owner_secret=storage['oauth_token_secret'],
                                    signature_type=oauthlib.oauth1.SIGNATURE_TYPE_QUERY)
            resp_userinfo = rsession.get(API_USERINFO_URL, auth=auth)
            
            if dbg:
                plugin.log.info('Requesting %s' % resp_userinfo.url)

            if resp_userinfo.status_code == 200:
                api_res = resp_userinfo.json()
                settings.setSetting("settings.user.name", api_res['display_name'])
                settings.setSetting("settings.user.email", api_res['email'])
            else:
                # TODO
                # This may leave some inconsistency if we have a user but could not get its info
                # Maybe we should clean up the tokens if we don't have user info?
                if dbg:
                    plugin.log.error(resp_userinfo)
                    plugin.log.error(resp_userinfo.content)
                plugin.notify(msg=language(30015), title=language(30012), delay=5000)

            url = plugin.url_for('index')
            plugin.redirect(url)

        except:
            plugin.notify(msg=language(30015), title=language(30012), delay=5000)