I'm using tweepy trying to get history tweets from a specific user, for example "IBM"
In TwitterAPI I can use
api = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret)
end_point = 'tweets/search/30day/:development'
param = {'query': 'from:IBM', 'maxResults':'500'}
r = TwitterPager(api, end_point, param)
To specify the end point. I'm wondering how I can do similar in Tweepy.
You cannot do this with Tweepy. tweets/search/30day is a Premium Search endpoint. Tweepy does not support the Premium Search API.
Related
I want to pull emails by Graph API from client inbox using python.
I started with a tutorial and successfully experimented over my personal inbox.
My problem,
Every time my code generates an authorization URL.
I have to browse through it (using web browser library) , sign in using my credentials and copy paste the authorization code for generating access token.
Which is a lot of manual work every time.
Question :
Is there a way to automate the whole process of token generation ?
Such that my client only shares his application id and client secret, and email is pulled without his sign in credentials ?
My code is attached below -
import msal
from msal import PublicClientApplication
import webbrowser
import requests
import pandas as pd
APPLICATION_ID="app id"
CLIENT_SECRET="client secret"
authority_url='https://login.microsoftonline.com/common/'
base_url = 'https://graph.microsoft.com/v1.0/'
endpoint_url = base_url+'me'
SCOPES = ['Mail.Read','Mail.ReadBasic']
client_instance = msal.ConfidentialClientApplication(client_id = APPLICATION_ID,client_credential = CLIENT_SECRET,authority = authority_url)
authorization_request_url=client_instance.get_authorization_request_url(SCOPES)
#print(authorization_request_url)
# browsing authorization request URL for retrieving authorization code.
webbrowser.open(authorization_request_url,new=True)
# Manually pasting authorization code.
authorization_code='authorization code from authorization URL'
access_token = client_instance.acquire_token_by_authorization_code(code=authorization_code,scopes=SCOPES)
access_token_id=access_token['access_token']
# Rest of the codes are for hitting the end point and retrieving the messages
Any help with code suggestions will be much appreciated.
Thanks in advance
If you would like to authenticate only with a clientId and clientSecret, without any user context, you should leverage a client credentials flow.
You can check this official MS sample that uses the same MSAL library to handle the client credentials flow. It is quite straightforward, as you can see below:
import sys # For simplicity, we'll read config file from 1st CLI param sys.argv[1]
import json
import logging
import requests
import msal
# Optional logging
# logging.basicConfig(level=logging.DEBUG)
config = json.load(open(sys.argv[1]))
# Create a preferably long-lived app instance which maintains a token cache.
app = msal.ConfidentialClientApplication(
config["client_id"], authority=config["authority"],
client_credential=config["secret"],
# token_cache=... # Default cache is in memory only.
# You can learn how to use SerializableTokenCache from
# https://msal-python.rtfd.io/en/latest/#msal.SerializableTokenCache
)
# The pattern to acquire a token looks like this.
result = None
# Firstly, looks up a token from cache
# Since we are looking for token for the current app, NOT for an end user,
# notice we give account parameter as None.
result = app.acquire_token_silent(config["scope"], account=None)
if not result:
logging.info("No suitable token exists in cache. Let's get a new one from AAD.")
result = app.acquire_token_for_client(scopes=config["scope"])
if "access_token" in result:
# Calling graph using the access token
graph_data = requests.get( # Use token to call downstream service
config["endpoint"],
headers={'Authorization': 'Bearer ' + result['access_token']}, ).json()
print("Graph API call result: ")
print(json.dumps(graph_data, indent=2))
else:
print(result.get("error"))
print(result.get("error_description"))
print(result.get("correlation_id")) # You may need this when reporting a bug
The sample is retrieving a list of users from MS Graph, but it should be just a matter of adapting it to retrieve the list of emails of a specific user by changing the "endpoint" parameter in the parameters.json file to:
"endpoint": "https://graph.microsoft.com/v1.0/users//users/{id | userPrincipalName}/messages"
You can check here more information regarding the MS Graph request to list emails.
register your app
get your tenant id from azure portal and disable mfa
application_id = "xxxxxxxxxx"
client_secret = "xxxxxxxxxxxxx"
#authority_url = "xxxxxxxxxxx"
authority_url = 'xxxxxxxxxxxxxxxxxxxx'
base_url = "https://graph.microsoft.com/v1.0/"
endpoint = base_url+"me"
scopes = ["User.Read"]
tenant_id = "xxxxxxxxxxxx"
token_url = 'https://login.microsoftonline.com/'+tenant_id+'/oauth2/token'
token_data = {
'grant_type': 'password',
'client_id': application_id,
'client_secret': client_secret,
'resource': 'https://graph.microsoft.com',
'scope':'https://graph.microsoft.com',
'username':'xxxxxxxxxxxxxxxx', # Account with no 2MFA
'password':'xxxxxxxxxxxxxxxx',
}
token_r = requests.post(token_url, data=token_data)
token = token_r.json().get('access_token')
print(token)
I am new to REST and was wondering if this is right?
#app.route('/addticket/<name>')
def addticket(name):
tick = Tickets(event_name=name, redeemed=False)
try:
db.session.add(tick)
db.session.commit()
except:
return 'There was an issue adding a ticket'
viewer = Tickets.query.filter(Tickets.event_name==name).all()
return render_template('view.html', viewer = viewer, title = name )
I am required to create a service (backend) application that exposes the RESTful
endpoints to accomplish the task.
In this method, I am adding an additional ticket to an event.
Just confused if I have to return JSON serialised.
You have to create a seperate endpoint to fetch data and parse it to json instead of using the same endpoint to put and fetch data.
#app.route('/getticket/<name>',methods=['GET'])
def getticket(name):
viewer = Tickets.query.filter(Tickets.event_name==name).all()
jsonify({'response': viewer })
How do I set this up correctly? The description seems straight-forward
import os
import json
from ibm_watson import SpeechToTextV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
api_key='XYZ'
service_endpoint='https://api.eu-gb.speech-to-text.watson.cloud.ibm.com'
authenticator = IAMAuthenticator(api_key)
speech2text = SpeechToTextV1(authenticator=authenticator)
speech_to_text.set_service_url(service_endpoint)
speech_models = speech_to_text.list_models().get_result()
print(json.dumps(speech_models, indent=2))
Which API key do I have to pass to the 'IAMAuthenticator'?
I don't quite understand if I first have to create an Instance/Resource (https://cloud.ibm.com/catalog/services/speech-to-text) and use that API key, or only create and use an IBM Cloud API key (https://cloud.ibm.com/iam/apikeys). I have tried different combinations of the above, but cannot connect successfully, raising ApiException: Error: Provided API key could not be found, Code: 400
Actually, you use IAMAuthenticator with the API key. Don't forget to import it.
See the example in the API reference and more information about the SDK in the README. Can you make a call with curl with the same API key and URL
(The BasicAuthenticator is specifically for username and password auth.)
To use the APIKEY directly you must use the BasicAuthenticator instead of the IAMAuthenticator from ibm_cloud_sdk_core.authenticators. See the code snippet below:
from ibm_watson import SpeechToTextV1, ApiException
from ibm_cloud_sdk_core.authenticators import BasicAuthenticator
...
# Basic Authentication with Watson STT API
stt_authenticator = BasicAuthenticator(
'apikey',
'<insert_your_watson_stt_apikey_here>'
)
# Construct a Watson STT client with the authentication object
stt = SpeechToTextV1(
authenticator=stt_authenticator
)
# Set the URL endpoint for your Watson STT client
stt.set_service_url(
'<insert_your_watson_stt_endpoint_url_here>'
)
# Read audio file and call Watson STT API:
with open(
os.path.join(
os.path.dirname(__file__), './.',
'audio_sample.flac'
), 'rb'
) as audio_file:
# Transcribe the audio.flac with Watson STT
# Recognize method API reference:
# https://cloud.ibm.com/apidocs/speech-to-text?code=python#recognize
stt_result = stt.recognize(
audio=audio_file,
content_type='audio/flac',
model='pt-BR_BroadbandModel'
).get_result()
# Print STT API call results
print(json.dumps(stt_result, indent=2))
...
I hope this answer helps you.
I am trying to get the tweets in a 10 miles radius around Twitter's headquarters in San Francisco location using Tweepy API. I have authenitication information (api key, etc.) in an external .json file.
Below is my code:
import tweepy as tw
import json
import pandas as pd
import datetime
secrets = json.loads(open('secrets.json').read())
api_key = secrets['api_key']
api_secret_key = secrets['api_secret_key']
access_token = secrets['access_token']
access_token_secret = secrets['access_token_secret']
auth = tw.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth)
try:
api.verify_credentials()
print('Authenticated')
except:
print("Error authenticating")
# After running the above, I get status 'Authenitcated'
start = '2020-09-01'
end = '2020-09-26'
location = '37.7749, 122.4149, 10miles'
query = "*"
max_tweets = 1000
tweet_search_results = [status for status in tw.Cursor(api.search, query=query, geocode=location, since=start, until=end).items(max_tweets)]
After running the above, I get this error:
TweepError: Twitter error response: status code = 403
This status code means access to the requested resource is forbidden for some reason. The server understood the request, but will not fulfill due to the client-related issues.
Within the same code I am able to get the list of tweets for a specific user name, but without geolocation data.
Why am I getting 403 when trying to get geolocation tweets?
Is there a way to implement a multi-thread streamming in twitter using tweepy?
Currently I am using the following code:
import tweepy
auth = tweepy.OAuthHandler(TWITTER_APP_KEY, TWITTER_APP_SECRET)
auth.set_access_token(TWITTER_KEY, TWITTER_SECRET)
api = tweepy.API(auth)
stream_listener = tweepy.StreamListener()
stream = tweepy.Stream(auth=api.auth, listener=stream_listener)
stream.filter(track=["token"], async=True)
stream.filter(track=settings.TRACK_TERMS)