Commit d81fd5bd authored by Alp Deniz Ogut's avatar Alp Deniz Ogut
Browse files

Start using pluginbase oauth

1 merge request!2Working
Pipeline #6000 passed with stage
in 1 minute and 51 seconds
Showing with 75 additions and 43 deletions
+75 -43
import os
import json
from pymemri.pod.client import PodClient
from pymemri.plugin.pluginbase import PluginRun, run_plugin_from_run_id
from pymemri.data.schema import Account
from twitter.plugin import TwitterPlugin
......@@ -9,17 +10,20 @@ SERVICE_NAME = 'twitter'
def testPlugin():
client = PodClient()
credentials_str = os.environ.get("TWITTER_CI_CREDENTIALS")
existing_accounts = client.search({'service': SERVICE_NAME, 'isMe': True})
# use a provided token if exists. If not complete authorization.
if len(existing_accounts) == 0 and credentials_str:
credentials = json.loads(credentials_str)
me = Account(service=SERVICE_NAME, isMe=True, identifier=credentials['access_token'], secret=credentials['access_token_secret'])
if len(existing_accounts) == 0:
me = Account(service=SERVICE_NAME, isMe=True)
client.create(me)
else:
me = existing_accounts[0]
plugin = TwitterPlugin(client=client)
plugin.add_to_schema()
plugin.run()
client.add_to_schema(PluginRun)
run = PluginRun(containerImage="none", pluginName="TwitterPlugin", pluginModule="twitter.plugin", status="Not started")
run.add_edge('account', me)
client.create(run)
run_plugin_from_run_id(run.id, client)
# test authorization
me = client.search({'service': SERVICE_NAME, 'isMe': True})[0]
......
import json
from tweepy import API, OAuth1UserHandler
from .constants import APP_KEY, APP_SECRET, USER_CREDENTIALS
from pymemri.plugin.authenticators.oauth import OAuthAuthenticator
class TwitterAuthenticator(OAuthAuthenticator):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.me = self.pluginRun.account[0]
self.oauth = OAuth1UserHandler(APP_KEY, APP_SECRET, callback="oob")
if USER_CREDENTIALS:
credentials = json.loads(USER_CREDENTIALS)
self.me.identifier = credentials['access_token']
self.me.secret=credentials['access_token_secret']
self.me.update(self.client)
def get_oauth_url(self):
return self.oauth.get_authorization_url()
def get_tokens_from_code(self, code):
key, secret = self.oauth.get_access_token(code)
return {
'identifier': key,
'secret' : secret
}
def verify_tokens(self):
if self.oauth.access_token and self.oauth.access_token_secret:
me = API(self.oauth).verify_credentials(include_entities=False)
if me.id > 0:
return True
raise Exception("Could not verify tokens: Non-existent or invalid")
def verify_access_token(self, token):
pass
def refresh_tokens(self):
pass
def store_tokens(self, tokens):
for key in tokens.keys():
if tokens[key] and hasattr(self.me, key):
setattr(self.me, key, tokens[key])
self.me.update(self.client)
def authenticate(self):
try:
self.oauth.set_access_token(self.me.identifier, self.me.secret)
self.verify_tokens()
except:
url = self.get_oauth_url()
self.present_url_to_user(url)
code = self.poll_for_code()
tokens = self.get_tokens_from_code(code)
self.store_tokens(tokens)
......@@ -6,3 +6,4 @@ SERVICE_NAME = "twitter"
# App credentials
APP_KEY = os.environ.get("TWITTER_APP_KEY")
APP_SECRET = os.environ.get("TWITTER_APP_SECRET")
USER_CREDENTIALS = os.environ.get("TWITTER_CI_CREDENTIALS")
\ No newline at end of file
......@@ -6,6 +6,7 @@ from datetime import datetime
from time import sleep
from . import helpers
from .constants import *
from .authenticator import TwitterAuthenticator
from pymemri.plugin.pluginbase import PluginBase
from pymemri.plugin.states import RUN_USER_ACTION_NEEDED, RUN_USER_ACTION_COMPLETED
from pymemri.data.schema import Edge, Account, Photo, Message, MessageChannel
......@@ -31,6 +32,10 @@ class TwitterPlugin(PluginBase):
Setup plugin
"""
# Start authentication
auth = TwitterAuthenticator(client=self.client, pluginRun=self.pluginRun)
auth.authenticate()
# Get existing item ids
self.existing_photo_hashes = helpers.get_file_hashes(self.client)
self.existing_account_ids = helpers.get_external_ids(self.client, 'Account')
......@@ -38,25 +43,9 @@ class TwitterPlugin(PluginBase):
self.existing_message_ids = helpers.get_external_ids(self.client, 'Message')
self.existing_channel_ids = helpers.get_external_ids(self.client, "MessageChannel")
# Start authentication
oauth = tweepy.OAuth1UserHandler(APP_KEY, APP_SECRET, callback="oob")
try:
me = helpers.get_user(self.client, isMe=True)
oauth.set_access_token(me.identifier, me.secret)
except Exception as e:
authorization_url = oauth.get_authorization_url()
verifier = self.get_authorization_code(authorization_url)
# print("AUTH URL:", authorization_url)
# user-interaction below
# verifier = input("Input PIN: ")
access_token, access_token_secret = oauth.get_access_token(verifier)
me = Account(service=SERVICE_NAME, isMe=True, identifier=access_token, secret=access_token_secret)
self.client.create(me)
self.api = tweepy.API(oauth, wait_on_rate_limit=True)
self.api = tweepy.API(auth.oauth, wait_on_rate_limit=True)
# Update me
self.me = self.process_user(user=self.api.verify_credentials(), isMe=True, target_account=me)
self.me = self.process_user(user=self.api.verify_credentials(), isMe=True, target_account=auth.me)
self.write()
def run(self):
......@@ -141,8 +130,7 @@ class TwitterPlugin(PluginBase):
account.add_edge('profilePicture', picture)
self.add_to_queue(items=[picture, picture.file[0]], files=[picture.data],
edges=picture.get_edges('file') + [Edge(account, picture, 'profilePicture')])
# picture.store(self.client)
# account.add_edge('profilePicture', picture)
# picture.store(self.client)
# account.store(self.client)
self.add_to_queue(updates=[account], edges=edges)
......@@ -263,21 +251,5 @@ class TwitterPlugin(PluginBase):
self.writing=False
logging.info("Written queues to Pod")
def get_authorization_code(self, url):
"""
Display authorization url and wait for its corresponding codec
"""
self.pluginRun.authUrl = url
self.pluginRun.status = RUN_USER_ACTION_NEEDED
self.pluginRun.update(self.client)
# wait for user input
logger.info("Waiting for user action")
while self.pluginRun.status != RUN_USER_ACTION_COMPLETED:
sleep(1)
self.pluginRun = self.client.get(self.pluginRun.id)
# get the user-supplied code
return self.pluginRun.account[0].code
def add_to_schema(self):
self.client.add_to_schema(Account, Photo, Tweet, Message, MessageChannel)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment