I am trying to extract tweets from twitter within certain time frame. code
import tweepy
import csv
from tweepy import OAuthHandler
consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
#csvFile=open('tweets.csv','a')
#csvWriter=csv.writer(csvFile)
for tweet in tweepy.Cursor(api.search,
q="#tatamotors",
lang="en",
since="2016-01-12",
until="2016-06-07").items():
print (tweet.created_at, tweet.text)
#csvWriter.writerow([tweet.created_at,tweet.text.encode('utf-8')])
i am getting - "name error: name api is not defined"
You're missing a line before the call of api, construct the api first using this:
api = tweepy.API(auth)
Related
I have set up my Twitter API
consumerKey <- "xx"
consumerSecret <- "xx"
accessToken <- "xx"
accessTokenSecret <- "xx"
setup_twitter_oauth(consumerKey, consumerSecret, accessToken, accessTokenSecret)
This was returned (which has been successful before)
[1] "Using direct authentication"
Now, when I am trying to stream tweets for a certain amount of time, it seems to be successful as its running but it returns the file under values and not under data and shows "NULL"
btw21test <- stream_tweets("#BTW21",
timeout = (60*5),
parse = FALSE,
file_name = "BTW21.json"
)
## read in JSON file
btw21test <- parse_stream("BTW21.json")
NULL
Does anyone know if this is because of the Twitter API? I have used the exact same code successfully in the past. Thanks!
I have been banging my head over this the whole day. I am trying to access StockTwits API (https://api.stocktwits.com/developers) from an R session. I have earlier accessed the twitter API (via rtweet) without hassles.
I have created an app and got the client id and key (the below are just examples).
app_name = "some.name";
consumer_key = "my_client_id";
consumer_secret = "my_client_key";
uri = "http://iimb.ac.in" # this is my institute's homepage. It doesn't allow locahost OR 127.0.0.1
scope = "read,watch_lists,publish_messages,publish_watch_lists,direct_messages,follow_users,follow_stocks";
base_url = "https://api.stocktwits.com/api/2/oauth"; # see https://api.stocktwits.com/developers/docs/api
The procedure is to create an oauth2.0 app and endpoint. Then call oauth2.0_token.
oa = httr::oauth_app(app_name, key = consumer_key, secret = consumer_secret, redirect_uri = uri);
oe = httr::oauth_endpoint("stocktwits", "authorize", "token", base_url = base_url);
mytoken = httr::oauth2.0_token(oe, oa, user_params = list(resource = base_url), use_oob = F); # use_oob = T doesn't work.
After firing the above, it takes me to the browser for sign-in. I sign-in and it asks me to connect. After that, I am taken back to my URI plus a code, i.e. https://www.iimb.ac.in/?code=295ea3114c3d8680a0ed295d52313d7092dd90ae&state=j9jXzEqri1
Is the code my access token or something else? The oauth2.0_token() call keeps waiting for the code since the callback is not localhost. I didn't seem to get a hang of that.
I then try to access the API using the above code as access token but I am thrown "invalid access token" error. The format is described in https://api.stocktwits.com/developers/docs/api#search-index-docs
Can someone tell me what I have missed? If required I can share my app_name, consumer_key and consumer_secret for replication.
I am a API newbie trying to access a certain webpage on Indeed.Com website using it's API for my employer.I am not able to receive token once I am sending the CLIENT ID AND SECRET ID.Any help is appreciated.
The Indeed API documentation : http://opensource.indeedeng.io/api-documentation/docs/oauth/#before-you-start
Indeed Employer API : https://employers.indeed.com/api/v1
options("httr_oob_default" = T)
options("httr_oauth_cache"= T)
Sys.setenv("HTTR_SERVER_PORT" = "1410/")
library(httr)
library('httpuv')
#Token Request URL
request_url = 'https://secure.indeed.com/'
#Token Access URL
access_url = 'https://secure.indeed.com'
#Authorization URL
authorization_url = 'https://secure.indeed.com/account/oauth?client_id="Your client ID" &redirect_uri=http://localhost:1410/&response_type=code'
#Saving client ID and Secret ID
key <- 'YOUR CLIENT ID'
secret <- 'CLIENT SECRET ID'
#Sending OAUTH Request
myinapp = oauth_app(NULL, key, secret)
in_app = oauth_endpoint(request_url, authorization_url, access_url)
#Getting Token
token = oauth1.0_token(in_app, myinapp)
#Authenticating Token
mytoken = config(token = token)
I got some problems accessing tracking API via 0auth;
I have sign key, secret and Authorization_key.
Sign key and secret should form a sig file as I understand and authorization_key that should be added as header.
The code:
auth_info <-
read.csv2('./Data/auth_info.csv',
encoding = 'UTF-8',
stringsAsFactors = F)
express <-
oauth_app("express", key = 'Ur1475', secret = auth_info$sign_key)
sig <- sign_oauth1.0(express)
order <-
GET(
"http://www.express.ru/api/v2/getOrder?orderNumber=WEBN3141018",
authenticate("Ur1475", auth_info$sign_key),
add_headers(Authorization = auth_info$auth_key),
sig
)
order_info <- content(order)
order_info
Returns me auth error;
Official api documentation tells that should be formed md5(client key . URL . GET params . POST params . sign key).
Also I've looked at 0auth function code it does not use http.
Could it cause the problem?
Could you please comment on how to get data from API via R?
API doc (in russian)): https://www.express.ru/docs/APIExpressRu.pdf
The major of API's (Google, Twitter, Facebook) need a token that you must to obtain from an oauth authentication. Depending from the API's you must to use the functions GETor POST. And you must to provide your key encoded in base64. So what I think you need is:
First: Authenticate.
if(!require("jsonlite")){library("jsonlite")}
if(!require("httr")){library("httr")}
#Create your own appication key
consumer_key <- "your consumer key" #needed
consumer_secret <- "your consumer secret" #some api's don't need.
#Use basic auth
secret <- jsonlite::base64_enc(paste(consumer_key, consumer_secret, sep = ":"))
req <- httr::POST("https://url.of.your.api/oauth/token", #"oauth" and "token" are not parameters, they are fixed.
httr::add_headers(
"Authorization" = paste("Basic", gsub("\n", "", secret)),
"Content-Type" = "application/x-www-form-urlencoded;charset=UTF-8"
),
body = "grant_type=client_credentials"
)
#Extract the access token
token <- paste("Bearer", httr::content(req)$access_token)
Next: Make a request.
url<-"http://url.of.your.api/search?parameter1=value¶meter2=value"
req <- httr::POST(url, httr::add_headers("Authorization" = token))
json <- httr::content(req)
json
As I said, some API's need GETand others POST, try both.
I am trying to post an update to Twitter using the api. Following the steps at https://github.com/hadley/httr/blob/master/demo/oauth1-twitter.r, I can get the GET call to work, but I haven't managed to get the POST to work. Here is the relevant code:
library(httr)
library(httpuv)
oauth_endpoints("twitter")
myapp <- oauth_app("twitter",
key = "key"
secret = "secret"
)
twitter_token <- oauth1.0_token(oauth_endpoints("twitter"), myapp)
#this works fine
req <- GET("https://api.twitter.com/1.1/statuses/home_timeline.json",
config(token = twitter_token))
#this doesn't work. Returns error 403: status is forbidden
POST(url = "https://api.twitter.com/1.1/statuses/update.json",
body = "test",
config(token = twitter_token)
)
Any solution that allows a status update (Tweet) from R is acceptable.
The docs say you need to pass a parameter status as a query parameter, not in the body, which makes sense b/c it can only be 140 characters long anyway.
POST("https://api.twitter.com/1.1/statuses/update.json",
query = list(status = "testing from R"),
config(token = twitter_token)
)