use only bearer token

main
Simone Masiero 3 years ago
parent f62b707ea1
commit 4be4acccfb

@ -1,4 +1,2 @@
CONSUMER_KEY=
CONSUMER_SECRET=
ACCESS_TOKEN_KEY=
ACCESS_TOKEN_SECRET=

1
.gitignore vendored

@ -2,3 +2,4 @@
node_modules/
.env
yarn-error.log
users.txt

@ -9,7 +9,6 @@ The concept is easy and straightforward but the realization encompasses a lot of
You can read more about the realization of this project on the [HackerTyper Blog](https://blog.hackertyper.net/post/twitter-interaction-circles-guide/)
## Installation
```shell script
git clone git@github.com:duiker101/twitter-interaction-circle.git
@ -31,13 +30,15 @@ The output will go in `users.txt` file in the root of the project
## Setup
To use the project you will need Twitter API keys. You can get one on the [developer portal](https://developer.twitter.com).
Once you have them, rename the `.env.example` file `.env` and put them in there, it should look like this:
As this project uses only public data, you don't need an authorization key, but you can generate a Bearer Token. This will prevent you from fetching any private data but will grant you slightly higher rate limiting.
More information about Twitter's bearer tokens can be found [here](https://developer.twitter.com/en/docs/basics/authentication/oauth-2-0)
Once you have your consumer keys, rename the `.env.example` file `.env` and put them in there, it should look like this:
```dotenv
CONSUMER_KEY=AAAAAAA
CONSUMER_SECRET=BBBBBBB
ACCESS_TOKEN_KEY=CCCCCCC
ACCESS_TOKEN_SECRET=DDDDD
CONSUMER_KEY=AAAA
CONSUMER_SECRET=BBBBBB
```
Make sure you don't have spaces.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 984 KiB

After

Width:  |  Height:  |  Size: 1.0 MiB

@ -1,21 +1,3 @@
const Twitter = require("twitter-lite");
/**
* The environment variable are provided by the dotenv via the .env file
*/
const dotenv = require("dotenv");
dotenv.config();
/**
* Create an instance of the API Client that we will use.
* @type {Twitter}
*/
const client = new Twitter({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token_key: process.env.ACCESS_TOKEN_KEY,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
});
/**
* Fetch a single page of the timeline
@ -32,7 +14,7 @@ async function getTimelinePage(screen_name, page, max_id = null) {
};
console.log("Fetching Timeline page " + page);
const res = await client.get("statuses/user_timeline", params);
const res = await globalThis.TwitterClient.get("statuses/user_timeline", params);
return res;
}
@ -71,7 +53,7 @@ async function getLikedPage(screen_name, page, max_id = null) {
};
console.log("Fetching Liked page " + page);
const res = await client.get("favorites/list", params);
const res = await globalThis.TwitterClient.get("favorites/list", params);
return res;
}
@ -107,7 +89,7 @@ async function getAvatars(ids) {
console.log("Fetching avatars " + ids.length);
const res = await client.get("users/lookup", params);
const res = await globalThis.TwitterClient.get("users/lookup", params);
return Object.fromEntries(
res.map((user) => [
@ -117,21 +99,6 @@ async function getAvatars(ids) {
);
}
/**
* Return informations about the currently logged in user
* @returns {Promise<{screen_name: string, id: string, avatar: string}>}
*/
async function getMe() {
console.log("Fetching me");
const res = await client.get("account/verify_credentials");
return {
id: res.id_str,
screen_name: res.screen_name,
avatar: res.profile_image_url_https.replace("normal", "400x400"),
};
}
/**
* Return information about a single specific user
* @returns {Promise<{screen_name: string, id: string, avatar: string}>}
@ -143,7 +110,7 @@ async function getUser(screen_name) {
};
console.log("Fetching user " + screen_name);
const res = (await client.get("users/lookup", params))[0];
const res = (await globalThis.TwitterClient.get("users/lookup", params))[0];
return {
id: res.id_str,
@ -156,6 +123,5 @@ module.exports = {
getLiked,
getTimeline,
getAvatars,
getMe,
getUser
};

@ -1,16 +1,38 @@
const dotenv = require("dotenv");
const getInteractions = require("./data");
const render = require("./image");
const {getMe, getUser} = require("./api");
const {getUser} = require("./api");
const {renderText} = require("./text");
const Twitter = require("twitter-lite");
/**
* Load the environment variables from the .env file
*/
dotenv.config();
/**
* This is the main function of the app. It need to be a function because we can't have a top level await.
*/
async function main() {
// Create an instance of the API client using the consumer keys for your app
const client = new Twitter({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
});
// Use the previous client to fetch the bearer token
// This method gives you an application-only token specifically for read-only access to public information.
const bearer = await client.getBearerToken();
// Create a new twitter client with this token.
// We assign this client to a global variable so we can access it in the api module
globalThis.TwitterClient = new Twitter({
bearer_token: bearer.access_token,
});
// fetch the information of the logged in user
// instead of getMe you could replace it with another method to get a third user to generate their circles
const user = await getMe();
// const user = await getUser(USERNAME);
const user = await getUser("WHATEVER_USERNAME_YOU_WANT");
// this is how many users we will have for each layer from the inside out
const layers = [8, 15, 26];

Loading…
Cancel
Save