I'm trying to learn social web mining from this python book entitled Mining the social web, and I'm getting on the first few functions, here's my output:
import twitter
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
OAUTH_TOKEN_SECRET = ''
OAUTH_TOKEN = ''
auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET)
twitter_api = twitter.Twitter(auth=auth)
print twitter_api
WORLD_WOE_ID = 1
US_WOE_ID = 23424977
world_trends = twitter_api.trends.place(_id=WORLD_WOE_ID)
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/twitter/api.py", line 245, in call
return self._handle_response(req, uri, arg_data, _timeout)
File "/usr/local/lib/python2.7/dist-packages/twitter/api.py", line 276, in _handle_response
raise TwitterHTTPError(e, uri, self.format, arg_data)
twitter.api.TwitterHTTPError: Twitter sent status 400 for URL: 1.1/trends/place.json using parameters: (id=1&oauth_consumer_key=&oauth_nonce=9996743856969920690&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1406787153&oauth_version=1.0&oauth_signature=NAVC2M%2FtUQRL3xtuWZRqsuP1rCg%3D)
details: {"errors":[{"message":"Bad Authentication data","code":215}]}
What seems to be the problem here, how can I use this function correctly?
You should get access token before accessing the twitter url. Follow this link to get the access token.
Related
My code is as simple as this, but it gave me 403 error:
def init_api():
api = json.load(open('secret.json'))['twitterapi']
consumer_key = api['api_token']
consumer_secret = api['api_secret']
access_token = api["access_token"]
access_token_secret = api["access_secret"]
bearer_token = api["bearer_token"]
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
#Step 2 - Retrieve Tweets
public_tweets = api.home_timeline()
for tweet in public_tweets:
print(tweet.text)
if __name__ == "__main__":
init_api()
Traceback (most recent call last):
File "path/to/project/config.py", line 45, in <module>
init_api()
File "path/to/project/config.py", line 39, in init_api
public_tweets = api.home_timeline()
File "path/to/project/venv/lib64/python3.10/site-packages/tweepy/api.py", line 33, in wrapper
return method(*args, **kwargs)
File "path/to/project/venv/lib64/python3.10/site-packages/tweepy/api.py", line 46, in wrapper
return method(*args, **kwargs)
File "path/to/project/venv/lib64/python3.10/site-packages/tweepy/api.py", line 488, in home_timeline
return self.request(
File "path/to/project/venv/lib64/python3.10/site-packages/tweepy/api.py", line 259, in request
raise Forbidden(resp)
tweepy.errors.Forbidden: 403 Forbidden
453 - You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve
(venv)
From the error message:
453 - You currently have Essential access which includes access to Twitter API v2 endpoints only.
So, you are only allowed to use v2 endpoints, but api.home_timeline() is a v1.1 API, so the v2 endpoint for reading the timeline is:
GET /2/users/:id/tweets
Or, since you are using tweepy, use their v2 API
Client.get_users_tweets(id, *, end_time=None, exclude=None, expansions=None, max_results=None,
media_fields=None, pagination_token=None, place_fields=None, poll_fields=None,
since_id=None, start_time=None, tweet_fields=None, until_id=None,
user_fields=None, user_auth=False)
Request the elevated v1.1 access. It generally gets accepted either instantly or very quickly. May not solve your issue though, because since they folded StreamListener into Stream, I can't seem to figure it out myself.
I am trying to configure and use OAuth to authenticate and request token using Python. I have generated jira private key.
import requests
from oauthlib.oauth1 import SIGNATURE_RSA,SIGNATURE_HMAC
from requests_oauthlib import OAuth1Session
from jira.client import JIRA
import time
def read(file_path):
with open(file_path) as f:
return f.read()
CONSUMER_KEY = "OauthKey"
RSA_KEY = read("jira_privatekey.pem")
JIRA_SERVER = "http://rhtstngjira012:8080"
REQUEST_TOKEN_URL = JIRA_SERVER + "/plugins/servlet/oauth/request-token"
AUTHORIZE_URL = JIRA_SERVER + "/plugins/servlet/oauth/authorize"
ACCESS_TOKEN_URL = JIRA_SERVER + "/plugins/servlet/oauth/access-token"
oauth = OAuth1Session(CONSUMER_KEY, signature_type="auth_header", signature_method=SIGNATURE_RSA, rsa_key=RSA_KEY)
request_token = oauth.fetch_request_token(REQUEST_TOKEN_URL)
But while requesting tokens, I am getting the following error -
Traceback (most recent call last):
File "Access_Tokens_JIRA.py", line 23, in <module>
request_token = oauth.fetch_request_token(REQUEST_TOKEN_URL)
File "/PYTHON_SCRIPTS/Environments/TESTING/lib/python3.6/site-packages/requests_oauthlib/oauth1_session.py", line 287, in fetch_request_token
token = self._fetch_token(url, **request_kwargs)
File "/PYTHON_SCRIPTS/Environments/TESTING/lib/python3.6/site-packages/requests_oauthlib/oauth1_session.py", line 369, in _fetch_token
raise TokenRequestDenied(error % (r.status_code, r.text), r)
requests_oauthlib.oauth1_session.TokenRequestDenied: Token request failed with code 401, response was 'oauth_problem=signature_invalid&oauth_signature=ABC&oauth_signature_base_string=POST%26http%253A%252F%252Frhtstngjira012%253A8080%252Fplugins%252Fservlet%252Foauth%252Frequest-token%26oauth_consumer_key%253DOauthKey%2526oauth_nonce%123%2526oauth_signature_method%253DRSA-SHA1%2526oauth_timestamp%253D1630512420%2526oauth_version%253D1.0&oauth_signature_method=RSA-SHA1'
I have installed all latest libraries and have generated keys too. Could you please advise what I can change to request tokens from JIRA?
You should create API token and encode to base64 the whole String email:your-api-token.
You have to create API token for your user as described here: https://confluence.atlassian.com/cloud/api-tokens-938839638.html.
I'm trying to search tweets by geo-location. I'm trying to get tweets in New York using:
twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)
geocode = "42.3482° N, 75.1890° W"
search_results = twitter.search_geo(count=10,geocode=geocode)
try:
for tweet in search["statuses"]:
print(tweet ['text'])
it returns an error
Traceback (most recent call last):
File "D:\Projects\M Tools\Twython\My works\new.py", line 18, in <module>
search_results = twitter.search_geo(count=10,geocode=geocode)
File "C:\Python34\lib\site-packages\twython-3.2.0-py3.4.egg\twython\endpoints.py", line 818, in search_geo
return self.get('geo/search', params=params)
File "C:\Python34\lib\site-packages\twython-3.2.0-py3.4.egg\twython\api.py", line 263, in get
return self.request(endpoint, params=params, version=version)
File "C:\Python34\lib\site-packages\twython-3.2.0-py3.4.egg\twython\api.py", line 257, in request
api_call=url)
File "C:\Python34\lib\site-packages\twython-3.2.0-py3.4.egg\twython\api.py", line 198, in _request
retry_after=response.headers.get('X-Rate-Limit-Reset'))
twython.exceptions.TwythonError: Twitter API returned a 400 (Bad Request), You must provide valid coordinates, IP address, query, or attributes.
The geocode parameter should be similar to the following in the eventual Twitter search call:
41.8734,-70.6394,5mi
According to the call in the Twitter API console (https://dev.twitter.com/rest/tools/console)
Returns tweets by users located within a given radius of the given
latitude/longitude. The location is preferentially taking from the
Geotagging API, but will fall back to their Twitter profile. The
parameter value is specified by "latitude,longitude,radius", where
radius units must be specified as either "mi" (miles) or "km"
(kilometers). Note that you cannot use the near operator via the API
to geocode arbitrary locations; however you can use this geocode
parameter to search near geocodes directly. A maximum of 1,000
distinct "sub-regions" will be considered when using the radius
modifier.
In Twython, the method search_geo is used to find place_id for 'places' that are within radius of the provided geolocation. You need to use the search method like so:
from twython import Twython, TwythonError
app_key = 'your_app_key'
app_secret = 'your_app_secret'
oauth_token = 'your_oauth_tokem'
oauth_token_secret = 'your_oauth_secret'
twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)
geocode = '42.3482,75.1890,1mi' # latitude,longitude,distance(mi/km)
search_results = twitter.search(count=10, geocode=geocode)
try:
for tweet in search_results['statuses']:
print (tweet['text'])
except TwythonError as e:
print(e)
I am very new to Python having taught it to myself just a few weeks ago. I have tried to cobble together some simple script using Tweepy to do various things with the Twitter API. I have been trying to get the Search API working but to no avail. I have the following code just to simply Search the last 7 days of Twitter for keywords.
# 1.Import required libs and used objects/libs
import tweepy
from tweepy import OAuthHandler
from tweepy import API
from tweepy import Cursor
#2. GET or input App keys and tokens. Here keys/tokens are pasted from Twitter.
ckey = 'key'
csecret = 'secret'
atoken = 'token'
asecret = 'secret'
# 3. Set authorization tokens.
auth = tweepy.OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
#4. Define API.
api = tweepy.API(auth)
#5. Define list or library.
for tweets in tweepy.Cursor(api.search, q = '#IS', count = 100,
result_type ='recent').items():
print tweet.text
Every time I get the following error:
Traceback (most recent call last):
File "C:/Users/jbutk_001/Desktop/Tweety Test/tweepy streaming.py", line 25, in <module>
result_type ='recent')).items():
File "build\bdist.win-amd64\egg\tweepy\cursor.py", line 22, in __init__
raise TweepError('This method does not perform pagination')
TweepError: This method does not perform pagination
I also tried
for tweets in tweepy.Cursor(api.search(q = '#IS', count = 100,
result_type ='recent')).items():
print tweet.text
But then I get:
Traceback (most recent call last):
File "C:/Users/jbutk_001/Desktop/Tweety Test/tweepy streaming.py", line 25, in <module>
result_type ='recent').items():
File "build\bdist.win-amd64\egg\tweepy\cursor.py", line 181, in next
self.current_page = self.page_iterator.next()
File "build\bdist.win-amd64\egg\tweepy\cursor.py", line 101, in next
old_parser = self.method.__self__.parser
AttributeError: 'function' object has no attribute '__self__'
Can anyone please point me in the right direction, this has been driving me nuts for the past few days.
Thanks.
First of all, you are importing some things wrong, you might want to read more about how import works:
What are good rules of thumb for Python imports?
A working example of how to make this kind of search work:
import tweepy
CONSUMER_KEY = 'key'
CONSUMER_SECRET = 'secret'
ACCESS_KEY = 'accesskey'
ACCESS_SECRET = 'accesssecret'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
for tweet in tweepy.Cursor(api.search,
q="#IS",
count=100,
result_type="recent",
include_entities=True,
lang="en").items():
print tweet.tweet
Also, I would recommend to avoid spaces in filenames, instead of "tweepy streaming.py" go for something like "tweepy_streaming.py".
I tried so many methods, but none seem to work. Help me make a connection with linkedin using python. I have all the tokens. I have python 2.7.5. Please post a sample of basic code that establishes a connection and gets a user's name.
Below, I have done character for character like the example said, but it doesn't work.
https://github.com/ozgur/python-linkedin <---This is where I got the api and I copied it exactly. See below:
CONSUMER_KEY = '9puxXXXXXXXX' # This is api_key
CONSUMER_SECRET = 'brtXoXEXXXXXXXX' # This is secret_key
USER_TOKEN = '27138ae8-XXXXXXXXXXXXXXXXXXXXXXXXXXX' # This is oauth_token
USER_SECRET = 'ca103e23XXXXXXXXXXXXXXXXXXXXXXXXXXX' # This is oauth_secret
from linkedin import linkedin
# Define CONSUMER_KEY, CONSUMER_SECRET,
# USER_TOKEN, and USER_SECRET from the credentials
# provided in your LinkedIn application
# Instantiate the developer authentication class
authentication = linkedin.LinkedInDeveloperAuthentication(CONSUMER_KEY, CONSUMER_SECRET,
USER_TOKEN, USER_SECRET,
RETURN_URL, linkedin.PERMISSIONS.enums.values())
# Pass it in to the app...
application = linkedin.LinkedInApplication(authentication)
# Use the app....
application.get_profile()
I get this error:
Traceback (most recent call last):
File "C:/Documents and Settings/visolank/Desktop/Python/programs/linkedinapi.py", line 8, in <module>
from linkedin import linkedin
File "C:/Documents and Settings/visolank/Desktop/Python/programs\linkedin\linkedin.py", line 2, in <module>
from requests_oauthlib import OAuth1
File "C:\Python27\lib\site-packages\requests_oauthlib\__init__.py", line 1, in <module>
from .core import OAuth1
File "C:\Python27\lib\site-packages\requests_oauthlib\core.py", line 4, in <module>
from oauthlib.oauth1 import (Client, SIGNATURE_HMAC, SIGNATURE_TYPE_AUTH_HEADER)
File "C:\Python27\lib\site-packages\requests_oauthlib\oauthlib\oauth1\__init__.py", line 12, in <module>
from .rfc5849 import Client
File "C:\Python27\lib\site-packages\requests_oauthlib\oauthlib\oauth1\rfc5849\__init__.py", line 26, in <module>
from oauthlib.common import Request, urlencode, generate_nonce
ImportError: No module named oauthlib.common
Got it. For future reference you need to download the oauthlib from here https://github.com/idan/oauthlib
here is the full functional code:
CONSUMER_KEY = '9pux1XcwXXXXXXXXXX' # This is api_key
CONSUMER_SECRET = 'brtXoXEXXXXXXXXXXXXX' # This is secret_key
USER_TOKEN = '27138ae8-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXb' # This is oauth_token
USER_SECRET = 'ca103e23-XXXXXXXXXXXXXXXXXXXXXXXX7bba512625e' # This is oauth_secret
RETURN_URL = 'http://localhost:8000'
from linkedin import linkedin
from oauthlib import *
# Define CONSUMER_KEY, CONSUMER_SECRET,
# USER_TOKEN, and USER_SECRET from the credentials
# provided in your LinkedIn application
# Instantiate the developer authentication class
authentication = linkedin.LinkedInDeveloperAuthentication(CONSUMER_KEY, CONSUMER_SECRET,
USER_TOKEN, USER_SECRET,
RETURN_URL, linkedin.PERMISSIONS.enums.values())
# Pass it in to the app...
application = linkedin.LinkedInApplication(authentication)
# Use the app....
g = application.get_profile()
print g
You can get USER_TOKEN & USER_SECRET in such way (if you have CONSUMER_KEY & CONSUMER_SECRET):
import oauth2 as oauth
import urllib
consumer_key = '' #from Linkedin site
consumer_secret = '' #from Linkedin site
consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)
request_token_url = 'https://api.linkedin.com/uas/oauth/requestToken'
resp, content = client.request(request_token_url, "POST")
if resp['status'] != '200' :
raise Exception('Invalid response %s.' % resp['status'])
content_utf8 = str(content,'utf-8')
request_token = dict(urllib.parse.parse_qsl(content_utf8))
authorize_url = request_token['xoauth_request_auth_url']
print('Go to the following link in your browser:', "\n")
print(authorize_url + '?oauth_token=' + request_token['oauth_token'])
accepted='n'
while accepted.lower() == 'n' :
accepted = input('Have you authorized me? (y/n)')
oauth_verifier = input('What is the PIN?')
access_token_url = 'https://api.linkedin.com/uas/oauth/accessToken'
token = oauth.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
token.set_verifier(oauth_verifier)
client = oauth.Client(consumer, token)
resp, content = client.request(access_token_url, 'POST')
content8 = str(content,'utf-8')
access_token = dict(urllib.parse.parse_qsl(content8))
print('Access Token:', "\n")
print('- oauth_token = ' + access_token['oauth_token']+'\n')
print('- oauth_token_secret = ' + access_token['oauth_token_secret'])
print('You may now access protected resources using the access tokens above.')