### Get Authorization URL Source: https://github.com/yifangyun/fangcloud-python-sdk/blob/master/README.md Generates the authorization URL required for the OAuth 2.0 flow. Ensure YfyInit.init_system has been called prior to this. ```python from fangcloud.oauth import FangcloudOAuth2FlowBase from fangcloud.yifangyun import YfyInit YfyInit.init_system("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET") state = utils.generate_new_state() oauth = FangcloudOAuth2FlowBase() authorize_url = oauth.get_authorize_url("YOUR_REDIRECT_URL", state) ``` -------------------------------- ### Get File Information Source: https://github.com/yifangyun/fangcloud-python-sdk/blob/master/README.md Retrieves information about a specific file using its ID. This snippet demonstrates how to call the get_file_info method on the file client. ```python file_info = yfy_client.file().get_file_info(file_id) print('file_name: '+file_info['name']) ``` -------------------------------- ### Get Fangcloud Client Instance Source: https://github.com/yifangyun/fangcloud-python-sdk/blob/master/README.md Obtains a client instance for interacting with Fangcloud APIs. The first call requires user ID, access token, and refresh token. Subsequent calls can use just the user ID to reuse the client. ```python from fangcloud.yifangyun import YfyClientFactory YfyInit.init_system("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET") yfy_client = YfyClientFactory.get_client_instance("YOUR-USER-ID", access_token, refresh_token) ``` ```python yfy_client = YfyClientFactory.get_client_instance("YOUR-USER-ID") ``` -------------------------------- ### JWT: Get User OAuth Token Source: https://github.com/yifangyun/fangcloud-python-sdk/blob/master/README.md Obtains a user access token and refresh token using JWT authentication. Requires your private key and user/KID details. ```python from fangcloud.oauth import FangcloudOAuth2FlowBase from fangcloud.yifangyun import YfyInit YfyInit.init_system("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET") oauth = FangcloudOAuth2FlowBase() private_key = open("YOUR_PRIVATE_KEY_PATH", "r").read() # 获取用户token jwt_user_login = oauth.jwt_user_login("YOUR_USER_ID", "KID", private_key) user_access_token, user_refresh_token = jwt_user_login.access_token, jwt_user_login.refresh_token ``` -------------------------------- ### JWT: Get Enterprise OAuth Token Source: https://github.com/yifangyun/fangcloud-python-sdk/blob/master/README.md Obtains an enterprise access token and refresh token using JWT authentication. Requires your private key and enterprise/KID details. ```python from fangcloud.oauth import FangcloudOAuth2FlowBase from fangcloud.yifangyun import YfyInit YfyInit.init_system("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET") oauth = FangcloudOAuth2FlowBase() private_key = open("YOUR_PRIVATE_KEY_PATH", "r").read() # 获取企业token jwt_enterprise_login = oauth.jwt_enterprise_login("YOUR_ENTERPRISE_ID", "KID", private_key) enterprise_access_token, enterprise_refresh_token = jwt_enterprise_login.access_token, jwt_enterprise_login.refresh_token ``` -------------------------------- ### Initialize Fangcloud SDK Source: https://github.com/yifangyun/fangcloud-python-sdk/blob/master/README.md Initializes the Fangcloud SDK system with your application's client ID and client secret. This must be called before using other SDK functionalities. ```python from fangcloud.yifangyun import YfyInit YfyInit.init_system("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET") ``` -------------------------------- ### Web Demo: Initiate Authorization Source: https://github.com/yifangyun/fangcloud-python-sdk/blob/master/README.md A handler class from the web-demo that initiates the OAuth authorization process by generating a state and redirecting the user to the authorization URL. ```python class AuthStartHandler(BasicHandler): def post(self, *args, **kwargs): username = self.get_login_user() user = self.__database__.get_user(username) #check user login if user is None: response_page = utils.build_page("Error", "Nobody is logged in.") self.write(response_page) return # create a random stats, used for CSRF or remember web service current stats state = utils.generate_new_state() # the state can be stored in session self.set_state(state) # use SDK to fetch authorized url authorize_url = self.__oauth__.get_authorize_url(Config.redirect_url, state) self.redirect(authorize_url) ``` -------------------------------- ### Web Demo: Handle Authorization Callback Source: https://github.com/yifangyun/fangcloud-python-sdk/blob/master/README.md A handler class from the web-demo that processes the callback from the authorization server, validates the state, and exchanges the authorization code for tokens. ```python class AuthFinishHandler(BasicHandler): def get(self): code = self.get_argument("code", None) state = self.get_argument("state", None) username = self.get_login_user() user = self.__database__.get_user(username) if user is None: response_page = utils.build_page("Error", "Nobody is logged in.") self.write(response_page) return # check state state_in_session = self.get_state() if state != state_in_session: self.send_error(400, reason="Wrong state received") try: result = self.__oauth__.authenticate(code, Config.redirect_url) except OAuthCodeParamError: self.send_error(400, reason="Wrong oauth code to fetch oauth token") return except OAuthRedirectParamError: self.send_error(400, reason="Wrong oauth redirect url to fetch oauth") return self.write(str(result)) ``` -------------------------------- ### Authenticate with Authorization Code Source: https://github.com/yifangyun/fangcloud-python-sdk/blob/master/README.md Exchanges an authorization code for an access token and refresh token. This is a crucial step after the user has authorized your application. ```python oauth.FangcloudOAuth2FlowBase() result = oauth.authenticate('YOUR_AUTH_CODE') access_token, refresh_token = result.access_token, result.refresh_token ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.