Introducing TwitterOAuth Package0:01
This connection has already followed me for quite some years now, and it connects my application with my favorite social platform, Twitter. X. I don't know how you want to call it. But the name of the package probably says already all. It's called Twitter OAuth. We're here on the website and documentation of the Twitter OAuth package, the most popular PHP library for use with the Twitter OAuth REST API. And I can only agree. As I mentioned, I already worked with this for a very long time. So thank you, Abram, for doing all this work.
As I mentioned, I already worked with this for a very long time. So thank you, Abram, for doing all this work. So this is a really solid package, even in those hard times currently where Twitter X are changing a lot on their sites with the API. Still, this is working very nicely. But how does this work? I don't like if documentations don't show examples right at the top. So we have to scroll down here for an example, which we also want to use.
So we have to scroll down here for an example, which we also want to use. And this is this one here. Because we want to post a Twitter message, and this is what it looks like if this package is set up correctly. We're going to post a new status update. This, by the way, also changes with the new API 2.0, which we are using. But basically, this is what we want to do. We want to post a new tweet to our Twitter account.
But basically, this is what we want to do. We want to post a new tweet to our Twitter account. And for this, we're going to use the real account, the real-life account of mine from Lara Streamers. Not sure if you know this service of mine, where I share a lot of streams from the Lara community. So we're going to post here a new tweet on this channel here. So I think it's more fun if it's live. Okay, so how are we going to do this? Let's start at the beginning.
Installing and Configuring Keys1:47
Okay, so how are we going to do this? Let's start at the beginning. First, of course, we're going to need to install this package. Let's do this. All right, then what's next? This is the most important thing, creating a new connection with the TwitterOAuth class and then providing all those different keys, which we're going to need. So what we are going to do here is right in our web.php file.
which we're going to need. So what we are going to do here is right in our web.php routes file of Laravel application, we're going to try to do this together here now. So first, we're going to bring in this class here. And then we need from a Twitter app, which you have to create yourself on the Twitter development platform, you need to create an app where you get the consumer key, consumer secret, access token, access token secret. So this means in your .env file, you need to have all those set up,
So this means in your .env file, you need to have all those set up, which you have to do yourself and then providing my own credentials here for the LaraStreamers account, but you won't see this. And then also very important, you want to also add this to your services.php config file. And that's also what I have already done here, created a new Twitter key with all those four things that we need in order to create a new connection to our Twitter app.
created a new Twitter key with all those four things that we need in order to create a new connection to our Twitter app. All right, so these are the four things that we need. And I hope you have set this up correctly if you're trying to do this yourself right now. And yeah, make sure that all those gets to the right key in your config file and then also to the correct environment variables that all those are set up correctly. All right, so this is the first thing creating a new connection. Then the next thing I already mentioned
Posting a Tweet via API v23:31
All right, so this is the first thing creating a new connection. Then the next thing I already mentioned that we want to use the new Twitter API v2. And we have to set this as well on the connection. You can do this by, what is it called? setApiVersion. And we want to use version two, which is the current one. And then what we're now going to do on the connection, we want to post to our API and we want to create a tweet.
we want to post to our API and we want to create a tweet. And the endpoint is called tweets. And then we're going to provide an array. And inside here, we just have to provide a text. All right, what do we want to send to our LaraStreamers channel? Maybe let's just go with Twitter or off is awesome. And we need some nice emojis. Some kind of star like this. I think this is good.
Some kind of star like this. I think this is good. And let's also return what we get back from here just to make sure that we see a response. Now we go to level small packages. And I think the endpoint was called tweet. And we do get something back, which is already good. And if we go now back to our Twitter client here, we should see this new message on my profile. And here we go.
we should see this new message on my profile. And here we go. Twitter or off is awesome. And how cool is this that this was so easy to get this done. So take a look at the code again. We are creating our connection with the credentials. We're using the second API version, and then we're just sending a tweet with this. I have to admit that setting up the Twitter app on Twitter development platform and to get those credentials.
I have to admit that setting up the Twitter app on Twitter development platform and to get those credentials and that they work with the correct endpoint and with what you want to do currently isn't always that easy because there are a lot of changes on Twitter X, however you want to call this. So yeah, I also had an application which didn't work for one week and then the next week it was working again. So yes, if it doesn't work the first time you tried, please don't give up.
Refactoring with ServiceProvider Binding5:34
So yes, if it doesn't work the first time you tried, please don't give up. There could be some things on the Twitter site going on and it probably isn't your fault. And since this is a little bit ugly having all this in here, I want to show you how I work with this usually. So usually I create a ServiceProvider, which I have already prepared, which is called TwitterServiceProvider. And here what I'm going to do is I bind to the TwitterAuth class.
which is called TwitterServiceProvider. And here what I'm going to do is I bind to the TwitterAuth class. Now this new instance of the TwitterAuth class where we're going to provide all the credentials, we are already setting the API version and we're going to return just this. So this means also for our example here what I could do now instead of all of this is TwitterClient resolve the TwitterAuth class. This is where we bind the implementation.
is Twitter client resolve the TwitterAuth class. This is where we bind the implementation. And let's also name this and let's call it TwitterAuth is still awesome. And let's give it one more try. Here we go. This also seems to be working. Let's get back here. We should see now another tweet here. And we do. Let's like it.
Exploring Other API Calls6:43
And we do. Let's like it. Yeah, I like it. All right. So this is now a nice way to bind this to the service container and use this service. So I think you should do something like this if you're using this package inside your next application. Of course, there are a few other things that you can do with this package. So if we go here, you can see you can also get some specific stuff.
Of course, there are a few other things that you can do with this package. So if we go here, you can see you can also get some specific stuff. You can get users as well. I think that's what we're going to do here. And yeah, everything else you can do with this package you can find here on this documentation page. Let's break down what we just learned. Whenever you need to integrate Twitter to your application to send automatically tweets or get some data from Twitter, then TwitterAuth is your package.
to send automatically tweets or get some data from Twitter, then Twitter auth is your package. Thank you, Abraham Williams.
