I am trying to read tweets related to Zika virus and have created an application in twitter, but not able to connect to twitter through R. I have tried following commands
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "https://api.twitter.com/oauth/access_token"
authURL <- "https://api.twitter.com/oauth/authorize"
consumerKey <- "my consumer key"
consumerSecret <- "my consumer secret key"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,
consumerSecret=consumerSecret,
requestURL=reqURL,
accessURL=accessURL,
authURL=authURL)
twitCred$handshake()
immediately after this I get this error message:
Error in function (type, msg, asError = TRUE) :
Could not resolve host: api.twitter.com
Please help me I have installed following packages
library("bitops", lib.loc="~/R/win-library/3.1")
library("digest", lib.loc="~/R/win-library/3.1")
library("RCurl", lib.loc="~/R/win-library/3.1")
library("rjson", lib.loc="~/R/win-library/3.1")
library("ROAuth", lib.loc="~/R/win-library/3.1")
library("twitteR", lib.loc="~/R/win-library/3.1")
I think you are making authentication with R and Twitter too complex. From several blogs which I read, all you need to do is make one call to setup_twitter_oauth() and you should be good to go:
consumer_key <- "your_consumer_key"
consumer_secret <- "your_consumer_secret"
access_token <- "your_access_token"
access_secret <- "your_access_secret"
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
In order to obtain an access token and secret, you will have to create a Twitter application from the Twitter settings page, and generate them manually.
Please read through this blog for a step-by-step guide on how to do this.
Related
I want to get access to a user's contact list with the google contacts API.
I've managed to get the token and refresh token and I'm now trying to use then on my rails server.
The google-api-client gem seems to be the way to go but I could not find which discovered_api to use. Greg Baugues provides a great tuto to get the gmail API working. The general request seems to look like
client = Google::APIClient.new
client.authorization.access_token = user_token
service = client.discovered_api('gmail')
result = client.execute(
:api_method => service.users.labels.list,
:parameters => {'userId' => 'me'},
:headers => {'Content-Type' => 'application/json'})
pp JSON.parse(result.body)
But I could not find how to query it for contacts. Running
client.discovered_apis.each do |gapi|
puts "#{gapi.title} \t #{gapi.id} \t #{gapi.preferred} \n"
end
(from here) shows now API related to contacts and I'm wondering if this is implemented in the alpha version of the gem...
As mentioned by #abraham, the Google contact API is not supported by the discovery API. Here is how I did it in ruby from the access token using the gems google_contacts_api and oauth2 (thanks to Rael Gugelmin Cunha for pointing them to me):
client = OAuth2::Client.new(client_id, client_secret, site: url)
token = OAuth2::AccessToken.new(client, access_token)
google_contacts_user = GoogleContactsApi::User.new(token)
contacts = google_contacts_user.contacts
There might be some more elegant way to do it but this works :)
The Google Contacts API is on Google's older GData API standard and is not supported by the discovery API. There is a pretty extensive guide for plain Ruby and a helper gem. Retrieving all contacts doesn't provide a Ruby sample but the Python sample should translate pretty easily.
def PrintAllContacts(gd_client):
feed = gd_client.GetContacts()
for i, entry in enumerate(feed.entry):
print '\n%s %s' % (i+1, entry.name.full_name.text)
if entry.content:
print ' %s' % (entry.content.text)
# Display the primary email address for the contact.
for email in entry.email:
if email.primary and email.primary == 'true':
print ' %s' % (email.address)
I am using https://github.com/StepanKuzmin/erlang-dropbox to connect with dropbox api. I can use it from command line. But when I am trying to write all of this into a module and run it I am always getting an authorization error (403).
Here is part of my code:
crypto:start(),
ssl:start(),
inets:start(),
Key = "key",
Secret = "secret key",
[{"oauth_token_secret", TokenSecret}, {"oauth_token", Token}] = dropbox:request_token(Key, Secret),
io:format(Token),
Url = "https://www.dropbox.com/1/oauth/authorize?oauth_token=" ++ Token,
httpc:request(post, {Url, [],"application/x-www-form-urlencoded", []},[],[]).
I am receiving the token but not being able to authorize it from code. I have tried to token data separately but did not work out.
Thanks
Samiul Monir
I have a twitter account with an App made, currently it's setup so students can tweet from our website and as such their tweets show "via SchoolAppNameHere" at the bottom of their tweets.
Is it possible to use Twython to use the Appkey and secret key and then get auth tokens from a completely different so when I was to run the bit of code below it would tweet from an account what didn't create the app...
from twython import Twython
APP_KEY = ''
APP_SECRET = ''
OAUTH_TOKEN = ''
OAUTH_TOKEN_SECRET = ''
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter.update_status(status="test")
Any ideas would be much appreciated :)
Edit, Updated example/explanation below:
Let's say the following image's are from the account "stackoverflowapp" and the app called "Stackoverflow Test App":
Using the following bit of code would tweet from the account "stackoverflowapp" with the tweet "test" via the applicationg called "Stackoverflow Test App"
from twython import Twython
APP_KEY = 'coN_kEY_123456789'
APP_SECRET = 'cOn_sEcr3t_123456789'
OAUTH_TOKEN = 'Acc3ss_tok3N_123456789'
OAUTH_TOKEN_SECRET = 'aCCeSS_tOkEn_sEcrET_123456789'
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter.update_status(status="test")
Let's say that the following image is from the account "useraccount1" and the app is called "testing123":
So now that I have the access tokens to login to the account "useraccount1", how can I tweet via the app called "Stackoverflow test app" which was created by the user: "stackoverflowapp" example of what I tried is below:
from twython import Twython
APP_KEY = 'coN_kEY_123456789'
APP_SECRET = 'cOn_sEcr3t_123456789'
OAUTH_TOKEN = 'Acc3ss_123456789'
OAUTH_TOKEN_SECRET = 'aCCeSS_sEcrET_123456789'
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter.update_status(status="test update")
Unfortunately, I get the error:
TwythonAuthError: Twitter API returned a 401 (Unauthorized), Could not authenticate you
This is of course, possible. When you create an application in Twitter, they give you your own authentication tokens for you to use immediately, as a convenience.
Use the access token string as your "oauth_token" and the access token secret as your "oauth_token_secret" to sign requests with your own Twitter account. Do not share your oauth_token_secret with anyone.
To get keys for other accounts for the same application, you need to request more keys for each account. This is described in detail here: https://dev.twitter.com/docs/auth/obtaining-access-tokens
Since it sounds like you're going to be doing the authorization yourself, the simpler PIN-based approach should probably be used.
You're using twython, obtaining these can be done using the library: https://twython.readthedocs.org/en/latest/usage/starting_out.html#authentication
get_authentication_tokens and get_authorized_tokens are the methods you're looking for.
from twython import Twython
import sys
APP_KEY = 'coN_kEY_123456789'
APP_SECRET = 'cOn_sEcr3t_123456789'
twitter = Twython( APP_KEY, APP_SECRET )
auth = twitter.get_authentication_tokens()
print( 'Visit %s and enter your PIN: ' % auth.get( 'auth_url' ) ),
pin = sys.stdin.readline().strip()
twitter = Twython( APP_KEY, APP_SECRET, auth.get( 'oauth_token' ), auth.get( 'oauth_token_secret' ) )
tokens = twitter.get_authorized_tokens( pin )
print( 'OAUTH_TOKEN: %s' % tokens.get( 'oauth_token' ) )
print( 'OAUTH_TOKEN_SECRET: %s' % tokens.get( 'oauth_token_secret' ) )
Store OAUTH_TOKEN and OAUTH_TOKEN_SECRET some place safe and reuse at will. Also, make sure you authorize the correct account when visiting the URL and getting the PIN.
All your API calls will be made on bahalf the account that authorized access via the tokens and your via line will be your original application. Use the appropriate tokens for each account you'd like to tweet from, it's not possible to mix and match.
I want run a back-end process to get the list of channels from youtube without prompting for username password.I tried to do so with the following python code.
#!/usr/bin/python
from apiclient.discovery import build
from optparse import OptionParser
DEVELOPER_KEY = "MY API KEY"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,developerKey=DEVELOPER_KEY)
channels_response = youtube.channels().list(
part="contentDetails",
managedByMe="true",
onBehalfOfContentOwner=ownerdetail
).execute()
for channel in channels_response["items"]:
channel_id = channel["id"]
channel_title = channel["snippet"]["title"]
print "Channel details: %s - %s" % channel_id % channel_title
print "Done"
When I try to run this code I'm getting "Access Not Configured"> error in console.
My requirement is to run this successfully without prompting for username and password(since i want it as a back-end process). Any help is this would be really helpful since I'm new to this.
You can do so by getting a refresh token from OAuth2 Playground and setting it in your youtube object.
Here it explains a little more.
And a step by step video.
Iam following these steps https://secure.flickr.com/services/api/auth.oauth.html to implement oauth in my clojure prog.
Everything is working fine to step 3 with the following code. (printlns are just for checking the return values)
(def consumer-key "0000")
(def consumer-secret "0000")
(def consumer (oauth.client/make-consumer consumer-key
consumer-secret
"http://www.flickr.com/services/oauth/request_token"
"http://www.flickr.com/services/oauth/access_token"
"http://www.flickr.com/services/oauth/authorize"
:hmac-sha1))
(def request-token (oauth/request-token consumer "http://localhost:8080/authorize"))
(defn flickrauth []
(def auth-url (oauth/user-approval-uri consumer
(:oauth_token request-token)))
(println (str auth-url "&perms=write")))
After typing the auth-url to my my browser can authorize the access with write permissions.
(defn get-access-token [oauth-token verifier]
(println "CONSUMER: " consumer "REQ TOKEN: " oauth-token "verifier: " verifier)
In the following code i only got "oauth_problem=token_rejected, status 401". So i guess there is a problem with exchanging the request token for an access token...
(def access-token-response (oauth/access-token consumer
request-token
verifier))
(println "ACCESS TOKEN RESPONSE: " access-token-response)
Short summary...
I get a request token and verifier, but in access-token-response is another oauth_token used..and i don't know why.
Thanks for any help and hint!