I built a twitter bot that automatically tweets content generated by artificial intelligence using GPT-3. Learn how to build your own twitter bot from scratch with Node.js and the OAuth 2.0 protocol.  https://fireship.io/lessons/twitter-bot-oauth2-tutorial/

#ai #js #tutorial

Follow Bob https://twitter.com/boblikescode
Follow Fireship https://twitter.com/fireship_dev

🔗 Resources

Source Code https://github.com/fireship-io/gpt3-twitter-bot
Twitter API https://developer.twitter.com/
OpenAI https://openai.com/

📚 Chapters

00:00 AI Twitter Bot
00:42 OAuth 2.0 Crash Course 
02:36 Project Setup
04:38 Step 1. OAuth URL 
05:44 Step 2. OAuth Callback
06:41 Step 3. Use the Twitter API
09:00 AI-generated Faces

🔥 Get More Content - Upgrade to PRO

Upgrade to Fireship PRO at https://fireship.io/pro
Use code lORhwXd2 for 25% off your first payment. 

🎨 My Editor Settings

- Atom One Dark 
- vscode-icons
- Fira Code Font

🔖 Topics Covered

- API Authentication Basics
- How to use the Twitter API?
- How to build a twitter bot?
- What is OAuth 2.0?
- Twitter API 2.0 tutorial
- OpenAI Tutorial
2024-02-04T13:44:07.337Z
nova-2
2-general-nova
2024-01-09.29447
30089e05-99d1-4376-b32e-c263170674af
b040087b-d722-4882-9a38-c9a2bed3cb2a
ccd6505eba27ab75cbd7aaf2e26c7f8951d755c40b169ace0d27c6e62aae4cf8
67875a7f-c9c4-48a0-aa55-5bdb8a91c34a
deprecated
A few weeks ago, I taught you how to exploit the Twitter algorithm to get a 6 figure job in tech, but there's just one major flaw with that strategy.
Nobody learning how to code has time to tweet multiple times per day.
Luckily, you don't have to because today, I'll teach you how to build a Twitter bot from scratch that uses artificial intelligence to create unique high value tweets all day long.
My Twitter bot, Bob, is coming up with awesome tweets at The top of the hour every hour and I only pay him, like, 10¢ a day.
He tells funny jokes, gives web dev hot takes, promulgates conspiracy theories, Says nice things about his mom and humble brags about his 100 days of code journey.
He is my protege, my magnum opus.
When I die, he will be the one to carry the fire.
If you're new here, like and subscribe.
Because in today's tutorial, you'll learn one of the most important techniques in web development today and that's how to work with the OAuth 2.0 authentication protocol.
As a developer, it opens the door to building apps that act on behalf of a user from another service.
Like, you might use it to upload files Also, Google Drive process payments with Stripe Connect or in our case post tweets to somebody's Twitter account.
So before we jump into the code, let's do a quick crash course on an API authentication.
For Twitter, you'll first want to create a developer account, then on the developer portal, you can create a new application.
When you create an app, it will give you an API key and API secret.
The API key is like a username while the secret is like a password.
These two values together create what's known as a bearer token to authenticate your developer app.
This token is included on an Authorization header when making a request to the API that tells Twitter who you are and what you have access to.
That's nice and easy, but it just provides read only access to public information on Twitter.
That's That's because you don't automatically have access to somebody else's account.
To get permission from a user, you'll need to go through the OAuth 2 point o authorization code flow.
There are 3 steps in the process.
On the server, you'll first need to create an authorization link that will redirect the user to Twitter, where they can grant access to your application from the browser.
Once permission is granted, Twitter will then redirect them to a different endpoint on your server, which is called the callback URL.
That URL will also include a special code that you'll need to verify on the server.
Once verified, Twitter will give you 2 values, an access token and a refresh token.
You'll want to save these values in a database.
The third step is to use the access token.
That's the value that gives you access to make API calls on behalf of the user.
Now, the one problem though It's that the access token will expire after a certain amount of time.
It depends on what your app is trying to do, but if you want to post tweets in the background while your user is sleep, then you'll also need to refresh the token.
For that, you make an additional call to the Twitter API with your refresh token, and it will respond with a new access token.
This is security measure that prevents a hacker from going absolutely crazy if your database is ever compromised.
Now that you know how OAuth 2.0 works, let's actually implement it with some JavaScript code.
I'm going to use Firebase Cloud Functions for this project, but feel free to use any Node.
Js server that you want.
And also keep in mind that if you're building a real Twitter bot, you'll also need a database to store the access tokens.
In this demo, I'll be using Firestore to handle that.
All you need is a Firebase account, which is completely free.
And if you wanna get really good at Firebase, become a pro member at Fireship IO.
I'll leave a discount in the description.
To create a new Node.
Js project, I'll first run firebase init functions from the command line, lcd into the functions directory, and then install 2 dependencies.
We have the Twitter API Node SDK and also the OpenAI SDK for GPT 3 that we'll look at later.
Now, in the index JS file, We can create multiple serverless functions.
Let's go ahead and do that for all three steps in the process.
First, we have auth, which will generate the authentication link, then we have the callback URL.
And, finally, we have an endpoint to do something interesting with the API.
Now, we're working in a trusted server environment, so I'm going to import Firebase admin, and call admin initialize app.
This will give us access to the Firestore database, giving us a place to store the access tokens.
In fact, I'm going to make a reference to a document in the database right now, which you can think of as a JSON document that we read and write to.
Now from there, we'll want to initialize the Twitter API.
There are a few different ways to do it, but in our case using OAuth 2.0, we'll want to instantiate it using that client ID and client secret.
To get those values, is we'll need to go back to our Twitter app and select OAuth 2.0 from the authentication settings.
Now, there is one very important value that we'll need to add in the configuration settings and that's the callback URL.
Currently, we don't have a production URL to use here, but we can create one by going to the terminal in our project and running firebase serve.
Go ahead and copy the URL that points to callback, then paste it into the Twitter app.
Now, Twitter doesn't allow you to use localhost, but you can use your localhost IP, which is the same for everybody, 127.zero.zero.one.
Now, go ahead and save your settings and it will give you a client ID and client secret.
Those Those are the values you wanna use when instantiating the Twitter client.
I'm also setting up a variable here for the callback URL because we'll need to reference that later as well.
Now, we're ready to implement step 1.
We can generate the authentication URL by running generate oauth2offlink.
It takes the callback URL as an argument and also an array of scopes.
These are the various permission levels that you're asking the user to grant your app.
In this example, we want to be tweeting all day every day, which means we need read and write access as well as offline access, which allows us to do things in the background while the user is sleeping.
Now, calling this method will return 3 values.
The first one is the URL.
We can take that URL and simply redirect the user to it, but in order to verify the user in the future, we'll also need to save a code verifier and state to the database.
I can do that with Firestore by simply setting those values on the document in the database.
That's all there is to it.
Let's go ahead and try it out in the browser.
When we go to the auth URL, it should automatically redirect you to Twitter.
At this point, you want to be signed into the Twitter account that you want to use as the bot, But what you'll also notice is that if you go to the Firestore database, you should see a value for code verifier and state saved there as well.
When When you click authorize, notice how Twitter will redirect you to the callback URL and that URL also contains parameters for code and state.
Now let's implement the code to handle this callback.
1st, we'll grab the state and code values from the request query in the URL.
We need to compare these values is with what we stored in the database in step 1.
Let's grab the code verifier and the state from the database, then run a validation to see if there's a mismatch between the state and the stored state.
If they don't match, then we'll respond with an error.
Otherwise, we can use the Twitter client to log in with OAuth 2.
Notice how it takes the code from the URL and the code verifier from the database to authenticate the Get the user with our Twitter app.
That will return an authenticated client that we can use to start making API requests on behalf of that user.
But more importantly for now, it returns an access token and refresh token, which we'll also want to save in the database.
Now at this point, we could use the logged in client to make requests to the Twitter API.
We'll get to that in just a second.
For now, let's just send a 200 response back and then go to the browser and test it out.
After the user is done granting permission via Twitter, It should redirect to the callback URL, at which point the access and refresh tokens will be saved in the database.
And now finally, we've reached the fun part of this video, where we outsource our tweets to artificial intelligence via GPT 3.
I would like to invite you all to come along with me on a journey.
And the 3rd endpoint, the first thing we'll do is access the refresh token from the database.
Now, let's assume that a few hours have passed and we want to re access the user's Twitter account.
We can do that by calling the refresh oauth2token method, which will give us a brand new access token and also a client where we can start making API calls.
When we get the new access token, we'll want to also save that to the database.
Dealing with a refresh token is only required if your Twitter bot It does things in the background because otherwise your app could just use the access token that wouldn't be expired yet.
In any case, we can now use the Twitter client to do things like access the user's profile information or to send a tweet on their behalf.
And there's all kinds of other things you could do as well, like send DMs, retweets, or any kind of other pointless activity you can imagine doing Twitter.
For example, let's go ahead and use the client with version 2 of the API to access their profile using the me method.
That will give us some JSON data, which we can send as the response.
Then when we open the function in the browser, the result is an object with that user's profile data.
Now let's take things to the next level with OpenAI.
You'll first need an open AI account to access GPT 3, then you'll need to grab your organization ID and API secret, which are then used to initialize the Node SDK in our JavaScript code.
Now, tweeting is about to become the easiest thing you've ever done in your life.
To create a new tweet, go back into the function code, then call open AI create completion.
It takes an AI model like DaVinci as its first argument, then a prompt as the second argument.
If we prompt it to give us a tweet, it will generate a unique tweet on the fly.
I want to point out That in the full source code, I did some randomizing to give it over 10,000 potential prompts resulting in a greater variation of tweets.
This code is top secret to just me and Bob, but play around with the prompt to see what kind of interesting results you get.
Now all we have to do is take that text and then call the tweet method on the Twitter client.
Congratulations, you just built an AI powered Twitter bot.
Call this function from the browser, then go to your Twitter account, and you should see the new tweet posted there.
Now, all you have to do is create a Cron job to run this in the background a few times a day, and after a few months, you should have all kinds of 6 figure job offers in your ems.
If you found this tutorial useful and want to build more complex apps, I have multiple articles and videos for pro members on FireShip IO explaining different OAuth 2.0 authentication flows.
And as a final note, I wanna talk about who Bob really is.
Like, whose face is that?
It's not a friend of mine or some random model I found on the Internet.
It's a face generated with AI, specifically a generative adversarial network called StyleGAN.
Bob's face doesn't actually exist in reality, at least in the biological sense.
Check out generated dot photos to create your own random person.
Thanks for watching, and I will see you in the next one.

