def test_refresh_access_token(self): def request_callback(request): self.assertEqual(request.url, "https://v2.steemconnect.com/api/oauth2/token/") self.assertEqual( request.body, 'refresh_token=refresh_token&client_id=client_id&' 'client_secret=client_secret&scope=login') return 200, {}, json.dumps({"access_token": "foo"}) c = Client(client_id="client_id", client_secret="client_secret") responses.add_callback( responses.POST, 'https://v2.steemconnect.com/api/oauth2/token/', callback=request_callback, ) c.refresh_access_token("refresh_token", "login")
def vote(self, voter, author, permlink, weight): db = database.Database() result = db.get_user_auth(voter) access_token, refresh_token, expire_on = result[0] dt = datetime.now() c = Client( client_id=CLIENT_ID, client_secret=CLIENT_SECRET, access_token=access_token, ) try: # Verify access_token if dt > expire_on: result = c.refresh_access_token( refresh_token, "login,vote" # scopes ) access_token = result['access_token'] refresh_token = result['refresh_token'] expires_in = result['expires_in'] # Update access token in the DB self.db.update_authentication_tokens( voter, access_token, refresh_token, expires_in, self.timestamp, ) print('Updated access token\n') # Perform vote vote = Vote(voter, author, permlink, weight) result = c.broadcast([vote.to_operation_structure()]) # Log vote if 'error' in result: message = result['error_description'] db.add_to_error_log( voter, author, permlink, weight, message, self.timestamp, ) else: message = 'Succes' print(f"Voter: {voter}\nAuthor: {author}\n" + f"Permlink: {permlink}\nWeight: {weight}\n" + "Upvote succes\n") except Exception as error: db.add_to_error_log( voter, author, permlink, weight, error, self.timestamp, )
def vote(self, voter, author, permlink, weight, type): result = self.db.get_user_auth(voter) access_token, refresh_token, expire_on = result[0] dt = datetime.now() c = Client( client_id=CLIENT_ID, client_secret=CLIENT_SECRET, access_token=access_token, ) try: # Verify access_token if dt > expire_on: access_token = self.refresh_token(refresh_token) result = c.refresh_access_token( refresh_token, "login,vote" # scopes ) access_token = result['access_token'] refresh_token = result['refresh_token'] expires_in = result['expires_in'] self.db.update_authentication_tokens( voter, access_token, refresh_token, expires_in, self.timestamp, ) print('Updated access token\n') # Perform vote if not self.is_already_voted(voter, author, permlink): vote = Vote(voter, author, permlink, weight) result = c.broadcast([vote.to_operation_structure()]) # Log vote if 'error' in result: message = result['error_description'] self.db.add_to_error_log( voter, author, permlink, weight, type, message, self.timestamp, ) else: message = 'Succes' if type == 'vote': self.db.update_log( voter, permlink, message, 'vote_log', ) elif type == 'trail': self.db.update_log( voter, permlink, message, 'trail_log', ) print(f"Voter: {voter}\nAuthor: {author}\n" + f"Permlink: {permlink}\nWeight: {weight}\n" + "Upvote succes\n") except Exception as error: print(voter, author, permlink, weight, error) self.db.add_to_error_log( voter, author, permlink, weight, type, error, self.timestamp, )