def setup_nokia(options, config):
    """ Setup the Nokia Health API
    """
    if options.key is None:
        print(
            "To set a connection with Nokia Health you must have registered an application at https://developer.health.nokia.com/en/partner/add ."
        )
        options.key = input('Please enter the consumer key: ')

    if options.secret is None:
        options.secret = input('Please enter the consumer secret: ')

    auth = nokia.NokiaAuth(options.key, options.secret)
    authorize_url = auth.get_authorize_url()
    print("Go to %s allow the app and authorize the application." %
          authorize_url)
    oauth_verifier = input('Please enter your oauth_verifier: ')
    creds = auth.get_credentials(oauth_verifier)

    if not config.has_section('nokia'):
        config.add_section('nokia')

    config.set('nokia', 'consumer_key', options.key)
    config.set('nokia', 'consumer_secret', options.secret)
    config.set('nokia', 'access_token', creds.access_token)
    config.set('nokia', 'access_token_secret', creds.access_token_secret)
    config.set('nokia', 'user_id', creds.user_id)
示例#2
0
    def get_auth_client(self, profile: str):
        """Get a new auth client."""
        flow = self.hass.data[DATA_FLOW_IMPL][profile]
        client_id = flow[const.CLIENT_ID]
        client_secret = flow[const.CLIENT_SECRET]
        base_url = flow[const.BASE_URL].rstrip('/')
        callback_path = auth_callback_path(profile).lstrip('/')
        auth_callback_path_str = const.AUTH_CALLBACK_PATH.lstrip('/')

        # Clean up the base url in case the user configured a bit too much.
        if base_url.endswith(callback_path):
            base_url = base_url[:-len(callback_path)]
        if base_url.endswith(auth_callback_path_str):
            base_url = base_url[:-len(auth_callback_path_str)]

        callback_uri = '{}/{}'.format(
            base_url.rstrip('/'),
            callback_path.lstrip('/')
        )

        return nokia.NokiaAuth(
            client_id,
            client_secret,
            callback_uri,
            scope=','.join(['user.info', 'user.metrics', 'user.activity'])
        )
    def get_auth_client(self, profile: str):
        """Get a new auth client."""
        flow = self.hass.data[DATA_FLOW_IMPL]
        client_id = flow[const.CLIENT_ID]
        client_secret = flow[const.CLIENT_SECRET]
        base_url = flow[const.BASE_URL].rstrip("/")

        callback_uri = "{}/{}?flow_id={}&profile={}".format(
            base_url.rstrip("/"),
            const.AUTH_CALLBACK_PATH.lstrip("/"),
            self.flow_id,
            profile,
        )

        return nokia.NokiaAuth(
            client_id,
            client_secret,
            callback_uri,
            scope=",".join(["user.info", "user.metrics", "user.activity"]),
        )
def setup_nokia( options, config ):
    """ Setup the Nokia Health API
    """
    global nokia_auth_code
    if options.key is None:
        print("To set a connection with Nokia Health you must have registered an application at https://account.withings.com/partner/add_oauth2 .")
        options.key = input('Please enter the client id: ')

    if options.secret is None:
        options.secret = input('Please enter the consumer secret: ')

    if options.callback is None:
        options.callback = input('Please enter the callback url known by Nokia: ')

    if options.auth_serv is None:
        auth_serv_resp = input('Spin up HTTP server to automate authorization? [y/n] : ')
        if auth_serv_resp is 'y':
            options.auth_serv = True
        else:
            options.auth_serv = False

    if options.auth_serv:
        callback_parts = urlparse(options.callback)
        httpd_port = callback_parts.port
        httpd_ssl = callback_parts.scheme == 'https'
        if not httpd_port:
            httpd_port = 443 if httpd_ssl else 80
        certfile = None
        if httpd_ssl and not certfile:
            print("Your callback url is over https, but no certificate is present.")
            print("Change the scheme to http (also over at Nokia!) or specify a certfile above.")
            exit(0)

    auth = nokia.NokiaAuth(options.key, options.secret, options.callback)
    authorize_url = auth.get_authorize_url()
    print("Visit: %s\nand select your user and click \"Allow this app\"." % authorize_url)

    if options.auth_serv:
        httpd = HTTPServer(('', httpd_port), AuthorizationRepsponseHandler)
        if httpd_ssl:
            httpd.socket = ssl.wrap_socket(httpd.socket, certfile=certfile, server_side=True)
        httpd.socket.settimeout(100)
        httpd.handle_request()
    else:
        print("After redirection to your callback url find the authorization code in the url.")
        print("Example: https://your_original_callback?code=abcdef01234&state=XFZ")
        print("         example value to copy: abcdef01234")
        nokia_auth_code = input('Please enter the authorization code: ')
    creds = auth.get_credentials(nokia_auth_code)

    if not config.has_section('nokia'):
        config.add_section('nokia')

    config.set('nokia', 'consumer_key', options.key)
    config.set('nokia', 'consumer_secret', options.secret)
    config.set('nokia', 'callback_uri', options.callback)
    config.set('nokia', 'access_token', creds.access_token)
    config.set('nokia', 'token_expiry', creds.token_expiry)
    config.set('nokia', 'token_type', creds.token_type)
    config.set('nokia', 'refresh_token', creds.refresh_token)
    config.set('nokia', 'user_id', creds.user_id)