Speaker 0: A few weeks ago, I taught you how to exploit the Twitter algorithm to get a 6 figure job in tech, but there's just one major flaw with that strategy. Nobody learning how to code has time to tweet multiple times per day. Luckily, you don't have to because today, I'll teach you how to build a Twitter bot from scratch that uses artificial intelligence to create unique high value tweets all day long. My Twitter bot, Bob, is coming up with awesome tweets at The top of the hour every hour and I only pay him, like, 10¢ a day. He tells funny jokes, gives web dev hot takes, promulgates conspiracy theories, Says nice things about his mom and humble brags about his 100 days of code journey.

He is my protege, my magnum opus. When I die, he will be the one to carry the fire. If you're new here, like and subscribe. Because in today's tutorial, you'll learn one of the most important techniques in web development today and that's how to work with the OAuth 2.0 authentication protocol. As a developer, it opens the door to building apps that act on behalf of a user from another service.

Like, you might use it to upload files Also, Google Drive process payments with Stripe Connect or in our case post tweets to somebody's Twitter account. So before we jump into the code, let's do a quick crash course on an API authentication. For Twitter, you'll first want to create a developer account, then on the developer portal, you can create a new application. When you create an app, it will give you an API key and API secret. The API key is like a username while the secret is like a password.

These two values together create what's known as a bearer token to authenticate your developer app. This token is included on an Authorization header when making a request to the API that tells Twitter who you are and what you have access to. That's nice and easy, but it just provides read only access to public information on Twitter. That's That's because you don't automatically have access to somebody else's account. To get permission from a user, you'll need to go through the OAuth 2 point o authorization code flow.

There are 3 steps in the process. On the server, you'll first need to create an authorization link that will redirect the user to Twitter, where they can grant access to your application from the browser. Once permission is granted, Twitter will then redirect them to a different endpoint on your server, which is called the callback URL. That URL will also include a special code that you'll need to verify on the server. Once verified, Twitter will give you 2 values, an access token and a refresh token.

You'll want to save these values in a database. The third step is to use the access token. That's the value that gives you access to make API calls on behalf of the user. Now, the one problem though It's that the access token will expire after a certain amount of time. It depends on what your app is trying to do, but if you want to post tweets in the background while your user is sleep, then you'll also need to refresh the token.

For that, you make an additional call to the Twitter API with your refresh token, and it will respond with a new access token. This is security measure that prevents a hacker from going absolutely crazy if your database is ever compromised. Now that you know how OAuth 2.0 works, let's actually implement it with some JavaScript code. I'm going to use Firebase Cloud Functions for this project, but feel free to use any Node. Js server that you want.

And also keep in mind that if you're building a real Twitter bot, you'll also need a database to store the access tokens. In this demo, I'll be using Firestore to handle that. All you need is a Firebase account, which is completely free. And if you wanna get really good at Firebase, become a pro member at Fireship IO. I'll leave a discount in the description.

To create a new Node. Js project, I'll first run firebase init functions from the command line, lcd into the functions directory, and then install 2 dependencies. We have the Twitter API Node SDK and also the OpenAI SDK for GPT 3 that we'll look at later. Now, in the index JS file, We can create multiple serverless functions. Let's go ahead and do that for all three steps in the process.

First, we have auth, which will generate the authentication link, then we have the callback URL. And, finally, we have an endpoint to do something interesting with the API. Now, we're working in a trusted server environment, so I'm going to import Firebase admin, and call admin initialize app. This will give us access to the Firestore database, giving us a place to store the access tokens. In fact, I'm going to make a reference to a document in the database right now, which you can think of as a JSON document that we read and write to.

Now from there, we'll want to initialize the Twitter API. There are a few different ways to do it, but in our case using OAuth 2.0, we'll want to instantiate it using that client ID and client secret. To get those values, is we'll need to go back to our Twitter app and select OAuth 2.0 from the authentication settings. Now, there is one very important value that we'll need to add in the configuration settings and that's the callback URL. Currently, we don't have a production URL to use here, but we can create one by going to the terminal in our project and running firebase serve.

Go ahead and copy the URL that points to callback, then paste it into the Twitter app. Now, Twitter doesn't allow you to use localhost, but you can use your localhost IP, which is the same for everybody, 127.zero.zero.one. Now, go ahead and save your settings and it will give you a client ID and client secret. Those Those are the values you wanna use when instantiating the Twitter client. I'm also setting up a variable here for the callback URL because we'll need to reference that later as well.

Now, we're ready to implement step 1. We can generate the authentication URL by running generate oauth2offlink. It takes the callback URL as an argument and also an array of scopes. These are the various permission levels that you're asking the user to grant your app. In this example, we want to be tweeting all day every day, which means we need read and write access as well as offline access, which allows us to do things in the background while the user is sleeping.

Now, calling this method will return 3 values. The first one is the URL. We can take that URL and simply redirect the user to it, but in order to verify the user in the future, we'll also need to save a code verifier and state to the database. I can do that with Firestore by simply setting those values on the document in the database. That's all there is to it.

Let's go ahead and try it out in the browser. When we go to the auth URL, it should automatically redirect you to Twitter. At this point, you want to be signed into the Twitter account that you want to use as the bot, But what you'll also notice is that if you go to the Firestore database, you should see a value for code verifier and state saved there as well. When When you click authorize, notice how Twitter will redirect you to the callback URL and that URL also contains parameters for code and state. Now let's implement the code to handle this callback.

1st, we'll grab the state and code values from the request query in the URL. We need to compare these values is with what we stored in the database in step 1. Let's grab the code verifier and the state from the database, then run a validation to see if there's a mismatch between the state and the stored state. If they don't match, then we'll respond with an error. Otherwise, we can use the Twitter client to log in with OAuth 2.

Notice how it takes the code from the URL and the code verifier from the database to authenticate the Get the user with our Twitter app. That will return an authenticated client that we can use to start making API requests on behalf of that user. But more importantly for now, it returns an access token and refresh token, which we'll also want to save in the database. Now at this point, we could use the logged in client to make requests to the Twitter API. We'll get to that in just a second.

For now, let's just send a 200 response back and then go to the browser and test it out. After the user is done granting permission via Twitter, It should redirect to the callback URL, at which point the access and refresh tokens will be saved in the database. And now finally, we've reached the fun part of this video, where we outsource our tweets to artificial intelligence via GPT 3. I would like to invite you all to come along with me on a journey. And the 3rd endpoint, the first thing we'll do is access the refresh token from the database.

Now, let's assume that a few hours have passed and we want to re access the user's Twitter account. We can do that by calling the refresh oauth2token method, which will give us a brand new access token and also a client where we can start making API calls. When we get the new access token, we'll want to also save that to the database. Dealing with a refresh token is only required if your Twitter bot It does things in the background because otherwise your app could just use the access token that wouldn't be expired yet. In any case, we can now use the Twitter client to do things like access the user's profile information or to send a tweet on their behalf.

And there's all kinds of other things you could do as well, like send DMs, retweets, or any kind of other pointless activity you can imagine doing Twitter. For example, let's go ahead and use the client with version 2 of the API to access their profile using the me method. That will give us some JSON data, which we can send as the response. Then when we open the function in the browser, the result is an object with that user's profile data. Now let's take things to the next level with OpenAI.

You'll first need an open AI account to access GPT 3, then you'll need to grab your organization ID and API secret, which are then used to initialize the Node SDK in our JavaScript code. Now, tweeting is about to become the easiest thing you've ever done in your life. To create a new tweet, go back into the function code, then call open AI create completion. It takes an AI model like DaVinci as its first argument, then a prompt as the second argument. If we prompt it to give us a tweet, it will generate a unique tweet on the fly.

I want to point out That in the full source code, I did some randomizing to give it over 10,000 potential prompts resulting in a greater variation of tweets. This code is top secret to just me and Bob, but play around with the prompt to see what kind of interesting results you get. Now all we have to do is take that text and then call the tweet method on the Twitter client. Congratulations, you just built an AI powered Twitter bot. Call this function from the browser, then go to your Twitter account, and you should see the new tweet posted there.

Now, all you have to do is create a Cron job to run this in the background a few times a day, and after a few months, you should have all kinds of 6 figure job offers in your ems. If you found this tutorial useful and want to build more complex apps, I have multiple articles and videos for pro members on FireShip IO explaining different OAuth 2.0 authentication flows. And as a final note, I wanna talk about who Bob really is. Like, whose face is that? It's not a friend of mine or some random model I found on the Internet.

