def get_tokens(): oauth = requests_oauthlib.OAuth1Session(client_key, client_secret=client_secret) request_token_url = 'https://api.twitter.com/oauth/request_token' fetch_response = oauth.fetch_request_token(request_token_url) resource_owner_key = fetch_response.get('oauth_token') resource_owner_secret = fetch_response.get('oauth_token_secret') base_authorization_url = 'https://api.twitter.com/oauth/authorize' authorization_url = oauth.authorization_url(base_authorization_url) webbrowser.open(authorization_url) verifier = raw_input('4626449') oauth = requests_oauthlib.OAuth1Session( client_key, client_secret=client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret, verifier=verifier) access_token_url = 'https://api.twitter.com/oauth/access_token' oauth_tokens = oauth.fetch_access_token(access_token_url) resource_owner_key = oauth_tokens.get('oauth_token') resource_owner_secret = oauth_tokens.get('oauth_token_secret') return (client_key, client_secret, resource_owner_key, resource_owner_secret, verifier)
def tokens(): oauth = requests_oauthlib.OAuth1Session(client_key, client_secret=client_secret) request_token_url = 'https://api.twitter.com/oauth/request_token' fetch_response = oauth.fetch_request_token(request_token_url) resource_owner_key = fetch_response.get('oauth_token') resource_owner_secret = fetch_response.get('oauth_token_secret') base_authorization_url = 'https://api.twitter.com/oauth/authorize' authorization_url = oauth.authorization_url(base_authorization_url) print("""Please click on the URL below, click on "Authorize App", and then copy the authorization code and paste it below.""") print(authorization_url) verifier = input('Please input the verifier>>> ') oauth = requests_oauthlib.OAuth1Session( client_key, client_secret=client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret, verifier=verifier) access_token_url = 'https://api.twitter.com/oauth/access_token' oauth_tokens = oauth.fetch_access_token(access_token_url) resource_owner_key = oauth_tokens.get('oauth_token') resource_owner_secret = oauth_tokens.get('oauth_token_secret') return (client_key, client_secret, resource_owner_key, resource_owner_secret, verifier)
def handle(self, *args, **options): if not settings.TWITTER_CLIENT: raise CommandError("TWITTER_CLIENT must be set in settings_local.py") if not settings.TWITTER_CLIENTSECRET: raise CommandError("TWITTER_CLIENTSECRET must be set in settings_local.py") if settings.TWITTER_NEWS_TOKEN: raise CommandError("TWITTER_NEWS_TOKEN is already set in settings_local.py") if settings.TWITTER_NEWS_TOKENSECRET: raise CommandError("TWITTER_NEWS_TOKENSECRET is already set in settings_local.py") # OK, now we're good to go :) oauth = requests_oauthlib.OAuth1Session(settings.TWITTER_CLIENT, settings.TWITTER_CLIENTSECRET) fetch_response = oauth.fetch_request_token('https://api.twitter.com/oauth/request_token') authorization_url = oauth.authorization_url('https://api.twitter.com/oauth/authorize') print('Please go here and authorize: %s' % authorization_url) pin = input('Paste the PIN here: ') oauth = requests_oauthlib.OAuth1Session(settings.TWITTER_CLIENT, settings.TWITTER_CLIENTSECRET, resource_owner_key=fetch_response.get('oauth_token'), resource_owner_secret=fetch_response.get('oauth_token_secret'), verifier=pin) oauth_tokens = oauth.fetch_access_token('https://api.twitter.com/oauth/access_token') print("Authorized. Please configure:") print("TWITTER_NEWS_TOKEN='%s'" % oauth_tokens.get('oauth_token')) print("TWITTER_NEWS_TOKENSECRET='%s'" % oauth_tokens.get('oauth_token_secret')) print("In your local_settings.py!")
def get_tokens(client_key=CLIENT_KEY, client_secret=CLIENT_SECRET, request_token_url=REQUEST_TOKEN_URL, base_authorization_url=BASE_AUTH_URL, access_token_url=ACCESS_TOKEN_URL, verifier_auto=False): oauth_inst = requests_oauthlib.OAuth1Session(client_key, client_secret=client_secret) fetch_response = oauth_inst.fetch_request_token(request_token_url) resource_owner_key = fetch_response.get('oauth_token') resource_owner_secret = fetch_response.get('oauth_token_secret') auth_url = oauth_inst.authorization_url(base_authorization_url) webbrowser.open(auth_url) if verifier_auto: verifier = input("Please input the verifier: ") else: redirect_result = input("Paste the full redirect URL here: ") oauth_resp = oauth_inst.parse_authorization_response(redirect_result) verifier = oauth_resp.get('oauth_verifier') oauth_inst = requests_oauthlib.OAuth1Session( client_key, client_secret=client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret, verifier=verifier) oauth_tokens = oauth_inst.fetch_access_token(access_token_url) resource_owner_key, resource_owner_secret = oauth_tokens.get( 'oauth_token'), oauth_tokens.get('oauth_token_secret') return client_key, client_secret, resource_owner_key, resource_owner_secret, verifier
def test_oauth1_callback_authentication(self): oauth = requests_oauthlib.OAuth1Session(self.oauth1_callback_client_key, client_secret=self.oauth1_callback_client_secret, callback_uri=self.callback_url) fetch_response = oauth.fetch_request_token(self.oauth1_request_token_url) resource_owner_key = fetch_response.get('oauth_token') resource_owner_secret = fetch_response.get('oauth_token_secret') self.assertTrue(resource_owner_key) self.assertTrue(resource_owner_secret) self.assertTrue(self.url_open(oauth.authorization_url(self.oauth1_authorization_url))) data = {'oauth_token': resource_owner_key, 'login': self.login, 'password': self.password} authorization_url = self.url_prepare(self.oauth1_authorization_url) response = self.opener.post(authorization_url, data=data, timeout=10, allow_redirects=False) callback = urllib.parse.urlparse(response.headers['Location']) verifier = urllib.parse.parse_qs(callback.query)['oauth_verifier'][0] self.assertTrue(verifier) oauth = requests_oauthlib.OAuth1Session(self.oauth1_callback_client_key, client_secret=self.oauth1_callback_client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret, verifier=verifier) oauth_tokens = oauth.fetch_access_token(self.oauth1_access_token_url) resource_owner_key = oauth_tokens.get('oauth_token') resource_owner_secret = oauth_tokens.get('oauth_token_secret') self.assertTrue(resource_owner_key) self.assertTrue(resource_owner_secret) oauth = requests_oauthlib.OAuth1Session(self.oauth1_callback_client_key, client_secret=self.oauth1_callback_client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret) self.assertTrue(oauth.get(self.test_authentication_url))
def flickr_session(): """Get a Flickr session, authenticating if necessary.""" if os.path.isfile(FLICKR_TOKEN_FILENAME): with open(FLICKR_TOKEN_FILENAME, 'rb') as f_open: access_token = pickle.load(f_open) return requests_oauthlib.OAuth1Session( client_key=KEY, client_secret=SECRET, resource_owner_key=access_token['oauth_token'], resource_owner_secret=access_token['oauth_token_secret']) else: flickr = requests_oauthlib.OAuth1Session( client_key=KEY, client_secret=SECRET, callback_uri='http://127.0.0.1/cb', ) flickr.fetch_request_token(REQUEST_TOKEN_URL) authorization_url = flickr.authorization_url(AUTH_BASE_URL) print('Please go here and authorize,', authorization_url) flickr.parse_authorization_response( input('Paste the full redirect URL here: ')) with open(FLICKR_TOKEN_FILENAME, 'wb') as f_write: token = flickr.fetch_access_token(ACCESS_TOKEN_URL) pickle.dump(token, f_write) return flickr
def get_tokens(client_key=CLIENT_KEY, client_secret=CLIENT_SECRET, request_token_url=REQUEST_TOKEN_URL, base_authorization_url=AUTHORIZATION_BASE_URL, access_token_url=ACCESS_TOKEN_URL, verifier_auto=True, redirect_uri=REDIRECT_URI): oauth_inst = requests_oauthlib.OAuth1Session(client_key, client_secret=client_secret) fetch_response = oauth_inst.fetch_request_token(request_token_url) resource_owner_key = fetch_response.get('oauth_token') resource_owner_secret = fetch_response.get('oauth_token_secret') auth_url = oauth_inst.authorization_url(base_authorization_url) webbrowser.open(auth_url) redirect_result = redirect_uri oauth_resp = oauth_inst.parse_authorization_response(redirect_result) oauth_inst = requests_oauthlib.OAuth1Session( client_key, client_secret=client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret, verifier=verifier) oauth_tokens = oauth_inst.fetch_access_token(access_token_url) resource_owner_key, resource_owner_secret = oauth_tokens.get( 'oauth_token'), oauth_tokens.get('oauth_token_secret') return client_key, client_secret, resource_owner_key, resource_owner_secret, verifier
def get_tokens(client_key=CLIENT_KEY, client_secret=CLIENT_SECRET,request_token_url=REQUEST_TOKEN_URL,base_authorization_url=BASE_AUTH_URL,access_token_url=ACCESS_TOKEN_URL,verifier_auto=True): oauth_inst = requests_oauthlib.OAuth1Session(client_key,client_secret=client_secret) fetch_response = oauth_inst.fetch_request_token(request_token_url) # Using the dictionary .get method in these lines resource_owner_key = fetch_response.get('oauth_token') resource_owner_secret = fetch_response.get('oauth_token_secret') auth_url = oauth_inst.authorization_url(base_authorization_url) #authorization_url is a method of requests_oathlib.OAuth1Session # Open the auth url in browser: # User input required webbrowser.open(auth_url) # For user to interact with & approve access of this app -- this script # Deal with required input, which will vary by API # if verifier_auto: # if the input is default (True), like Tumblr # verifier = input("Please input the verifier: ") # else: redirect_result = input("Paste the full redirect URL here: ") oauth_resp = oauth_inst.parse_authorization_response(redirect_result) # returns a dictionary -- you may want to inspect that this works and edit accordingly verifier = oauth_resp.get('oauth_verifier') # Regenerate instance of oauth1session class with more data oauth_inst = requests_oauthlib.OAuth1Session(client_key, client_secret=client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret, verifier=verifier) oauth_tokens = oauth_inst.fetch_access_token(access_token_url) # returns a dictionary # Use that dictionary to get these things # Tuple assignment syntax resource_owner_key, resource_owner_secret = oauth_tokens.get('oauth_token'), oauth_tokens.get('oauth_token_secret') return client_key, client_secret, resource_owner_key, resource_owner_secret, verifier # returns authentication data
def test_oauth1_oob_authentication(self): oauth = requests_oauthlib.OAuth1Session(self.oauth1_oob_client_key, client_secret=self.oauth1_oob_client_secret, callback_uri='oob') fetch_response = oauth.fetch_request_token(self.oauth1_request_token_url) resource_owner_key = fetch_response.get('oauth_token') resource_owner_secret = fetch_response.get('oauth_token_secret') self.assertTrue(resource_owner_key) self.assertTrue(resource_owner_secret) self.assertTrue(self.url_open(oauth.authorization_url(self.oauth1_authorization_url))) data = {'oauth_token': resource_owner_key, 'login': self.login, 'password': self.password} verifier = self.url_open(self.oauth1_authorization_url, data=data).json()['oauth_verifier'] self.assertTrue(verifier) oauth = requests_oauthlib.OAuth1Session(self.oauth1_oob_client_key, client_secret=self.oauth1_oob_client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret, verifier=verifier) oauth_tokens = oauth.fetch_access_token(self.oauth1_access_token_url) resource_owner_key = oauth_tokens.get('oauth_token') resource_owner_secret = oauth_tokens.get('oauth_token_secret') self.assertTrue(resource_owner_key) self.assertTrue(resource_owner_secret) oauth = requests_oauthlib.OAuth1Session(self.oauth1_oob_client_key, client_secret=self.oauth1_oob_client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret) self.assertTrue(oauth.get(self.test_authentication_url))
def get_tokens(): ## Step 1: Obtain a request token which will identify you (the client) in the next step. # At this stage you will only need your client key and secret # after this line executes, oauth will now be an instance of the class OAuth1Session oauth = requests_oauthlib.OAuth1Session(client_key, client_secret=client_secret) request_token_url = 'https://api.twitter.com/oauth/request_token' # invoke the fetch_request_token method of the class OAuth1Session on our instance # it returns a dictionary # oauth is bound to, so it can be used in later steps fetch_response = oauth.fetch_request_token(request_token_url) # pull the two values out of the dictionary and store them in a variable for later use # note that d.get('somekey') is another way of writing d['somekey'] resource_owner_key = fetch_response.get('oauth_token') resource_owner_secret = fetch_response.get('oauth_token_secret') ## Step 2. Obtain authorization from the user (resource owner) to access base_authorization_url = 'https://api.twitter.com/oauth/authorize' # append the query parameters need to make it a full url. # they will include the resource_owner_key from the previus step, # which was stored in the oauth object above as an instance variable # when fetch_request_token was invoked authorization_url = oauth.authorization_url(base_authorization_url) webbrowser.open(authorization_url) # User will have to cut and paste the verifier or the whole redirect url # version where the website provides a verifier verifier = raw_input('Please input the verifier>>> ') ## Step 3. Obtain an access token from the OAuth provider. Save this token so it can be re-used later. # In this step we re-use most of the credentials obtained up to this point. # make a new instance of OAuth1Session, with several more parameters filled in oauth = requests_oauthlib.OAuth1Session( client_key, client_secret=client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret, verifier=verifier) access_token_url = 'https://api.twitter.com/oauth/access_token' oauth_tokens = oauth.fetch_access_token(access_token_url) resource_owner_key = oauth_tokens.get('oauth_token') resource_owner_secret = oauth_tokens.get('oauth_token_secret') return (client_key, client_secret, resource_owner_key, resource_owner_secret, verifier)
def get_tokens(client_key=CLIENT_KEY, client_secret=CLIENT_SECRET, request_token_url=REQUEST_TOKEN_URL, base_authorization_url=BASE_AUTH_URL, access_token_url=ACCESS_TOKEN_URL, verifier_auto=False): # creates instance of OAuth1Session, upon which we can invoke methods oauth_inst = requests_oauthlib.OAuth1Session(client_key, client_secret=client_secret) # Use .fetch method that returns a dictionary of credential data fetch_response = oauth_inst.fetch_request_token(request_token_url) # Using the dictionary .get method in these lines resource_owner_key = fetch_response.get( 'oauth_token') # get value of oauth token resource_owner_secret = fetch_response.get( 'oauth_token_secret') # get value of oauth secret # get authentication url. base url is only the first step, because have to pass some parameters through this url (i.e. verifier) to get info back... auth_url = oauth_inst.authorization_url(base_authorization_url) # Open the auth url in browser so that user can put in their verifier: webbrowser.open( auth_url ) # For user to interact with & approve access of this app -- this script # Deal with required input, which will vary by API if verifier_auto: # if the input is default (True), like Tumblr verifier = input("Please input the verifier: ") else: redirect_result = input("Paste the full redirect URL here: ") oauth_resp = oauth_inst.parse_authorization_response( redirect_result ) # returns a dictionary -- you may want to inspect that this works and edit accordingly verifier = oauth_resp.get('oauth_verifier') # Regenerate instance of oauth1session class with more data oauth_inst = requests_oauthlib.OAuth1Session( client_key, client_secret=client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret, verifier=verifier) oauth_tokens = oauth_inst.fetch_access_token( access_token_url) # returns a dictionary # Use that dictionary to get these things # Tuple assignment syntax resource_owner_key, resource_owner_secret = oauth_tokens.get( 'oauth_token'), oauth_tokens.get('oauth_token_secret') return client_key, client_secret, resource_owner_key, resource_owner_secret, verifier
def get_tokens(client_key=CLIENT_KEY, client_secret=CLIENT_SECRET, request_token_url=REQUEST_TOKEN_URL, base_authorization_url=BASE_AUTH_URL, access_token_url=ACCESS_TOKEN_URL, verifier_auto=False): # step 1: Create a session: client_key and client_secret oauth_inst = requests_oauthlib.OAuth1Session(client_key, client_secret=client_secret) # step 2: Use request_token_url to get oauth token, oauth token secret from Tumblr fetch_response = oauth_inst.fetch_request_token(request_token_url) # step 3: Get oauth token, oauth token secret # Authorization URL: baseurl + client_key, client_secret, oauth token, oauth token secret(incorporated in the oauth instance) oauth_token = fetch_response['oauth_token'] oauth_token_secret = fetch_response['oauth_token_secret'] auth_url = oauth_inst.authorization_url(base_authorization_url) # step 4: Open the auth url in browser: webbrowser.open( auth_url ) # For user to interact with & approve access of this app -- this script. This will redirect to another page with verifier # step 5: Get verification tokens if verifier_auto: verifier = input("Please input the verifier: ") else: redirect_result = input("Paste the full redirect URL here: ") oauth_resp = oauth_inst.parse_authorization_response( redirect_result ) # returns a dictionary -- you may want to inspect that this works and edit accordingly verifier = oauth_resp['oauth_verifier'] # step 6: Regenerate a new session with: client_key, client_secret, oauth token, oauth_token_secret, oauth_verifier oauth_inst = requests_oauthlib.OAuth1Session( client_key, client_secret=client_secret, resource_owner_key=oauth_token, resource_owner_secret=oauth_token_secret, verifier=verifier) # step 7: Get Access Token from the new session access_token = oauth_inst.fetch_access_token( access_token_url) # returns a dictionary # step 8: Use access_token to get a new oauth_token and oauth_token_secret oauth_token, oauth_token_secret = access_token.get( 'oauth_token'), access_token.get('oauth_token_secret') return client_key, client_secret, oauth_token, oauth_token_secret, verifier
def get_tokens(): #Obtains a request token which will identify the client. #Within the requests_oauthlib module there is a class called OAuth1Session. #There are two paramaters(-- the key, -- the secret). These correspond to the client_key and client secret found in the code above. #oauth is now an instance of the class OAuth1Session. oauth = requests_oauthlib.OAuth1Session(client_key, client_secret=client_secret) #Where you must request a token. Visit this url. request_token_url = 'https://api.twitter.com/oauth/request_token' #We must invoke fetch_request_token method of the class OAuth1Session on oauth(our instance) and it will return a dictionary. fetch_response = oauth.fetch_request_token(request_token_url) #Store two values from the dictionary into variables. resource_owner_key = fetch_response.get('oauth_token') resource_owner_secret = fetch_response.get('oauth_token_secret') #Allows us to obtain authorization from the user to access their information. base_authorization_url = 'https://api.twitter.com/oauth/authorize' authorization_url = oauth.authorization_url(base_authorization_url) print("""Please click on the URL below, click on "Authorize App", and then copy the authorization code and paste it below.""") print(authorization_url) #Copy and paste the verifier or the whole redirect url. verifier = input('Please input the verifier>>> ') #Allows us to obtain an access token from OAuth provider. #A new instance of OAuth1Session is made. More parameters are filled in. oauth = requests_oauthlib.OAuth1Session( client_key, client_secret=client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret, verifier=verifier) access_token_url = 'https://api.twitter.com/oauth/access_token' oauth_tokens = oauth.fetch_access_token(access_token_url) resource_owner_key = oauth_tokens.get('oauth_token') resource_owner_secret = oauth_tokens.get('oauth_token_secret') return (client_key, client_secret, resource_owner_key, resource_owner_secret, verifier)
def __init__(self): self.twitter = requests_oauthlib.OAuth1Session( 'client_key', client_secret='client_secret', resource_owner_key='ownerkey', resource_owner_secret='ownersecret') self.counter = 0 self.letsAhiruyaki = 10 # あひる焼きレシピ self.ahiruyakiRecipe = [ 'あひる焼き', 'あひるのあひる焼き', 'Arduinoであひる焼きマシンをつくったぜ', 'ahiruyaki' ] self.beforeAhiruyaki = self.ahiruyakiRecipe[0] self.ahiruyakiRecipeKey = 0 result = self.twitter.get( 'https://api.twitter.com/1.1/application/rate_limit_status.json') resetDate = datetime.datetime.fromtimestamp( float(result.headers['x-rate-limit-reset'])) print("あひる焼きできる残り: {0}".format( result.headers['x-rate-limit-remaining'])) print("あひる焼きできるようになる時刻: {0}".format(resetDate))
def get_data_from_api(blog_identifier, method="info", service_ident="Tumblr", expire_in_days=7): """Check in cache, if not found, load data, save in cache and then return that data""" request_url = "http://api.tumblr.com/v2/blog/{}.tumblr.com/{}".format( blog_identifier, method) data = get_from_cache(request_url, CACHE_DICTION) if data: if DEBUG: print("Loading from data cache: {}... data".format(request_url)) else: if DEBUG: print("Fetching new data from {}".format(request_url)) client_key, client_secret, resource_owner_key, resource_owner_secret, verifier = get_tokens_from_service( service_ident) oauth_inst = requests_oauthlib.OAuth1Session( client_key, client_secret=client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret) resp = oauth_inst.get(request_url) data_str = resp.text data = json.loads(data_str) set_in_data_cache(request_url, data, expire_in_days) return data
def get_cache_twitter_search(search_term, num=25): base_url = "https://api.twitter.com/1.1/search/tweets.json" params_diction = {} params_diction['q'] = search_term params_diction['count'] = num unique_ident = params_unique_combination(base_url, params_diction) if unique_ident in CACHE_DICTION: return CACHE_DICTION[unique_ident] else: # Remember we need to use an oauth object instance to make the request, using requests_oauthlib oauth = requests_oauthlib.OAuth1Session( client_key, client_secret=client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret ) # Using the global vars from getting credentials / getting credentials from cache resp = oauth.get(base_url, params=params_diction) twitter_data = resp.text twitter_diction = json.loads(resp.text) CACHE_DICTION[unique_ident] = twitter_diction cache_dump = json.dumps(CACHE_DICTION) f = open(CACHE_FNAME, 'w') f.write(cache_dump) f.close() return CACHE_DICTION[unique_ident]
def user_auth(): #make the initial oauth call oauth = requests_oauthlib.OAuth1(key, client_secret=secret, callback_uri=callback) r = requests.post(url=request_token_url, auth=oauth) #parse the response response = urlparse.parse_qs(r.content) owner_key = response.get('oauth_token')[0] owner_secret = response.get('oauth_token_secret')[0] #get the verification code (interactive) authorize_url = authorization_base_url + '?oauth_token=' authorize_url = authorize_url + owner_key webbrowser.open_new_tab(authorize_url) # print 'Please go here and authorize,', authorize_url verifier = raw_input('Please input the verifier, ') #get the final token oauth = requests_oauthlib.OAuth1Session(key, client_secret=secret, resource_owner_key=owner_key, resource_owner_secret=owner_secret, verifier=verifier) #write response to yaml oauth_tokens = oauth.fetch_access_token(access_token_url) with open('auth.yml', 'w') as outfile: outfile.write(yaml.dump(oauth_tokens))
def main(): twitter = requests_oauthlib.OAuth1Session(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET) while True: r = twitter.post(TWITTER_API_URI, data=dict(track=KEYWORD), stream=True) status_code = r.status_code if status_code == 200: break elif r.status_code == 420: time.sleep(60) print(f"Wait 60s for {r.reason}") else: r.raise_for_status() mongo = pymongo.MongoClient( f"mongodb://{MONGO_USERNAME}:{MONGO_PASSWORD}@{MONGO_HOST}:{MONGO_PORT}" ) count = 0 for line in tqdm.tqdm(r.iter_lines(), unit="tweets", mininterval=1): if line: tweet = json.loads(line) tweet["timestamp"] = datetime.datetime.utcnow().isoformat() tweet["keyword"] = KEYWORD mongo.twitter.sample.insert_one(tweet) count += 1 if MAX_COUNTS <= count: break
def create_dnsresource(self): """ Create a DNS resource object Due to https://bugs.launchpad.net/maas/+bug/1555393 this is implemented outside of the maas lib. """ dns_url = '{}/api/2.0/dnsresources/?format=json'.format( self.maas_server) (consumer_key, access_token, token_secret) = self.maas_creds.split(':') # The use of PLAINTEXT signature is inline with libmaas # https://goo.gl/EJPrM7 but as noted there should be switched # to HMAC once it works server-side. maas_session = requests_oauthlib.OAuth1Session( consumer_key, signature_method='PLAINTEXT', resource_owner_key=access_token, resource_owner_secret=token_secret) fqdn_list = self.fqdn.split('.') payload = { 'fqdn': self.fqdn, 'name': fqdn_list[0], 'domain': '.'.join(fqdn_list[1:]), 'address_ttl': self.ttl, 'ip_addresses': self.ip, } @retry_on_request_error(retries=options_retries, base_delay=options_base_delay, codes=options_codes) def inner_maas_session_post(session, dns_url, payload): return session.post(dns_url, data=payload) return inner_maas_session_post(maas_session, dns_url, payload)
def twitter_get_cache_search(search_terms=None, user_search=None, num_results=50): params_dictionary = {} if search_terms is not None: params_dictionary["q"] = search_terms base_url = "https://api.twitter.com/1.1/search/tweets.json" params_dictionary["count"] = num_results if user_search is not None: params_dictionary["screen_name"] = user_search base_url = "https://api.twitter.com/1.1/statuses/user_timeline.json" params_dictionary["count"] = num_results twitter_unique_identifier = twitter_params_unique_combination( base_url, params_dictionary) if twitter_unique_identifier in CACHE_DICTIONARY: return CACHE_DICTIONARY[twitter_unique_identifier] else: oauth = requests_oauthlib.OAuth1Session( client_key, client_secret=client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret) resp = oauth.get(base_url, params=params_dictionary) twitter_data = resp.text twitter_dictionary = json.loads(resp.text) CACHE_DICTIONARY[twitter_unique_identifier] = twitter_dictionary cache_dump = json.dumps(CACHE_DICTIONARY) f = open(CACHE_FNAME, 'w') f.write(cache_dump) f.close() return CACHE_DICTIONARY[twitter_unique_identifier]
def get_from_twitter(search_term, num=25): base_url = "https://api.twitter.com/1.1/search/tweets.json" params_diction = {} params_diction['q'] = search_term params_diction['count'] = num unique_ident = params_unique_combination(base_url, params_diction) if unique_ident in CACHE_DICTION: print("Retrieving cached data...") return CACHE_DICTION[unique_ident] else: print("Making request to Twitter API...") try: oauth = requests_oauthlib.OAuth1Session( client_key, client_secret=client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret) resp = oauth.get(base_url, params=params_diction) twitter_data = resp.text twitter_diction = resp.json() CACHE_DICTION[unique_ident] = twitter_diction cache_dump = json.dumps(CACHE_DICTION) f = open(CACHE_FNAME, 'w') f.write(cache_dump) f.close() return CACHE_DICTION[unique_ident] except json.JSONDecodeError: print('Could not decode a row')
def get_api_data(hashtag): f = open("creds.txt", 'r') (client_key, client_secret, resource_owner_key, resource_owner_secret, verifier) = json.loads(f.read()) f.close() hashtag = "%23" + hashtag protected_url = 'https://api.twitter.com/1.1/account/settings.json' oauth = requests_oauthlib.OAuth1Session( client_key, client_secret=client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret) r = oauth.get("https://api.twitter.com/1.1/search/tweets.json", params={ 'q': str(hashtag), 'count': 5 }) res = r.json() f = open('nested.txt', 'w') f.write(json.dumps(res)) f.close() f = open('nested.txt', 'r') temp = json.loads(f.read()) statuses = temp['statuses'] return statuses
def __init__(self, consumer_key, consumer_secret, domain='https://www.schoology.com', three_legged=False, request_token=None, request_token_secret=None, access_token=None, access_token_secret=None): self.DOMAIN_ROOT = domain self.consumer_key = consumer_key self.consumer_secret = consumer_secret self.request_token = request_token self.request_token_secret = request_token_secret self.access_token = access_token self.access_token_secret = access_token_secret self.oauth = requests_oauthlib.OAuth1Session(self.consumer_key, self.consumer_secret) self.three_legged = three_legged if not self.authorized: raise AuthorizationError( 'Auth instance not authorized. Run authorize() after requesting authorization.' )
def get_data_from_api(request_url, service_ident, params_diction, expire_in_days=7): """Check in cache, if not found, load data, save in cache and then return that data""" ident = create_request_identifier(request_url, params_diction) data = get_from_cache(ident, CACHE_DICTION) if data: if DEBUG: print("Loading from data cache: {}... data".format(ident)) else: if DEBUG: print("Fetching new data from {}".format(request_url)) # Get credentials client_key, client_secret, resource_owner_key, resource_owner_secret, verifier = get_tokens_from_service( service_ident) # Create a new instance of oauth to make a request with oauth_inst = requests_oauthlib.OAuth1Session( client_key, client_secret=client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret) # Call the get method on oauth instance # Work of encoding and "signing" the request happens behind the sences, thanks to the OAuth1Session instance in oauth_inst resp = oauth_inst.get(request_url, params=params_diction) # Get the string data and set it in the cache for next time data_str = resp.text data = json.loads(data_str) set_in_data_cache(ident, data, expire_in_days) return data
def tweet_text_by_id(screen_name, consumer_key, consumer_secret, access_token, access_token_secret): """ Get the text of the most recent tweet. Args: screen_name (str): twitter account handle. consumer_key (str): Twitter API Consumer Key consumer_secret (str): Twitter API Consumer Secret access_token (str): Twitter API Access Token access_token_secret (str): Twitter API Access Token Secret Returns: str: The text of the specified tweet. """ CONSUMER_KEY = consumer_key #API KEY CONSUMER_SECRET = consumer_secret #API SECRET twitter = requests_oauthlib.OAuth1Session( CONSUMER_KEY, client_secret=CONSUMER_SECRET, resource_owner_key=access_token, resource_owner_secret=access_token_secret) request_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json' params = {'screen_name': screen_name} response = twitter.get(request_url, params=params) return response.json()[0]['text']
def get_data_from_api(request_url, service_ident, params_diction, expire_in_days=7): ident = create_request_identifier(request_url, params_diction) data = get_from_cache(ident, CACHE_DICTION) if data: if DEBUG: print("Loading from data cache: {}... data".format(ident)) else: if DEBUG: print("Fetching new data from {}".format(request_url)) client_key, client_secret, resource_owner_key, resource_owner_secret, verifier = get_tokens_from_service( service_ident) oauth_inst = requests_oauthlib.OAuth1Session( client_key, client_secret=client_secret, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret) resp = oauth_inst.get(request_url, params=params_diction) data_str = resp.text data = json.loads(data_str) set_in_data_cache(ident, data, expire_in_days) return (data)
def sess(): """ Return request session authenticated w/ Twitter API """ return oauth.OAuth1Session( auth[CONSUMER_KEY_STR], client_secret=auth[CONSUMER_SECRET_STR], resource_owner_key=auth[TOKEN_STR], resource_owner_secret=auth[SECRET_STR] )
def _make_request_session(self): return requests_oauthlib.OAuth1Session( client_key=self.config.consumer_key, client_secret=self.config.consumer_secret, resource_owner_key=self.config.token_id, resource_owner_secret=self.config.token_secret, realm=self.config.account, )
def authorize(self, ownerkey, ownersecret, pincode): oauth = requests_oauthlib.OAuth1Session(get_oauth_client('https://api.twitter.com'), get_oauth_secret('https://api.twitter.com'), resource_owner_key=ownerkey, resource_owner_secret=ownersecret, verifier=pincode) tokens = oauth.fetch_access_token('https://api.twitter.com/oauth/access_token') return tokens
def get_authorization_data(self): oauth = requests_oauthlib.OAuth1Session(get_oauth_client('https://api.twitter.com'), get_oauth_secret('https://api.twitter.com')) fetch_response = oauth.fetch_request_token('https://api.twitter.com/oauth/request_token') auth_url = oauth.authorization_url('https://api.twitter.com/oauth/authorize') return (auth_url, fetch_response.get('oauth_token'), fetch_response.get('oauth_token_secret'), )