def get_long_token(self, short_token, app_id=None, app_secret=None, return_json=False): # type: (str, Optional[str], Optional[str], bool) -> Optional[Union[AuthAccessToken, Dict]] """ Generate a long-lived token from a short-lived User access token. A long-lived token generally lasts about 60 days. :param app_id: Your app id. :param app_secret: Your app secret. :param short_token: short-lived token from the app. :param return_json: Set to false will return instance of AuthAccessToken. Or return json data. Default is false. """ if app_id is None: app_id = self.app_id app_secret = self.app_secret response = self._request(method='GET', path='{}/oauth/access_token'.format( self.version), args={ 'grant_type': 'fb_exchange_token', 'client_id': app_id, 'client_secret': app_secret, 'fb_exchange_token': short_token }, enforce_auth=False) data = self._parse_response(response) if return_json: return data else: return AuthAccessToken.new_from_json_dict(data)
def get_long_token(self, short_token, app_id=None, app_secret=None, return_json=False): # type: (str, Optional[str], Optional[str], bool) -> Optional[Union[AuthAccessToken, Dict]] """ Exchange a short-lived Instagram User Access Token for a long-lived Instagram User Access Token. :param short_token: short-lived Instagram User Access Token. :param app_id: Your Instagram app's id, this not necessary. :param app_secret: Your Instagram app's secret. :param return_json: Set to false will return instance of AuthAccessToken. Or return json data. Default is false. :return: Access token data. """ if app_secret is None: app_secret = self.app_secret resp = self._request(path="access_token", args={ "grant_type": "ig_exchange_token", "client_secret": app_secret, "access_token": short_token, }, enforce_auth=False) data = self._parse_response(resp) if return_json: return data else: return AuthAccessToken.new_from_json_dict(data)
def get_app_token(self, return_json=False): # type: (bool)-> Optional[Union[Dict, AuthAccessToken]] """ Use app credentials to generate an app access token. :param return_json: Set to false will return instance of AuthAccessToken. Or return json data. Default is false. """ resp = self._request(method="GET", path="{}/oauth/access_token".format(self.version), args={ 'grant_type': 'client_credentials', 'client_id': self.app_id, 'client_secret': self.app_secret, }, enforce_auth=False) data = self._parse_response(resp) if return_json: return data else: return AuthAccessToken.new_from_json_dict(data)
def exchange_access_token(self, response, redirect_uri=None, return_json=False): # type: (str, str, bool) -> Union[AuthAccessToken, Dict] """ :param response: The whole response url for your previous authorize step. :param redirect_uri: The URL that you want to redirect the person logging in back to. :param return_json: Set to false will return instance of AuthAccessToken. Or return json data. Default is false. :return: """ session = self._get_oauth_session(redirect_uri=redirect_uri) session.fetch_token( self.exchange_access_token_url, client_secret=self.app_secret, authorization_response=response ) self._access_token = session.access_token if return_json: return session.token else: return AuthAccessToken.new_from_json_dict(session.token)
def refresh_access_token(self, access_token=None, return_json=False): # type: (Optional[str], bool) -> Optional[Union[AuthAccessToken, Dict]] """ :param access_token: long-lived token will be refreshed. :param return_json: Set to false will return instance of AuthAccessToken. Or return json data. Default is false. :return: New long-lived access token data """ if access_token is None: access_token = self._access_token resp = self._request(path="refresh_access_token", args={ "grant_type": "ig_refresh_token", "access_token": access_token }) data = self._parse_response(resp) if return_json: return data else: return AuthAccessToken.new_from_json_dict(data)
def exchange_access_token(self, response, return_json=False): # type: (str, bool) -> Union[AuthAccessToken, Dict] """ :param response: The whole response url for your previous authorize step. :param return_json: Set to false will return instance of AuthAccessToken. Or return json data. Default is false. :return: """ if self.auth_session is None: raise PyFacebookException( ErrorMessage( code=ErrorCode.MISSING_PARAMS, message="exchange token should do authorize first")) self.auth_session.fetch_token(self.exchange_access_token_url, client_secret=self.app_secret, authorization_response=response) self._access_token = self.auth_session.access_token if return_json: return self.auth_session.token else: return AuthAccessToken.new_from_json_dict(self.auth_session.token)