It's a face generated with AI, specifically a generative adversarial network called StyleGAN. Bob's face doesn't actually exist in reality, at least in the biological sense. Check out generated dot photos to create your own random person. Thanks for watching, and I will see you in the next one.
A few weeks ago, I taught you how to exploit the Twitter algorithm to get a 6 figure job in tech, but there's just one major flaw with that strategy. Nobody learning how to code has time to tweet multiple times per day. Luckily, you don't have to because today, I'll teach you how to build a Twitter bot from scratch that uses artificial intelligence to create unique high value tweets all day long. My Twitter bot, Bob, is coming up with awesome tweets at The top of the hour every hour and I only pay him, like, 10¢ a day. He tells funny jokes, gives web dev hot takes, promulgates conspiracy theories, Says nice things about his mom and humble brags about his 100 days of code journey.
twitter
social media
He is my protege, my magnum opus. When I die, he will be the one to carry the fire. If you're new here, like and subscribe.
Because in today's tutorial, you'll learn one of the most important techniques in web development today and that's how to work with the OAuth 2.0 authentication protocol. As a developer, it opens the door to building apps that act on behalf of a user from another service. Like, you might use it to upload files Also, Google Drive process payments with Stripe Connect or in our case post tweets to somebody's Twitter account.
software development
So before we jump into the code, let's do a quick crash course on an API authentication. For Twitter, you'll first want to create a developer account, then on the developer portal, you can create a new application. When you create an app, it will give you an API key and API secret.
software development
The API key is like a username while the secret is like a password. These two values together create what's known as a bearer token to authenticate your developer app. This token is included on an Authorization header when making a request to the API that tells Twitter who you are and what you have access to. That's nice and easy, but it just provides read only access to public information on Twitter. That's That's because you don't automatically have access to somebody else's account. To get permission from a user, you'll need to go through the OAuth 2 point o authorization code flow. There are 3 steps in the process.
On the server, you'll first need to create an authorization link that will redirect the user to Twitter, where they can grant access to your application from the browser. Once permission is granted, Twitter will then redirect them to a different endpoint on your server, which is called the callback URL. That URL will also include a special code that you'll need to verify on the server. Once verified, Twitter will give you 2 values, an access token and a refresh token. You'll want to save these values in a database. The third step is to use the access token. That's the value that gives you access to make API calls on behalf of the user. Now, the one problem though It's that the access token will expire after a certain amount of time.
twitter
It depends on what your app is trying to do, but if you want to post tweets in the background while your user is sleep, then you'll also need to refresh the token. For that, you make an additional call to the Twitter API with your refresh token, and it will respond with a new access token. This is security measure that prevents a hacker from going absolutely crazy if your database is ever compromised. Now that you know how OAuth 2.0 works, let's actually implement it with some JavaScript code.
twitter
I'm going to use Firebase Cloud Functions for this project, but feel free to use any Node. Js server that you want. And also keep in mind that if you're building a real Twitter bot, you'll also need a database to store the access tokens. In this demo, I'll be using Firestore to handle that. All you need is a Firebase account, which is completely free. And if you wanna get really good at Firebase, become a pro member at Fireship IO. I'll leave a discount in the description. To create a new Node. Js project, I'll first run firebase init functions from the command line, lcd into the functions directory, and then install 2 dependencies.
We have the Twitter API Node SDK and also the OpenAI SDK for GPT 3 that we'll look at later. Now, in the index JS file, We can create multiple serverless functions. Let's go ahead and do that for all three steps in the process. First, we have auth, which will generate the authentication link, then we have the callback URL.
twitter
And, finally, we have an endpoint to do something interesting with the API. Now, we're working in a trusted server environment, so I'm going to import Firebase admin, and call admin initialize app.
This will give us access to the Firestore database, giving us a place to store the access tokens. In fact, I'm going to make a reference to a document in the database right now, which you can think of as a JSON document that we read and write to. Now from there, we'll want to initialize the Twitter API. There are a few different ways to do it, but in our case using OAuth 2.0, we'll want to instantiate it using that client ID and client secret. To get those values, is we'll need to go back to our Twitter app and select OAuth 2.0 from the authentication settings. Now, there is one very important value that we'll need to add in the configuration settings and that's the callback URL. Currently, we don't have a production URL to use here, but we can create one by going to the terminal in our project and running firebase serve. Go ahead and copy the URL that points to callback, then paste it into the Twitter app. Now, Twitter doesn't allow you to use localhost, but you can use your localhost IP, which is the same for everybody, 127.zero.zero.one. Now, go ahead and save your settings and it will give you a client ID and client secret. Those Those are the values you wanna use when instantiating the Twitter client. I'm also setting up a variable here for the callback URL because we'll need to reference that later as well.
twitter
databases
Now, we're ready to implement step 1. We can generate the authentication URL by running generate oauth2offlink. It takes the callback URL as an argument and also an array of scopes. These are the various permission levels that you're asking the user to grant your app.
In this example, we want to be tweeting all day every day, which means we need read and write access as well as offline access, which allows us to do things in the background while the user is sleeping. Now, calling this method will return 3 values. The first one is the URL.
twitter
We can take that URL and simply redirect the user to it, but in order to verify the user in the future, we'll also need to save a code verifier and state to the database.
I can do that with Firestore by simply setting those values on the document in the database. That's all there is to it. Let's go ahead and try it out in the browser. When we go to the auth URL, it should automatically redirect you to Twitter. At this point, you want to be signed into the Twitter account that you want to use as the bot, But what you'll also notice is that if you go to the Firestore database, you should see a value for code verifier and state saved there as well. When When you click authorize, notice how Twitter will redirect you to the callback URL and that URL also contains parameters for code and state. Now let's implement the code to handle this callback. 1st, we'll grab the state and code values from the request query in the URL. We need to compare these values is with what we stored in the database in step 1. Let's grab the code verifier and the state from the database, then run a validation to see if there's a mismatch between the state and the stored state.
twitter
websites
databases
If they don't match, then we'll respond with an error. Otherwise, we can use the Twitter client to log in with OAuth 2. Notice how it takes the code from the URL and the code verifier from the database to authenticate the Get the user with our Twitter app. That will return an authenticated client that we can use to start making API requests on behalf of that user. But more importantly for now, it returns an access token and refresh token, which we'll also want to save in the database. Now at this point, we could use the logged in client to make requests to the Twitter API. We'll get to that in just a second. For now, let's just send a 200 response back and then go to the browser and test it out. After the user is done granting permission via Twitter, It should redirect to the callback URL, at which point the access and refresh tokens will be saved in the database. And now finally, we've reached the fun part of this video, where we outsource our tweets to artificial intelligence via GPT 3. I would like to invite you all to come along with me on a journey. And the 3rd endpoint, the first thing we'll do is access the refresh token from the database. Now, let's assume that a few hours have passed and we want to re access the user's Twitter account. We can do that by calling the refresh oauth2token method, which will give us a brand new access token and also a client where we can start making API calls.
multimedia
twitter
youtube
visualization
streaming
social media
When we get the new access token, we'll want to also save that to the database. Dealing with a refresh token is only required if your Twitter bot It does things in the background because otherwise your app could just use the access token that wouldn't be expired yet. In any case, we can now use the Twitter client to do things like access the user's profile information or to send a tweet on their behalf. And there's all kinds of other things you could do as well, like send DMs, retweets, or any kind of other pointless activity you can imagine doing Twitter. For example, let's go ahead and use the client with version 2 of the API to access their profile using the me method.
twitter
social media
internet
That will give us some JSON data, which we can send as the response. Then when we open the function in the browser, the result is an object with that user's profile data. Now let's take things to the next level with OpenAI.
You'll first need an open AI account to access GPT 3, then you'll need to grab your organization ID and API secret, which are then used to initialize the Node SDK in our JavaScript code. Now, tweeting is about to become the easiest thing you've ever done in your life. To create a new tweet, go back into the function code, then call open AI create completion. It takes an AI model like DaVinci as its first argument, then a prompt as the second argument.
twitter
software development
If we prompt it to give us a tweet, it will generate a unique tweet on the fly. I want to point out That in the full source code, I did some randomizing to give it over 10,000 potential prompts resulting in a greater variation of tweets.
This code is top secret to just me and Bob, but play around with the prompt to see what kind of interesting results you get. Now all we have to do is take that text and then call the tweet method on the Twitter client. Congratulations, you just built an AI powered Twitter bot. Call this function from the browser, then go to your Twitter account, and you should see the new tweet posted there. Now, all you have to do is create a Cron job to run this in the background a few times a day, and after a few months, you should have all kinds of 6 figure job offers in your ems.
twitter
social media
programming
If you found this tutorial useful and want to build more complex apps, I have multiple articles and videos for pro members on FireShip IO explaining different OAuth 2.0 authentication flows. And as a final note, I wanna talk about who Bob really is. Like, whose face is that? It's not a friend of mine or some random model I found on the Internet. It's a face generated with AI, specifically a generative adversarial network called StyleGAN. Bob's face doesn't actually exist in reality, at least in the biological sense.
Check out generated dot photos to create your own random person. Thanks for watching, and I will see you in the next one.
A few weeks ago, I taught you how to exploit the Twitter algorithm to get a 6 figure job in tech, but there's just one major flaw with that strategy. Nobody learning how to code has time to tweet multiple times per day. Luckily, you don't have to because today, I'll teach you how to build a Twitter bot from scratch that uses artificial intelligence to create unique high value tweets all day long. My Twitter bot, Bob, is coming up with awesome tweets at The top of the hour every hour and I only pay him, like, 10¢ a day. He tells funny jokes, gives web dev hot takes, promulgates conspiracy theories, Says nice things about his mom and humble brags about his 100 days of code journey. He is my protege, my magnum opus. When I die, he will be the one to carry the fire. If you're new here, like and subscribe. Because in today's tutorial, you'll learn one of the most important techniques in web development today and that's how to work with the OAuth 2.0 authentication protocol. As a developer, it opens the door to building apps that act on behalf of a user from another service. Like, you might use it to upload files Also, Google Drive process payments with Stripe Connect or in our case post tweets to somebody's Twitter account. So before we jump into the code, let's do a quick crash course on an API authentication. For Twitter, you'll first want to create a developer account, then on the developer portal, you can create a new application. When you create an app, it will give you an API key and API secret. The API key is like a username while the secret is like a password. These two values together create what's known as a bearer token to authenticate your developer app. This token is included on an Authorization header when making a request to the API that tells Twitter who you are and what you have access to. That's nice and easy, but it just provides read only access to public information on Twitter. That's That's because you don't automatically have access to somebody else's account. To get permission from a user, you'll need to go through the OAuth 2 point o authorization code flow. There are 3 steps in the process. On the server, you'll first need to create an authorization link that will redirect the user to Twitter, where they can grant access to your application from the browser. Once permission is granted, Twitter will then redirect them to a different endpoint on your server, which is called the callback URL. That URL will also include a special code that you'll need to verify on the server. Once verified, Twitter will give you 2 values, an access token and a refresh token. You'll want to save these values in a database. The third step is to use the access token. That's the value that gives you access to make API calls on behalf of the user. Now, the one problem though It's that the access token will expire after a certain amount of time. It depends on what your app is trying to do, but if you want to post tweets in the background while your user is sleep, then you'll also need to refresh the token. For that, you make an additional call to the Twitter API with your refresh token, and it will respond with a new access token. This is security measure that prevents a hacker from going absolutely crazy if your database is ever compromised. Now that you know how OAuth 2.0 works, let's actually implement it with some JavaScript code. I'm going to use Firebase Cloud Functions for this project, but feel free to use any Node. Js server that you want. And also keep in mind that if you're building a real Twitter bot, you'll also need a database to store the access tokens. In this demo, I'll be using Firestore to handle that. All you need is a Firebase account, which is completely free. And if you wanna get really good at Firebase, become a pro member at Fireship IO. I'll leave a discount in the description. To create a new Node. Js project, I'll first run firebase init functions from the command line, lcd into the functions directory, and then install 2 dependencies. We have the Twitter API Node SDK and also the OpenAI SDK for GPT 3 that we'll look at later. Now, in the index JS file, We can create multiple serverless functions. Let's go ahead and do that for all three steps in the process. First, we have auth, which will generate the authentication link, then we have the callback URL. And, finally, we have an endpoint to do something interesting with the API. Now, we're working in a trusted server environment, so I'm going to import Firebase admin, and call admin initialize app. This will give us access to the Firestore database, giving us a place to store the access tokens. In fact, I'm going to make a reference to a document in the database right now, which you can think of as a JSON document that we read and write to. Now from there, we'll want to initialize the Twitter API. There are a few different ways to do it, but in our case using OAuth 2.0, we'll want to instantiate it using that client ID and client secret. To get those values, is we'll need to go back to our Twitter app and select OAuth 2.0 from the authentication settings. Now, there is one very important value that we'll need to add in the configuration settings and that's the callback URL. Currently, we don't have a production URL to use here, but we can create one by going to the terminal in our project and running firebase serve. Go ahead and copy the URL that points to callback, then paste it into the Twitter app. Now, Twitter doesn't allow you to use localhost, but you can use your localhost IP, which is the same for everybody, 127.zero.zero.one. Now, go ahead and save your settings and it will give you a client ID and client secret. Those Those are the values you wanna use when instantiating the Twitter client. I'm also setting up a variable here for the callback URL because we'll need to reference that later as well. Now, we're ready to implement step 1. We can generate the authentication URL by running generate oauth2offlink. It takes the callback URL as an argument and also an array of scopes. These are the various permission levels that you're asking the user to grant your app. In this example, we want to be tweeting all day every day, which means we need read and write access as well as offline access, which allows us to do things in the background while the user is sleeping. Now, calling this method will return 3 values. The first one is the URL. We can take that URL and simply redirect the user to it, but in order to verify the user in the future, we'll also need to save a code verifier and state to the database. I can do that with Firestore by simply setting those values on the document in the database. That's all there is to it. Let's go ahead and try it out in the browser. When we go to the auth URL, it should automatically redirect you to Twitter. At this point, you want to be signed into the Twitter account that you want to use as the bot, But what you'll also notice is that if you go to the Firestore database, you should see a value for code verifier and state saved there as well. When When you click authorize, notice how Twitter will redirect you to the callback URL and that URL also contains parameters for code and state. Now let's implement the code to handle this callback. 1st, we'll grab the state and code values from the request query in the URL. We need to compare these values is with what we stored in the database in step 1. Let's grab the code verifier and the state from the database, then run a validation to see if there's a mismatch between the state and the stored state. If they don't match, then we'll respond with an error. Otherwise, we can use the Twitter client to log in with OAuth 2. Notice how it takes the code from the URL and the code verifier from the database to authenticate the Get the user with our Twitter app. That will return an authenticated client that we can use to start making API requests on behalf of that user. But more importantly for now, it returns an access token and refresh token, which we'll also want to save in the database. Now at this point, we could use the logged in client to make requests to the Twitter API. We'll get to that in just a second. For now, let's just send a 200 response back and then go to the browser and test it out. After the user is done granting permission via Twitter, It should redirect to the callback URL, at which point the access and refresh tokens will be saved in the database. And now finally, we've reached the fun part of this video, where we outsource our tweets to artificial intelligence via GPT 3. I would like to invite you all to come along with me on a journey. And the 3rd endpoint, the first thing we'll do is access the refresh token from the database. Now, let's assume that a few hours have passed and we want to re access the user's Twitter account. We can do that by calling the refresh oauth2token method, which will give us a brand new access token and also a client where we can start making API calls. When we get the new access token, we'll want to also save that to the database. Dealing with a refresh token is only required if your Twitter bot It does things in the background because otherwise your app could just use the access token that wouldn't be expired yet. In any case, we can now use the Twitter client to do things like access the user's profile information or to send a tweet on their behalf. And there's all kinds of other things you could do as well, like send DMs, retweets, or any kind of other pointless activity you can imagine doing Twitter. For example, let's go ahead and use the client with version 2 of the API to access their profile using the me method. That will give us some JSON data, which we can send as the response. Then when we open the function in the browser, the result is an object with that user's profile data. Now let's take things to the next level with OpenAI. You'll first need an open AI account to access GPT 3, then you'll need to grab your organization ID and API secret, which are then used to initialize the Node SDK in our JavaScript code. Now, tweeting is about to become the easiest thing you've ever done in your life. To create a new tweet, go back into the function code, then call open AI create completion. It takes an AI model like DaVinci as its first argument, then a prompt as the second argument. If we prompt it to give us a tweet, it will generate a unique tweet on the fly. I want to point out That in the full source code, I did some randomizing to give it over 10,000 potential prompts resulting in a greater variation of tweets. This code is top secret to just me and Bob, but play around with the prompt to see what kind of interesting results you get. Now all we have to do is take that text and then call the tweet method on the Twitter client. Congratulations, you just built an AI powered Twitter bot. Call this function from the browser, then go to your Twitter account, and you should see the new tweet posted there. Now, all you have to do is create a Cron job to run this in the background a few times a day, and after a few months, you should have all kinds of 6 figure job offers in your ems. If you found this tutorial useful and want to build more complex apps, I have multiple articles and videos for pro members on FireShip IO explaining different OAuth 2.0 authentication flows. And as a final note, I wanna talk about who Bob really is. Like, whose face is that? It's not a friend of mine or some random model I found on the Internet. It's a face generated with AI, specifically a generative adversarial network called StyleGAN. Bob's face doesn't actually exist in reality, at least in the biological sense. Check out generated dot photos to create your own random person. Thanks for watching, and I will see you in the next one.
A
a
few
few
weeks
weeks
ago,
ago
I
i
taught
taught
you
you
how
how
to
to
exploit
exploit
the
the
Twitter
twitter
algorithm
algorithm
to
to
get
get
a
a
6
6
figure
figure
job
job
in
in
tech,
tech
but
but
there's
there's
just
just
one
one
major
major
flaw
flaw
with
with
that
that
strategy.
strategy
Nobody
nobody
learning
learning
how
how
to
to
code
code
has
has
time
time
to
to
tweet
tweet
multiple
multiple
times
times
per
per
day.
day
Luckily,
luckily
you
you
don't
don't
have
have
to
to
because
because
today,
today
I'll
i'll
teach
teach
you
you
how
how
to
to
build
build
a
a
Twitter
twitter
bot
bot
from
from
scratch
scratch
that
that
uses
uses
artificial
artificial
intelligence
intelligence
to
to
create
create
unique
unique
high
high
value
value
tweets
tweets
all
all
day
day
long.
long
My
my
Twitter
twitter
bot,
bot
Bob,
bob
is
is
coming
coming
up
up
with
with
awesome
awesome
tweets
tweets
at
at
The
the
top
top
of
of
the
the
hour
hour
every
every
hour
hour
and
and
I
i
only
only
pay
pay
him,
him
like,
like
10¢
10¢
a
a
day.
day
He
he
tells
tells
funny
funny
jokes,
jokes
gives
gives
web
web
dev
dev
hot
hot
takes,
takes
promulgates
promulgates
conspiracy
conspiracy
theories,
theories
Says
says
nice
nice
things
things
about
about
his
his
mom
mom
and
and
humble
humble
brags
brags
about
about
his
his
100
100
days
days
of
of
code
code
journey.
journey
He
he
is
is
my
my
protege,
protege
my
my
magnum
magnum
opus.
opus
When
when
I
i
die,
die
he
he
will
will
be
be
the
the
one
one
to
to
carry
carry
the
the
fire.
fire
If
if
you're
you're
new
new
here,
here
like
like
and
and
subscribe.
subscribe
Because
because
in
in
today's
today's
tutorial,
tutorial
you'll
you'll
learn
learn
one
one
of
of
the
the
most
most
important
important
techniques
techniques
in
in
web
web
development
development
today
today
and
and
that's
that's
how
how
to
to
work
work
with
with
the
the
OAuth
oauth
2.0
2.0
authentication
authentication
protocol.
protocol
As
as
a
a
developer,
developer
it
it
opens
opens
the
the
door
door
to
to
building
building
apps
apps
that
that
act
act
on
on
behalf
behalf
of
of
a
a
user
user
from
from
another
another
service.
service
Like,
like
you
you
might
might
use
use
it
it
to
to
upload
upload
files
files
Also,
also
Google
google
Drive
drive
process
process
payments
payments
with
with
Stripe
stripe
Connect
connect
or
or
in
in
our
our
case
case
post
post
tweets
tweets
to
to
somebody's
somebody's
Twitter
twitter
account.
account
So
so
before
before
we
we
jump
jump
into
into
the
the
code,
code
let's
let's
do
do
a
a
quick
quick
crash
crash
course
course
on
on
an
an
API
api
authentication.
authentication
For
for
Twitter,
twitter
you'll
you'll
first
first
want
want
to
to
create
create
a
a
developer
developer
account,
account
then
then
on
on
the
the
developer
developer
portal,
portal
you
you
can
can
create
create
a
a
new
new
application.
application
When
when
you
you
create
create
an
an
app,
app
it
it
will
will
give
give
you
you
an
an
API
api
key
key
and
and
API
api
secret.
secret
The
the
API
api
key
key
is
is
like
like
a
a
username
username
while
while
the
the
secret
secret
is
is
like
like
a
a
password.
password
These
these
two
two
values
values
together
together
create
create
what's
what's
known
known
as
as
a
a
bearer
bearer
token
token
to
to
authenticate
authenticate
your
your
developer
developer
app.
app
This
this
token
token
is
is
included
included
on
on
an
an
Authorization
authorization
header
header
when
when
making
making
a
a
request
request
to
to
the
the
API
api
that
that
tells
tells
Twitter
twitter
who
who
you
you
are
are
and
and
what
what
you
you
have
have
access
access
to.
to
That's
that's
nice
nice
and
and
easy,
easy
but
but
it
it
just
just
provides
provides
read
read
only
only
access
access
to
to
public
public
information
information
on
on
Twitter.
twitter
That's
that's
That's
that's
because
because
you
you
don't
don't
automatically
automatically
have
have
access
access
to
to
somebody
somebody
else's
else's
account.
account
To
to
get
get
permission
permission
from
from
a
a
user,
user
you'll
you'll
need
need
to
to
go
go
through
through
the
the
OAuth
oauth
2
2
point
point
o
o
authorization
authorization
code
code
flow.
flow
There
there
are
are
3
3
steps
steps
in
in
the
the
process.
process
On
on
the
the
server,
server
you'll
you'll
first
first
need
need
to
to
create
create
an
an
authorization
authorization
link
link
that
that
will
will
redirect
redirect
the
the
user
user
to
to
Twitter,
twitter
where
where
they
they
can
can
grant
grant
access
access
to
to
your
your
application
application
from
from
the
the
browser.
browser
Once
once
permission
permission
is
is
granted,
granted
Twitter
twitter
will
will
then
then
redirect
redirect
them
them
to
to
a
a
different
different
endpoint
endpoint
on
on
your
your
server,
server
which
which
is
is
called
called
the
the
callback
callback
URL.
url
That
that
URL
url
will
will
also
also
include
include
a
a
special
special
code
code
that
that
you'll
you'll
need
need
to
to
verify
verify
on
on
the
the
server.
server
Once
once
verified,
verified
Twitter
twitter
will
will
give
give
you
you
2
2
values,
values
an
an
access
access
token
token
and
and
a
a
refresh
refresh
token.
token
You'll
you'll
want
want
to
to
save
save
these
these
values
values
in
in
a
a
database.
database
The
the
third
third
step
step
is
is
to
to
use
use
the
the
access
access
token.
token
That's
that's
the
the
value
value
that
that
gives
gives
you
you
access
access
to
to
make
make
API
api
calls
calls
on
on
behalf
behalf
of
of
the
the
user.
user
Now,
now
the
the
one
one
problem
problem
though
though
It's
it's
that
that
the
the
access
access
token
token
will
will
expire
expire
after
after
a
a
certain
certain
amount
amount
of
of
time.
time
It
it
depends
depends
on
on
what
what
your
your
app
app
is
is
trying
trying
to
to
do,
do
but
but
if
if
you
you
want
want
to
to
post
post
tweets
tweets
in
in
the
the
background
background
while
while
your
your
user
user
is
is
sleep,
sleep
then
then
you'll
you'll
also
also
need
need
to
to
refresh
refresh
the
the
token.
token
For
for
that,
that
you
you
make
make
an
an
additional
additional
call
call
to
to
the
the
Twitter
twitter
API
api
with
with
your
your
refresh
refresh
token,
token
and
and
it
it
will
will
respond
respond
with
with
a
a
new
new
access
access
token.
token
This
this
is
is
security
security
measure
measure
that
that
prevents
prevents
a
a
hacker
hacker
from
from
going
going
absolutely
absolutely
crazy
crazy
if
if
your
your
database
database
is
is
ever
ever
compromised.
compromised
Now
now
that
that
you
you
know
know
how
how
OAuth
oauth
2.0
2.0
works,
works
let's
let's
actually
actually
implement
implement
it
it
with
with
some
some
JavaScript
javascript
code.
code
I'm
i'm
going
going
to
to
use
use
Firebase
firebase
Cloud
cloud
Functions
functions
for
for
this
this
project,
project
but
but
feel
feel
free
free
to
to
use
use
any
any
Node.
node
Js
js
server
server
that
that
you
you
want.
want
And
and
also
also
keep
keep
in
in
mind
mind
that
that
if
if
you're
you're
building
building
a
a
real
real
Twitter
twitter
bot,
bot
you'll
you'll
also
also
need
need
a
a
database
database
to
to
store
store
the
the
access
access
tokens.
tokens
In
in
this
this
demo,
demo
I'll
i'll
be
be
using
using
Firestore
firestore
to
to
handle
handle
that.
that
All
all
you
you
need
need
is
is
a
a
Firebase
firebase
account,
account
which
which
is
is
completely
completely
free.
free
And
and
if
if
you
you
wanna
wanna
get
get
really
really
good
good
at
at
Firebase,
firebase
become
become
a
a
pro
pro
member
member
at
at
Fireship
fireship
IO.
io
I'll
i'll
leave
leave
a
a
discount
discount
in
in
the
the
description.
description
To
to
create
create
a
a
new
new
Node.
node
Js
js
project,
project
I'll
i'll
first
first
run
run
firebase
firebase
init
init
functions
functions
from
from
the
the
command
command
line,
line
lcd
lcd
into
into
the
the
functions
functions
directory,
directory
and
and
then
then
install
install
2
2
dependencies.
dependencies
We
we
have
have
the
the
Twitter
twitter
API
api
Node
node
SDK
sdk
and
and
also
also
the
the
OpenAI
openai
SDK
sdk
for
for
GPT
gpt
3
3
that
that
we'll
we'll
look
look
at
at
later.
later
Now,
now
in
in
the
the
index
index
JS
js
file,
file
We
we
can
can
create
create
multiple
multiple
serverless
serverless
functions.
functions
Let's
let's
go
go
ahead
ahead
and
and
do
do
that
that
for
for
all
all
three
three
steps
steps
in
in
the
the
process.
process
First,
first
we
we
have
have
auth,
auth
which
which
will
will
generate
generate
the
the
authentication
authentication
link,
link
then
then
we
we
have
have
the
the
callback
callback
URL.
url
And,
and
finally,
finally
we
we
have
have
an
an
endpoint
endpoint
to
to
do
do
something
something
interesting
interesting
with
with
the
the
API.
api
Now,
now
we're
we're
working
working
in
in
a
a
trusted
trusted
server
server
environment,
environment
so
so
I'm
i'm
going
going
to
to
import
import
Firebase
firebase
admin,
admin
and
and
call
call
admin
admin
initialize
initialize
app.
app
This
this
will
will
give
give
us
us
access
access
to
to
the
the
Firestore
firestore
database,
database
giving
giving
us
us
a
a
place
place
to
to
store
store
the
the
access
access
tokens.
tokens
In
in
fact,
fact
I'm
i'm
going
going
to
to
make
make
a
a
reference
reference
to
to
a
a
document
document
in
in
the
the
database
database
right
right
now,
now
which
which
you
you
can
can
think
think
of
of
as
as
a
a
JSON
json
document
document
that
that
we
we
read
read
and
and
write
write
to.
to
Now
now
from
from
there,
there
we'll
we'll
want
want
to
to
initialize
initialize
the
the
Twitter
twitter
API.
api
There
there
are
are
a
a
few
few
different
different
ways
ways
to
to
do
do
it,
it
but
but
in
in
our
our
case
case
using
using
OAuth
oauth
2.0,
2.0
we'll
we'll
want
want
to
to
instantiate
instantiate
it
it
using
using
that
that
client
client
ID
id
and
and
client
client
secret.
secret
To
to
get
get
those
those
values,
values
is
is
we'll
we'll
need
need
to
to
go
go
back
back
to
to
our
our
Twitter
twitter
app
app
and
and
select
select
OAuth
oauth
2.0
2.0
from
from
the
the
authentication
authentication
settings.
settings
Now,
now
there
there
is
is
one
one
very
very
important
important
value
value
that
that
we'll
we'll
need
need
to
to
add
add
in
in
the
the
configuration
configuration
settings
settings
and
and
that's
that's
the
the
callback
callback
URL.
url
Currently,
currently
we
we
don't
don't
have
have
a
a
production
production
URL
url
to
to
use
use
here,
here
but
but
we
we
can
can
create
create
one
one
by
by
going
going
to
to
the
the
terminal
terminal
in
in
our
our
project
project
and
and
running
running
firebase
firebase
serve.
serve
Go
go
ahead
ahead
and
and
copy
copy
the
the
URL
url
that
that
points
points
to
to
callback,
callback
then
then
paste
paste
it
it
into
into
the
the
Twitter
twitter
app.
app
Now,
now
Twitter
twitter
doesn't
doesn't
allow
allow
you
you
to
to
use
use
localhost,
localhost
but
but
you
you
can
can
use
use
your
your
localhost
localhost
IP,
ip
which
which
is
is
the
the
same
same
for
for
everybody,
everybody
127.zero.zero.one.
127.zero.zero.one
Now,
now
go
go
ahead
ahead
and
and
save
save
your
your
settings
settings
and
and
it
it
will
will
give
give
you
you
a
a
client
client
ID
id
and
and
client
client
secret.
secret
Those
those
Those
those
are
are
the
the
values
values
you
you
wanna
wanna
use
use
when
when
instantiating
instantiating
the
the
Twitter
twitter
client.
client
I'm
i'm
also
also
setting
setting
up
up
a
a
variable
variable
here
here
for
for
the
the
callback
callback
URL
url
because
because
we'll
we'll
need
need
to
to
reference
reference
that
that
later
later
as
as
well.
well
Now,
now
we're
we're
ready
ready
to
to
implement
implement
step
step
1.
1
We
we
can
can
generate
generate
the
the
authentication
authentication
URL
url
by
by
running
running
generate
generate
oauth2offlink.
oauth2offlink
It
it
takes
takes
the
the
callback
callback
URL
url
as
as
an
an
argument
argument
and
and
also
also
an
an
array
array
of
of
scopes.
scopes
These
these
are
are
the
the
various
various
permission
permission
levels
levels
that
that
you're
you're
asking
asking
the
the
user
user
to
to
grant
grant
your
your
app.
app
In
in
this
this
example,
example
we
we
want
want
to
to
be
be
tweeting
tweeting
all
all
day
day
every
every
day,
day
which
which
means
means
we
we
need
need
read
read
and
and
write
write
access
access
as
as
well
well
as
as
offline
offline
access,
access
which
which
allows
allows
us
us
to
to
do
do
things
things
in
in
the
the
background
background
while
while
the
the
user
user
is
is
sleeping.
sleeping
Now,
now
calling
calling
this
this
method
method
will
will
return
return
3
3
values.
values
The
the
first
first
one
one
is
is
the
the
URL.
url
We
we
can
can
take
take
that
that
URL
url
and
and
simply
simply
redirect
redirect
the
the
user
user
to
to
it,
it
but
but
in
in
order
order
to
to
verify
verify
the
the
user
user
in
in
the
the
future,
future
we'll
we'll
also
also
need
need
to
to
save
save
a
a
code
code
verifier
verifier
and
and
state
state
to
to
the
the
database.
database
I
i
can
can
do
do
that
that
with
with
Firestore
firestore
by
by
simply
simply
setting
setting
those
those
values
values
on
on
the
the
document
document
in
in
the
the
database.
database
That's
that's
all
all
there
there
is
is
to
to
it.
it
Let's
let's
go
go
ahead
ahead
and
and
try
try
it
it
out
out
in
in
the
the
browser.
browser
When
when
we
we
go
go
to
to
the
the
auth
auth
URL,
url
it
it
should
should
automatically
automatically
redirect
redirect
you
you
to
to
Twitter.
twitter
At
at
this
this
point,
point
you
you
want
want
to
to
be
be
signed
signed
into
into
the
the
Twitter
twitter
account
account
that
that
you
you
want
want
to
to
use
use
as
as
the
the
bot,
bot
But
but
what
what
you'll
you'll
also
also
notice
notice
is
is
that
that
if
if
you
you
go
go
to
to
the
the
Firestore
firestore
database,
database
you
you
should
should
see
see
a
a
value
value
for
for
code
code
verifier
verifier
and
and
state
state
saved
saved
there
there
as
as
well.
well
When
when
When
when
you
you
click
click
authorize,
authorize
notice
notice
how
how
Twitter
twitter
will
will
redirect
redirect
you
you
to
to
the
the
callback
callback
URL
url
and
and
that
that
URL
url
also
also
contains
contains
parameters
parameters
for
for
code
code
and
and
state.
state
Now
now
let's
let's
implement
implement
the
the
code
code
to
to
handle
handle
this
this
callback.
callback
1st,
1st
we'll
we'll
grab
grab
the
the
state
state
and
and
code
code
values
values
from
from
the
the
request
request
query
query
in
in
the
the
URL.
url
We
we
need
need
to
to
compare
compare
these
these
values
values
is
is
with
with
what
what
we
we
stored
stored
in
in
the
the
database
database
in
in
step
step
1.
1
Let's
let's
grab
grab
the
the
code
code
verifier
verifier
and
and
the
the
state
state
from
from
the
the
database,
database
then
then
run
run
a
a
validation
validation
to
to
see
see
if
if
there's
there's
a
a
mismatch
mismatch
between
between
the
the
state
state
and
and
the
the
stored
stored
state.
state
If
if
they
they
don't
don't
match,
match
then
then
we'll
we'll
respond
respond
with
with
an
an
error.
error
Otherwise,
otherwise
we
we
can
can
use
use
the
the
Twitter
twitter
client
client
to
to
log
log
in
in
with
with
OAuth
oauth
2.
2
Notice
notice
how
how
it
it
takes
takes
the
the
code
code
from
from
the
the
URL
url
and
and
the
the
code
code
verifier
verifier
from
from
the
the
database
database
to
to
authenticate
authenticate
the
the
Get
get
the
the
user
user
with
with
our
our
Twitter
twitter
app.
app
That
that
will
will
return
return
an
an
authenticated
authenticated
client
client
that
that
we
we
can
can
use
use
to
to
start
start
making
making
API
api
requests
requests
on
on
behalf
behalf
of
of
that
that
user.
user
But
but
more
more
importantly
importantly
for
for
now,
now
it
it
returns
returns
an
an
access
access
token
token
and
and
refresh
refresh
token,
token
which
which
we'll
we'll
also
also
want
want
to
to
save
save
in
in
the
the
database.
database
Now
now
at
at
this
this
point,
point
we
we
could
could
use
use
the
the
logged
logged
in
in
client
client
to
to
make
make
requests
requests
to
to
the
the
Twitter
twitter
API.
api
We'll
we'll
get
get
to
to
that
that
in
in
just
just
a
a
second.
second
For
for
now,
now
let's
let's
just
just
send
send
a
a
200
200
response
response
back
back
and
and
then
then
go
go
to
to
the
the
browser
browser
and
and
test
test
it
it
out.
out
After
after
the
the
user
user
is
is
done
done
granting
granting
permission
permission
via
via
Twitter,
twitter
It
it
should
should
redirect
redirect
to
to
the
the
callback
callback
URL,
url
at
at
which
which
point
point
the
the
access
access
and
and
refresh
refresh
tokens
tokens
will
will
be
be
saved
saved
in
in
the
the
database.
database
And
and
now
now
finally,
finally
we've
we've
reached
reached
the
the
fun
fun
part
part
of
of
this
this
video,
video
where
where
we
we
outsource
outsource
our
our
tweets
tweets
to
to
artificial
artificial
intelligence
intelligence
via
via
GPT
gpt
3.
3
I
i
would
would
like
like
to
to
invite
invite
you
you
all
all
to
to
come
come
along
along
with
with
me
me
on
on
a
a
journey.
journey
And
and
the
the
3rd
3rd
endpoint,
endpoint
the
the
first
first
thing
thing
we'll
we'll
do
do
is
is
access
access
the
the
refresh
refresh
token
token
from
from
the
the
database.
database
Now,
now
let's
let's
assume
assume
that
that
a
a
few
few
hours
hours
have
have
passed
passed
and
and
we
we
want
want
to
to
re
re
access
access
the
the
user's
user's
Twitter
twitter
account.
account
We
we
can
can
do
do
that
that
by
by
calling
calling
the
the
refresh
refresh
oauth2token
oauth2token
method,
method
which
which
will
will
give
give
us
us
a
a
brand
brand
new
new
access
access
token
token
and
and
also
also
a
a
client
client
where
where
we
we
can
can
start
start
making
making
API
api
calls.
calls
When
when
we
we
get
get
the
the
new
new
access
access
token,
token
we'll
we'll
want
want
to
to
also
also
save
save
that
that
to
to
the
the
database.
database
Dealing
dealing
with
with
a
a
refresh
refresh
token
token
is
is
only
only
required
required
if
if
your
your
Twitter
twitter
bot
bot
It
it
does
does
things
things
in
in
the
the
background
background
because
because
otherwise
otherwise
your
your
app
app
could
could
just
just
use
use
the
the
access
access
token
token
that
that
wouldn't
wouldn't
be
be
expired
expired
yet.
yet
In
in
any
any
case,
case
we
we
can
can
now
now
use
use
the
the
Twitter
twitter
client
client
to
to
do
do
things
things
like
like
access
access
the
the
user's
user's
profile
profile
information
information
or
or
to
to
send
send
a
a
tweet
tweet
on
on
their
their
behalf.
behalf
And
and
there's
there's
all
all
kinds
kinds
of
of
other
other
things
things
you
you
could
could
do
do
as
as
well,
well
like
like
send
send
DMs,
dms
retweets,
retweets
or
or
any
any
kind
kind
of
of
other
other
pointless
pointless
activity
activity
you
you
can
can
imagine
imagine
doing
doing
Twitter.
twitter
For
for
example,
example
let's
let's
go
go
ahead
ahead
and
and
use
use
the
the
client
client
with
with
version
version
2
2
of
of
the
the
API
api
to
to
access
access
their
their
profile
profile
using
using
the
the
me
me
method.
method
That
that
will
will
give
give
us
us
some
some
JSON
json
data,
data
which
which
we
we
can
can
send
send
as
as
the
the
response.
response
Then
then
when
when
we
we
open
open
the
the
function
function
in
in
the
the
browser,
browser
the
the
result
result
is
is
an
an
object
object
with
with
that
that
user's
user's
profile
profile
data.
data
Now
now
let's
let's
take
take
things
things
to
to
the
the
next
next
level
level
with
with
OpenAI.
openai
You'll
you'll
first
first
need
need
an
an
open
open
AI
ai
account
account
to
to
access
access
GPT
gpt
3,
3
then
then
you'll
you'll
need
need
to
to
grab
grab
your
your
organization
organization
ID
id
and
and
API
api
secret,
secret
which
which
are
are
then
then
used
used
to
to
initialize
initialize
the
the
Node
node
SDK
sdk
in
in
our
our
JavaScript
javascript
code.
code
Now,
now
tweeting
tweeting
is
is
about
about
to
to
become
become
the
the
easiest
easiest
thing
thing
you've
you've
ever
ever
done
done
in
in
your
your
life.
life
To
to
create
create
a
a
new
new
tweet,
tweet
go
go
back
back
into
into
the
the
function
function
code,
code
then
then
call
call
open
open
AI
ai
create
create
completion.
completion
It
it
takes
takes
an
an
AI
ai
model
model
like
like
DaVinci
davinci
as
as
its
its
first
first
argument,
argument
then
then
a
a
prompt
prompt
as
as
the
the
second
second
argument.
argument
If
if
we
we
prompt
prompt
it
it
to
to
give
give
us
us
a
a
tweet,
tweet
it
it
will
will
generate
generate
a
a
unique
unique
tweet
tweet
on
on
the
the
fly.
fly
I
i
want
want
to
to
point
point
out
out
That
that
in
in
the
the
full
full
source
source
code,
code
I
i
did
did
some
some
randomizing
randomizing
to
to
give
give
it
it
over
over
10,000
10,000
potential
potential
prompts
prompts
resulting
resulting
in
in
a
a
greater
greater
variation
variation
of
of
tweets.
tweets
This
this
code
code
is
is
top
top
secret
secret
to
to
just
just
me
me
and
and
Bob,
bob
but
but
play
play
around
around
with
with
the
the
prompt
prompt
to
to
see
see
what
what
kind
kind
of
of
interesting
interesting
results
results
you
you
get.
get
Now
now
all
all
we
we
have
have
to
to
do
do
is
is
take
take
that
that
text
text
and
and
then
then
call
call
the
the
tweet
tweet
method
method
on
on
the
the
Twitter
twitter
client.
client
Congratulations,
congratulations
you
you
just
just
built
built
an
an
AI
ai
powered
powered
Twitter
twitter
bot.
bot
Call
call
this
this
function
function
from
from
the
the
browser,
browser
then
then
go
go
to
to
your
your
Twitter
twitter
account,
account
and
and
you
you
should
should
see
see
the
the
new
new
tweet
tweet
posted
posted
there.
there
Now,
now
all
all
you
you
have
have
to
to
do
do
is
is
create
create
a
a
Cron
cron
job
job
to
to
run
run
this
this
in
in
the
the
background
background
a
a
few
few
times
times
a
a
day,
day
and
and
after
after
a
a
few
few
months,
months
you
you
should
should
have
have
all
all
kinds
kinds
of
of
6
6
figure
figure
job
job
offers
offers
in
in
your
your
ems.
ems
If
if
you
you
found
found
this
this
tutorial
tutorial
useful
useful
and
and
want
want
to
to
build
build
more
more
complex
complex
apps,
apps
I
i
have
have
multiple
multiple
articles
articles
and
and
videos
videos
for
for
pro
pro
members
members
on
on
FireShip
fireship
IO
io
explaining
explaining
different
different
OAuth
oauth
2.0
2.0
authentication
authentication
flows.
flows
And
and
as
as
a
a
final
final
note,
note
I
i
wanna
wanna
talk
talk
about
about
who
who
Bob
bob
really
really
is.
is
Like,
like
whose
whose
face
face
is
is
that?
that
It's
it's
not
not
a
a
friend
friend
of
of
mine
mine
or
or
some
some
random
random
model
model
I
i
found
found
on
on
the
the
Internet.
internet
It's
it's
a
a
face
face
generated
generated
with
with
AI,
ai
specifically
specifically
a
a
generative
generative
adversarial
adversarial
network
network
called
called
StyleGAN.
stylegan
Bob's
bob's
face
face
doesn't
doesn't
actually
actually
exist
exist
in
in
reality,
reality
at
at
least
least
in
in
the
the
biological
biological
sense.
sense
Check
check
out
out
generated
generated
dot
dot
photos
photos
to
to
create
create
your
your
own
own
random
random
person.
person
Thanks
thanks
for
for
watching,
watching
and
and
I
i
will
will
see
see
you
you
in
in
the
the
next
next
one.
one
success
The speakers discuss the use of Twitter algorithm to create high-performance tweets and provide instructions on creating a Twitter bot from scratch using artificial intelligence. They also discuss security measures for a Twitter bot and the process of creating a new one. They provide instructions on setting up a callback URL and outsource tweets to artificial intelligence, as well as outsourceing them to artificial intelligence and creating a generated photo for a random person. They also mention the use of OpenAI to access GPT 3 and the use of a generated photo for a random person.
How I post banger tweets with artificial intelligence // Twitter Bot Tutorial
A few weeks ago, I taught you how to exploit the Twitter algorithm to get a 6 figure job in tech, but there's just one major flaw with that strategy. Nobody learning how to code has time to tweet multiple times per day. Luckily, you don't have to because today, I'll teach you how to build a Twitter bot from scratch that uses artificial intelligence to create unique high value tweets all day long. My Twitter bot, Bob, is coming up with awesome tweets at The top of the hour every hour and I only pay him, like, 10¢ a day. He tells funny jokes, gives web dev hot takes, promulgates conspiracy theories, Says nice things about his mom and humble brags about his 100 days of code journey. He is my protege, my magnum opus. When I die, he will be the one to carry the fire. If you're new here, like and subscribe. Because in today's tutorial, you'll learn one of the most important techniques in web development today and that's how to work with the OAuth 2.0 authentication protocol. As a developer, it opens the door to building apps that act on behalf of a user from another service. Like, you might use it to upload files Also, Google Drive process payments with Stripe Connect or in our case post tweets to somebody's Twitter account. So before we jump into the code, let's do a quick crash course on an API authentication. For Twitter, you'll first want to create a developer account, then on the developer portal, you can create a new application. When you create an app, it will give you an API key and API secret. The API key is like a username while the secret is like a password. These two values together create what's known as a bearer token to authenticate your developer app. This token is included on an Authorization header when making a request to the API that tells Twitter who you are and what you have access to. That's nice and easy, but it just provides read only access to public information on Twitter. That's That's because you don't automatically have access to somebody else's account. To get permission from a user, you'll need to go through the OAuth 2 point o authorization code flow. There are 3 steps in the process. On the server, you'll first need to create an authorization link that will redirect the user to Twitter, where they can grant access to your application from the browser. Once permission is granted, Twitter will then redirect them to a different endpoint on your server, which is called the callback URL. That URL will also include a special code that you'll need to verify on the server. Once verified, Twitter will give you 2 values, an access token and a refresh token. You'll want to save these values in a database. The third step is to use the access token. That's the value that gives you access to make API calls on behalf of the user. Now, the one problem though It's that the access token will expire after a certain amount of time. It depends on what your app is trying to do, but if you want to post tweets in the background while your user is sleep, then you'll also need to refresh the token. For that, you make an additional call to the Twitter API with your refresh token, and it will respond with a new access token. This is security measure that prevents a hacker from going absolutely crazy if your database is ever compromised. Now that you know how OAuth 2.0 works, let's actually implement it with some JavaScript code. I'm going to use Firebase Cloud Functions for this project, but feel free to use any Node. Js server that you want. And also keep in mind that if you're building a real Twitter bot, you'll also need a database to store the access tokens. In this demo, I'll be using Firestore to handle that. All you need is a Firebase account, which is completely free. And if you wanna get really good at Firebase, become a pro member at Fireship IO. I'll leave a discount in the description. To create a new Node. Js project, I'll first run firebase init functions from the command line, lcd into the functions directory, and then install 2 dependencies. We have the Twitter API Node SDK and also the OpenAI SDK for GPT 3 that we'll look at later. Now, in the index JS file, We can create multiple serverless functions. Let's go ahead and do that for all three steps in the process. First, we have auth, which will generate the authentication link, then we have the callback URL. And, finally, we have an endpoint to do something interesting with the API. Now, we're working in a trusted server environment, so I'm going to import Firebase admin, and call admin initialize app. This will give us access to the Firestore database, giving us a place to store the access tokens. In fact, I'm going to make a reference to a document in the database right now, which you can think of as a JSON document that we read and write to. Now from there, we'll want to initialize the Twitter API. There are a few different ways to do it, but in our case using OAuth 2.0, we'll want to instantiate it using that client ID and client secret. To get those values, is we'll need to go back to our Twitter app and select OAuth 2.0 from the authentication settings. Now, there is one very important value that we'll need to add in the configuration settings and that's the callback URL. Currently, we don't have a production URL to use here, but we can create one by going to the terminal in our project and running firebase serve. Go ahead and copy the URL that points to callback, then paste it into the Twitter app. Now, Twitter doesn't allow you to use localhost, but you can use your localhost IP, which is the same for everybody, 127.zero.zero.one. Now, go ahead and save your settings and it will give you a client ID and client secret. Those Those are the values you wanna use when instantiating the Twitter client. I'm also setting up a variable here for the callback URL because we'll need to reference that later as well. Now, we're ready to implement step 1. We can generate the authentication URL by running generate oauth2offlink. It takes the callback URL as an argument and also an array of scopes. These are the various permission levels that you're asking the user to grant your app. In this example, we want to be tweeting all day every day, which means we need read and write access as well as offline access, which allows us to do things in the background while the user is sleeping. Now, calling this method will return 3 values. The first one is the URL. We can take that URL and simply redirect the user to it, but in order to verify the user in the future, we'll also need to save a code verifier and state to the database. I can do that with Firestore by simply setting those values on the document in the database. That's all there is to it. Let's go ahead and try it out in the browser. When we go to the auth URL, it should automatically redirect you to Twitter. At this point, you want to be signed into the Twitter account that you want to use as the bot, But what you'll also notice is that if you go to the Firestore database, you should see a value for code verifier and state saved there as well. When When you click authorize, notice how Twitter will redirect you to the callback URL and that URL also contains parameters for code and state. Now let's implement the code to handle this callback. 1st, we'll grab the state and code values from the request query in the URL. We need to compare these values is with what we stored in the database in step 1. Let's grab the code verifier and the state from the database, then run a validation to see if there's a mismatch between the state and the stored state. If they don't match, then we'll respond with an error. Otherwise, we can use the Twitter client to log in with OAuth 2. Notice how it takes the code from the URL and the code verifier from the database to authenticate the Get the user with our Twitter app. That will return an authenticated client that we can use to start making API requests on behalf of that user. But more importantly for now, it returns an access token and refresh token, which we'll also want to save in the database. Now at this point, we could use the logged in client to make requests to the Twitter API. We'll get to that in just a second. For now, let's just send a 200 response back and then go to the browser and test it out. After the user is done granting permission via Twitter, It should redirect to the callback URL, at which point the access and refresh tokens will be saved in the database. And now finally, we've reached the fun part of this video, where we outsource our tweets to artificial intelligence via GPT 3. I would like to invite you all to come along with me on a journey. And the 3rd endpoint, the first thing we'll do is access the refresh token from the database. Now, let's assume that a few hours have passed and we want to re access the user's Twitter account. We can do that by calling the refresh oauth2token method, which will give us a brand new access token and also a client where we can start making API calls. When we get the new access token, we'll want to also save that to the database. Dealing with a refresh token is only required if your Twitter bot It does things in the background because otherwise your app could just use the access token that wouldn't be expired yet. In any case, we can now use the Twitter client to do things like access the user's profile information or to send a tweet on their behalf. And there's all kinds of other things you could do as well, like send DMs, retweets, or any kind of other pointless activity you can imagine doing Twitter. For example, let's go ahead and use the client with version 2 of the API to access their profile using the me method. That will give us some JSON data, which we can send as the response. Then when we open the function in the browser, the result is an object with that user's profile data. Now let's take things to the next level with OpenAI. You'll first need an open AI account to access GPT 3, then you'll need to grab your organization ID and API secret, which are then used to initialize the Node SDK in our JavaScript code. Now, tweeting is about to become the easiest thing you've ever done in your life. To create a new tweet, go back into the function code, then call open AI create completion. It takes an AI model like DaVinci as its first argument, then a prompt as the second argument. If we prompt it to give us a tweet, it will generate a unique tweet on the fly. I want to point out That in the full source code, I did some randomizing to give it over 10,000 potential prompts resulting in a greater variation of tweets. This code is top secret to just me and Bob, but play around with the prompt to see what kind of interesting results you get. Now all we have to do is take that text and then call the tweet method on the Twitter client. Congratulations, you just built an AI powered Twitter bot. Call this function from the browser, then go to your Twitter account, and you should see the new tweet posted there. Now, all you have to do is create a Cron job to run this in the background a few times a day, and after a few months, you should have all kinds of 6 figure job offers in your ems. If you found this tutorial useful and want to build more complex apps, I have multiple articles and videos for pro members on FireShip IO explaining different OAuth 2.0 authentication flows. And as a final note, I wanna talk about who Bob really is. Like, whose face is that? It's not a friend of mine or some random model I found on the Internet. It's a face generated with AI, specifically a generative adversarial network called StyleGAN. Bob's face doesn't actually exist in reality, at least in the biological sense. Check out generated dot photos to create your own random person. Thanks for watching, and I will see you in the next one.
The speakers discuss the use of Twitter algorithm to create high-performance tweets and provide instructions on creating a Twitter bot from scratch using artificial intelligence. They also discuss security measures for a Twitter bot and the process of creating a new one. They provide instructions on setting up a callback URL and outsource tweets to artificial intelligence, as well as outsourceing them to artificial intelligence and creating a generated photo for a random person. They also mention the use of OpenAI to access GPT 3 and the use of a generated photo for a random person.

        [INST]
        You are youtuber called Fireship you make engaging high-intensity and entertaining coding tutorials and tech news. 
        you covers a wide range of topics relevant to programmers, aiming to help them learn and improve their skills quickly.
        
        Given the title of the video : How I post banger tweets with artificial intelligence // Twitter Bot Tutorial 
        and a small summary : The speakers discuss the use of Twitter algorithm to create high-performance tweets and provide instructions on creating a Twitter bot from scratch using artificial intelligence. They also discuss security measures for a Twitter bot and the process of creating a new one. They provide instructions on setting up a callback URL and outsource tweets to artificial intelligence, as well as outsourceing them to artificial intelligence and creating a generated photo for a random person. They also mention the use of OpenAI to access GPT 3 and the use of a generated photo for a random person.
        [/INST]
        
        Generate the video : A few weeks ago, I taught you how to exploit the Twitter algorithm to get a 6 figure job in tech, but there's just one major flaw with that strategy. Nobody learning how to code has time to tweet multiple times per day. Luckily, you don't have to because today, I'll teach you how to build a Twitter bot from scratch that uses artificial intelligence to create unique high value tweets all day long. My Twitter bot, Bob, is coming up with awesome tweets at The top of the hour every hour and I only pay him, like, 10¢ a day. He tells funny jokes, gives web dev hot takes, promulgates conspiracy theories, Says nice things about his mom and humble brags about his 100 days of code journey. He is my protege, my magnum opus. When I die, he will be the one to carry the fire. If you're new here, like and subscribe. Because in today's tutorial, you'll learn one of the most important techniques in web development today and that's how to work with the OAuth 2.0 authentication protocol. As a developer, it opens the door to building apps that act on behalf of a user from another service. Like, you might use it to upload files Also, Google Drive process payments with Stripe Connect or in our case post tweets to somebody's Twitter account. So before we jump into the code, let's do a quick crash course on an API authentication. For Twitter, you'll first want to create a developer account, then on the developer portal, you can create a new application. When you create an app, it will give you an API key and API secret. The API key is like a username while the secret is like a password. These two values together create what's known as a bearer token to authenticate your developer app. This token is included on an Authorization header when making a request to the API that tells Twitter who you are and what you have access to. That's nice and easy, but it just provides read only access to public information on Twitter. That's That's because you don't automatically have access to somebody else's account. To get permission from a user, you'll need to go through the OAuth 2 point o authorization code flow. There are 3 steps in the process. On the server, you'll first need to create an authorization link that will redirect the user to Twitter, where they can grant access to your application from the browser. Once permission is granted, Twitter will then redirect them to a different endpoint on your server, which is called the callback URL. That URL will also include a special code that you'll need to verify on the server. Once verified, Twitter will give you 2 values, an access token and a refresh token. You'll want to save these values in a database. The third step is to use the access token. That's the value that gives you access to make API calls on behalf of the user. Now, the one problem though It's that the access token will expire after a certain amount of time. It depends on what your app is trying to do, but if you want to post tweets in the background while your user is sleep, then you'll also need to refresh the token. For that, you make an additional call to the Twitter API with your refresh token, and it will respond with a new access token. This is security measure that prevents a hacker from going absolutely crazy if your database is ever compromised. Now that you know how OAuth 2.0 works, let's actually implement it with some JavaScript code. I'm going to use Firebase Cloud Functions for this project, but feel free to use any Node. Js server that you want. And also keep in mind that if you're building a real Twitter bot, you'll also need a database to store the access tokens. In this demo, I'll be using Firestore to handle that. All you need is a Firebase account, which is completely free. And if you wanna get really good at Firebase, become a pro member at Fireship IO. I'll leave a discount in the description. To create a new Node. Js project, I'll first run firebase init functions from the command line, lcd into the functions directory, and then install 2 dependencies. We have the Twitter API Node SDK and also the OpenAI SDK for GPT 3 that we'll look at later. Now, in the index JS file, We can create multiple serverless functions. Let's go ahead and do that for all three steps in the process. First, we have auth, which will generate the authentication link, then we have the callback URL. And, finally, we have an endpoint to do something interesting with the API. Now, we're working in a trusted server environment, so I'm going to import Firebase admin, and call admin initialize app. This will give us access to the Firestore database, giving us a place to store the access tokens. In fact, I'm going to make a reference to a document in the database right now, which you can think of as a JSON document that we read and write to. Now from there, we'll want to initialize the Twitter API. There are a few different ways to do it, but in our case using OAuth 2.0, we'll want to instantiate it using that client ID and client secret. To get those values, is we'll need to go back to our Twitter app and select OAuth 2.0 from the authentication settings. Now, there is one very important value that we'll need to add in the configuration settings and that's the callback URL. Currently, we don't have a production URL to use here, but we can create one by going to the terminal in our project and running firebase serve. Go ahead and copy the URL that points to callback, then paste it into the Twitter app. Now, Twitter doesn't allow you to use localhost, but you can use your localhost IP, which is the same for everybody, 127.zero.zero.one. Now, go ahead and save your settings and it will give you a client ID and client secret. Those Those are the values you wanna use when instantiating the Twitter client. I'm also setting up a variable here for the callback URL because we'll need to reference that later as well. Now, we're ready to implement step 1. We can generate the authentication URL by running generate oauth2offlink. It takes the callback URL as an argument and also an array of scopes. These are the various permission levels that you're asking the user to grant your app. In this example, we want to be tweeting all day every day, which means we need read and write access as well as offline access, which allows us to do things in the background while the user is sleeping. Now, calling this method will return 3 values. The first one is the URL. We can take that URL and simply redirect the user to it, but in order to verify the user in the future, we'll also need to save a code verifier and state to the database. I can do that with Firestore by simply setting those values on the document in the database. That's all there is to it. Let's go ahead and try it out in the browser. When we go to the auth URL, it should automatically redirect you to Twitter. At this point, you want to be signed into the Twitter account that you want to use as the bot, But what you'll also notice is that if you go to the Firestore database, you should see a value for code verifier and state saved there as well. When When you click authorize, notice how Twitter will redirect you to the callback URL and that URL also contains parameters for code and state. Now let's implement the code to handle this callback. 1st, we'll grab the state and code values from the request query in the URL. We need to compare these values is with what we stored in the database in step 1. Let's grab the code verifier and the state from the database, then run a validation to see if there's a mismatch between the state and the stored state. If they don't match, then we'll respond with an error. Otherwise, we can use the Twitter client to log in with OAuth 2. Notice how it takes the code from the URL and the code verifier from the database to authenticate the Get the user with our Twitter app. That will return an authenticated client that we can use to start making API requests on behalf of that user. But more importantly for now, it returns an access token and refresh token, which we'll also want to save in the database. Now at this point, we could use the logged in client to make requests to the Twitter API. We'll get to that in just a second. For now, let's just send a 200 response back and then go to the browser and test it out. After the user is done granting permission via Twitter, It should redirect to the callback URL, at which point the access and refresh tokens will be saved in the database. And now finally, we've reached the fun part of this video, where we outsource our tweets to artificial intelligence via GPT 3. I would like to invite you all to come along with me on a journey. And the 3rd endpoint, the first thing we'll do is access the refresh token from the database. Now, let's assume that a few hours have passed and we want to re access the user's Twitter account. We can do that by calling the refresh oauth2token method, which will give us a brand new access token and also a client where we can start making API calls. When we get the new access token, we'll want to also save that to the database. Dealing with a refresh token is only required if your Twitter bot It does things in the background because otherwise your app could just use the access token that wouldn't be expired yet. In any case, we can now use the Twitter client to do things like access the user's profile information or to send a tweet on their behalf. And there's all kinds of other things you could do as well, like send DMs, retweets, or any kind of other pointless activity you can imagine doing Twitter. For example, let's go ahead and use the client with version 2 of the API to access their profile using the me method. That will give us some JSON data, which we can send as the response. Then when we open the function in the browser, the result is an object with that user's profile data. Now let's take things to the next level with OpenAI. You'll first need an open AI account to access GPT 3, then you'll need to grab your organization ID and API secret, which are then used to initialize the Node SDK in our JavaScript code. Now, tweeting is about to become the easiest thing you've ever done in your life. To create a new tweet, go back into the function code, then call open AI create completion. It takes an AI model like DaVinci as its first argument, then a prompt as the second argument. If we prompt it to give us a tweet, it will generate a unique tweet on the fly. I want to point out That in the full source code, I did some randomizing to give it over 10,000 potential prompts resulting in a greater variation of tweets. This code is top secret to just me and Bob, but play around with the prompt to see what kind of interesting results you get. Now all we have to do is take that text and then call the tweet method on the Twitter client. Congratulations, you just built an AI powered Twitter bot. Call this function from the browser, then go to your Twitter account, and you should see the new tweet posted there. Now, all you have to do is create a Cron job to run this in the background a few times a day, and after a few months, you should have all kinds of 6 figure job offers in your ems. If you found this tutorial useful and want to build more complex apps, I have multiple articles and videos for pro members on FireShip IO explaining different OAuth 2.0 authentication flows. And as a final note, I wanna talk about who Bob really is. Like, whose face is that? It's not a friend of mine or some random model I found on the Internet. It's a face generated with AI, specifically a generative adversarial network called StyleGAN. Bob's face doesn't actually exist in reality, at least in the biological sense. Check out generated dot photos to create your own random person. Thanks for watching, and I will see you in the next one.
        