in this video I'm going to look at how you can write code in a node application
to connect to the Twitter API, using the node package twit.So it assumes the
previous couple videos where you've got node installed, you've got the twit package
installed.And now you're ready, you're readyI, I'm ready we're ready here to
actually write some code.So the first thing I need to do is actually make a
JavaScript file.Like this particular project just has the modules directory
in the package.json file.So i need to add a file and I'm going to call it a
bot.js because this is the file that I'm going to write some code. and I'm
just going to make sure that the bot is starting.
I'm just gonna make sure that this is working so i'm just gonna put a little
bit of code in the file to make sure I could run it and things are happening. So
if I'm here I hope.... you can see I did that already clear clear clear.I'm going to say node.js
and I'm gonna hit enter and see the code starting. Here's a little
bit aside, you might see people when they're working with node use "npm start"
So I got all these errors because it's like ....
actually even a dislike error loggin file. So one thing here in the package
you can add under scripts.. I could sort of assign a script to the keyword start
and I might say node bot.js.So here what I'm doing: if i ever say npm
start run ndoe.js and there we go. I can now say npm start and you can see
the same thing has happened. So for us this doesn't really matter so much but
the reason why I bring that up is you might see it an other example in some
places where you have to ike put your notes
somewhere else. You've got to use this npm start thing and quickly using the
some a web service... I got off track there so edit
that part out! Anyway ok so this is not any good like the bot is
starting at the bottom end and the bot is not doing anything.
What you need to do now is actually use the twit package. So the way that
works.I'm gonna come back here to my bot.js code and save our twit equals
require('twit')t because I require i required twit
I have a twitter I require twit being mysel! Anyway so this is like an import
statement this is the same thing that you
might say like in Java or processing import you know something.something. you
know library file in JavaScript index.html, file you have a reference to
another javascript library like jquery or p5. So this for node this is like
import the package import the library import the module whatever you want to
call it it. So once I've done that I could start making use of it.
How do I make use of it? so the very first thing you need to do is
authenticate... with this
OAuth thing so and you know this could be a complicated endeavor and one of the
reasons why I'm so happy to just use this package is I really have to
understand what OAuth is, how it works
who you.... It's just going to authenticate, it just gives me some code
to do that. So how do you find that? I've got this already loaded
here in the browser, but you can see this is that github repository for the twit
package. You can actually just google "twit package node" and we'll probably
get this link right here and in the Readme file.If I scroll down there's a
lot of information "how to install it" and here you can see this is an important
bit of code. So I'm going to grab this bit of code to copy it and I'm going to
put it into my code so you can see this is what I first need to do. I need to
make a new twitter object a new twitter object and I need to have a consumer key
a consumer secret and access token and an access
token secret.All of these things are secret code values that are tied to the
particular twitter account that's going to be associated with this particular
node app. Now there is kind of a question here of how do you.. what happens if
somebody sees your access token and you're going to see mine for example.
And then I'm gonna talk about at some point in these videos how to usesome
techniques for how you might keep those hidden even as you open source or
publish your code. But for the time being, just gonna like put all my stuff right
here in my code and I'll show you in other ways you might grab your access
token from another source at some point. So first how do you find out your access
tokens? Well back to Chrome here I am logged into Twitter through an account
this is like a test account that I set up called A2Z ITP.
II teach a course at ITP called programming from
A2Z, I'm not very good at titles of course apparently! And you can see here that
so one of the things i would emphasize if you're starting to work with Twitter
box or mess around with the API you might make yourself a new twitter
account with a new gmail address just so in case like your API Keys end up out
there or you screw something up or Twitter blocks you because you did
something like tried to like post too often too fast you're not like ruining your
precious special like personal Twitter account. So that might be one thing I
recommend now. There is a bit of trick to this , an issue
with this now.. this is just the twitter page where you need to go in order to
work with the twitter API and twitter stuff from code is dev.twitter.com
so you can see here if I go to dev.twitter.com
I'm gonna be, I'm still logged into my account but I'm on this developer page
for this documentation, hopefully tutorials and lots of information. What
I'm looking for is this particular link down here "manage your apps" . So you
know this is really designed for somebody who's like making an iphone
twitter app that is going to be the best way to read tweets ever
yeah kind of like bing! But we're just like we want to just mock around and
make weird stuff happen on Twitter so this site is honestly the most
friendly to us in that sense. But on but what you're doing really your node
programme your little node program is a app. So if I click "manage your apps"
you can see here i already have two apps that I've been messing around. You're
going to need to hit this create new app button because you're gonna have to make
a new one. Now here's a little bit of a problem
twitter is not gonna let you make an app or get API keys unless you have a phone
number associated with your account. So you have a couple options here number
one, you could just use your real phone number, if it is associated with another
twitter account you could delete it from that twitter account put it with this
one.
Get your app keys and then move it back or you could sign up with a service like
twilio or google voice that gives you like a free phone number. I
think you might need to be able to get text message on the phone number ..but not
entirely sure.
But that's one thing we're gonna have to be aware of. I use the google
voice number for this so I can go here, I can... actually let's create a new one
I don't know maybe this is a bad idea. I'm gonna say a youtube live stream test
and it's just my description. It is exactly the same my website website doesn't
really matter i'll just put in shiffman.net because you don't have a website
associated with this you're just messing around.
Callback URL you can totally ignore this for now. These are things for more
other scenarios that don't matter to us. I'll hit yes I agree and I'm going to
create my Twitter application and hopefully this is going to work and you
can see now there's information.
None of this matters to me the only thing that matters to me is key and
access token. This is the stuff that I need in order to put ... here because I want
this node application to connect to twitter I just need those keys. So I go
back to the browser and make this bigger I click on keys and access tokens you
can see there we go.
Consumer key api key that's this one I'm going to copy it and I'm gonna go to my
code and then put it right here. Next thing I'm going to do is I'm going to
get the consumer secret API. I'm gonna get this all and it all. But I need
an access token and access token secret. Now why is their consumer key why is there an
access token? This has to do with other scenarios of an app that you make that
might authenticate to somebody else's account and all sorts of complex things.
that doesn't matter to us, we just need to be like: these are the four things we need.
I can find those four things on that page so I'm gonna scroll down and you
can see like ah I need to create an access token now. Here's the thing, if I
really cared about you guys not hacking my twitter account (which I don't please
hack it i think it would be interesting!). I could as soon as I finish making this
video hit regenerate consumer key and secret so that does those keys are no
longer valid but in case you publish your key number by accident, note that's
there. The next thing I can do here is create my access token, so I'm gonna
click that button and now as I scroll down ...and didn't seem to work I now have
a access token so let me get the host access token...
and I'm gonna put that here and now i'm going to get access token secret and i'm
going to put that here.
Copy paste .... I was like daydreaming! Simply when , you like
start copy-pasting you just ... I thought i was like thinking about something else...
that I have to do later and like a potluck dinner at my son's school or
something to bring kale salad like what?!
But let's go back to this.So now access tokens secret okay so right now i have a
so I'm done now Twitter API developer page. You know you might want to go back
there read through the documentation search around there might be stuff ,
things that you need here. But I'm really done with this and I could come back to
what happened to the twit github page, I can come back to twitter.
This was going so well ! So go to this page I can come back to the stuff down here so
I've done the the work of of creating this object that now connects to twitter.
Nw one thing that I might do is just kind of like a run my code again like...
Let's just see like do I get even any any errors? Like, does this even
work because in theory if those access code keys were invalidated hopefully we'll get
an error so i'm going to run again.
It seemed to have connected we could sort of like check by like, I could
get rid of that there that capital D and run it again. And so it didn't give me an
error! That's interesting to see that. I would love to like have to look further
like probably when I actually made the API query that perhaps I would have a
problem. Okay sorry about that aside. So now what do we need to do
okay so uh so you know what I'm gonna I'm gonna break this up into two videos.
I just went over here, thinking back. I'm gonna get to actually like, executing the
twitter API functions in just like a new video. So you know you can stop this wone
to move on to the next one but let's take a minute now to just look at
this question of; what if i want to decoupled the fact that all of this
stuff these keys are just in my code,
in my nodee program because this can actually be convenient for a lot of
reasons. Ultimately you might want to make a whole bunch of bots and you just
want to have like your keys all in one file that you're always pulling from. Or
you might want to like upload this as an example code but not have the keys in it.
So and and this is kind of actually also like a little extra tidbit of how things
work in node that I think might be useful to you.
So something that I could do is i can make another file so I'm gonna make a
new file. I'm going to call it config.js. Now I could call this anything that
I want, but i'm going to call it config.js because it's sort of like..
configure my Twitter config information and something that i'm going to write is
I'm going to say module.exports = { and then I would put brackets, I hopefully I
got that right I'm gonna check.
So what am I doing here something that you can do with node is store code that
you might want to reference in another file. The main file is bot.js. What about
when you want to pull objects or other piece of code just like I'm using the twit
package. So I'm making myself my own little mini package for my own code
Now all I want to put in here is my API keys but this is... all I want to do is put
my API keys in here but you can actually put a lot more in here. So if I make a
file called something.js put module.exports in it. Now I can go back to bot.js
and I can take all of this and take it out and I can put it in here so we
can see now that I now have made this module that exports into an object that
has the keys in it. And now in bot.js I can save our config = require('config')
So look at this, this is now how I'm saying require().Just like I said
require('twit') because of requiring the twit package now saying require config
which is just require the stuff that's in my config.js file. And what's now in
this variable if I say console.log.config and I run this code.
Haha it cannot find module configured so I knew I missed something there so it
has to do with the way the paths are set up.
It's looking for like a folder called config and really I just have a
JavaScript file called config so I can fix that I think by saying like . /
which is like go up a directory and then look for config.js something like
that.
Whatever it is it's just I think it'll just work and i'll feel happier about it
so now I could go back and try that again and you work.
Oh oh no it did work I could work it just like I got what didn't work is that
I didn't authenticate to Twitter's I didn't actually get the stuff so let me!
Let me let me run that again just to show you how to run that again and you
can see it's spinning back out all that information. So now that I have that
information in that config variable I can now say make a new twitter object
with config. So this is a way of exactly the same thing I now have all those API
keys in a separate file I'm importing those files and then I am making a new
twitter object with the stuff that's in that file and I can now say run it again.
Oops I must have not hit save.I can run it again and you can see it's
working!
So the reason why this is useful is there's lots of ways now if I were to
publish this that I could hide the config.js file and when I link to the
source code that will accompany this video. You'll see
that i have a file called like configMT.js with some instructions but I'm
not actually including the real config.js final.
This was a bit of an addendum on that maybe just maybe not watching this anymore
you're not interested in that addendum. But this is also going to simplify
things and allow us as we make more and more examples to always just you know
feel bring that config.js file with with with us as we go.
Ok so what I would say to you now is before you get to the next
video
what you should do is get your twitter account, go get a phone number
associated with it and create an app. Get all those keys
see if you can get the code running we authenticate and finally finally finally
in the next video
you're actually going to be able to start using the
api and posting tweets. Which you know suddenly from there it's gonna be rapid
fire to making Twitter box like all of this like so much of this is just
getting set up in the environment
ok.