def get_token(self): # If the token has expired, request a new one if self.timeout < int(time.time()): # For some dumb reason it has to be a string data = { "grant_type": "client_credentials", "client_id": self.gfyid, "client_secret": self.gfysecret } if self.username: data['grant_type'] = 'password' data['username'] = self.username data['password'] = self.password url = "https://api.gfycat.com/v1/oauth/token" r = requests.post(url, data=str(data), headers={'User-Agent': consts.user_agent}) try: response = r.json() except json.decoder.JSONDecodeError as e: print(r.text) raise self.timeout = int(time.time()) + response["expires_in"] self.token = response["access_token"] CredentialsLoader.set_credential('gfycat', 'refresh_token', self.token) CredentialsLoader.set_credential('gfycat', 'token_expiration', str(self.timeout)) return self.token
def __init__(self, id, secret): self.client_id = id self.client_secret = secret self.auth = None self.mashape_key = None access = CredentialsLoader.get_credentials()['imgur'].get( 'access_token', None) refresh = CredentialsLoader.get_credentials()['imgur'].get( 'refresh_token', None) # imgur_credentials = self.loadimgur() if access and refresh: self.auth = AuthWrapper(access, refresh, id, secret) else: # Oauth setup print("Imgur Auth URL: ", self.get_auth_url('pin')) pin = input("Paste the pin here:") credentials = self.authorize(pin, 'pin') CredentialsLoader.set_credential('imgur', 'access_token', credentials['access_token']) CredentialsLoader.set_credential('imgur', 'refresh_token', credentials['refresh_token']) # self.saveimgur((credentials['access_token'], credentials['refresh_token'])) self.set_user_auth(credentials['access_token'], credentials['refresh_token']) self.auth = AuthWrapper(credentials['access_token'], credentials['refresh_token'], id, secret)
def refresh(self): data = { 'refresh_token': self.refresh_token, 'client_id': self.client_id, 'client_secret': self.client_secret, 'grant_type': 'refresh_token' } url = API_URL + 'oauth2/token' response = requests.post(url, data=data) if response.status_code != 200: raise ImgurClientError('Error refreshing access token!', response.status_code) response_data = response.json() self.current_access_token = response_data['access_token'] CredentialsLoader.set_credential('imgur', 'access_token', response_data['access_token'])
def get_token(self): # If the token has expired, request a new one if self.timeout < int(time.time()): data = {"grant_type": "refresh", "client_id": self.gfyid, "client_secret": self.gfysecret, "refresh_token": self.refresh} url = self.TOKEN_URL # For some dumb reason, data has to be a string r = requests.post(url, data=str(data), headers={'User-Agent': consts.user_agent}) try: response = r.json() except json.decoder.JSONDecodeError as e: print(r.text) raise # Sometimes Gfycat randomly invalidates refresh tokens >:( if r.status_code == 401: raise InvalidRefreshToken self.timeout = int(time.time()) + response["expires_in"] self.access = response["access_token"] CredentialsLoader.set_credential(self.CREDENTIALS_BLOCK, 'access_token', self.access) CredentialsLoader.set_credential(self.CREDENTIALS_BLOCK, 'token_expiration', str(self.timeout)) return self.access
def authenticate(self, password=False): # For some dumb reason it has to be a string if password: print("Log into {}".format(self.SERVICE_NAME)) username = input("Username: "******"Password: "******"grant_type": "password", "client_id": self.gfyid, "client_secret": self.gfysecret, "username": username, "password": password} else: data = {"grant_type": "client_credentials", "client_id": self.gfyid, "client_secret": self.gfysecret} url = self.TOKEN_URL r = requests.post(url, data=str(data), headers={'User-Agent': consts.user_agent}) try: response = r.json() except json.decoder.JSONDecodeError as e: print(r.text) raise try: self.timeout = int(time.time()) + response["expires_in"] self.access = response["access_token"] self.refresh = response["refresh_token"] except KeyError: print(r.text) raise CredentialsLoader.set_credential(self.CREDENTIALS_BLOCK, 'refresh_token', self.refresh) CredentialsLoader.set_credential(self.CREDENTIALS_BLOCK, 'access_token', self.access) CredentialsLoader.set_credential(self.CREDENTIALS_BLOCK, 'token_expiration', str(self.timeout))
def authenticate(self, password=False): # For some dumb reason it has to be a string if password: print("Log into Gfycat") username = input("Username: "******"Password: "******"grant_type": "password", "client_id": self.gfyid, "client_secret": self.gfysecret, "username": username, "password": password} else: data = {"grant_type": "client_credentials", "client_id": self.gfyid, "client_secret": self.gfysecret} url = "https://api.gfycat.com/v1/oauth/token" r = requests.post(url, data=str(data), headers={'User-Agent': consts.user_agent}) try: response = r.json() except json.decoder.JSONDecodeError as e: print(r.text) raise self.timeout = int(time.time()) + response["expires_in"] self.access = response["access_token"] self.refresh = response["refresh_token"] CredentialsLoader.set_credential('gfycat', 'refresh_token', self.refresh) CredentialsLoader.set_credential('gfycat', 'access_token', self.access) CredentialsLoader.set_credential('gfycat', 'token_expiration', str(self.timeout))
def authenticate(self): print("Log into {}".format(self.SERVICE_NAME)) print("Authorize here: " + self.OAUTH_BASE + self.AUTHORIZATION_URL + "?" + urllib.parse.urlencode({ "client_id": self.client_id, "response_type": "token", "state": "GifHostLibrary" })) result = input().strip() parts = urllib.parse.urlparse(result) params = urllib.parse.parse_qs(parts.fragment) print(parts, params) self.timeout = int(time.time()) + int(params["expires_in"][0]) # [0] quirk of parse_qs self.access = params["access_token"][0] self.refresh = params["refresh_token"][0] CredentialsLoader.set_credential(self.CREDENTIALS_BLOCK, 'refresh_token', self.refresh) CredentialsLoader.set_credential(self.CREDENTIALS_BLOCK, 'access_token', self.access) CredentialsLoader.set_credential(self.CREDENTIALS_BLOCK, 'token_expiration', str(self.timeout))