Skip to content

V1.0.4 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions examples/oauth2-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const authClient = new auth.OAuth2User({
client_id: process.env.CLIENT_ID as string,
client_secret: process.env.CLIENT_SECRET as string,
callback: "http://127.0.0.1:3000/callback",
scopes: ["tweet.read", "users.read", "offline.access"],
scopes: ["tweet.read", "users.read"],
});

const client = new Client(authClient);
Expand All @@ -23,9 +23,7 @@ const STATE = "my-state";
app.get("/callback", async function (req, res) {
try {
const { code, state } = req.query;
if (state !== STATE) {
return res.status(500).send("State isn't matching");
}
if (state !== STATE) return res.status(500).send("State isn't matching");
await authClient.requestAccessToken(code as string);
res.redirect("/tweets");
} catch (error) {
Expand All @@ -36,7 +34,7 @@ app.get("/callback", async function (req, res) {
app.get("/login", async function (req, res) {
const authUrl = authClient.generateAuthURL({
state: STATE,
code_challenge: "challenge",
code_challenge_method: "s256",
});
res.redirect(authUrl);
});
Expand All @@ -47,14 +45,16 @@ app.get("/tweets", async function (req, res) {
});

app.get("/revoke", async function (req, res) {
const refresh_token = authClient.refresh_token;
if (refresh_token) {
try {
const response = await authClient.revokeAccessToken();
return res.send(response);
res.send(response);
} catch (error) {
console.log(error);
}
res.send("No access token to revoke");
});



app.listen(3000, () => {
console.log(`Go here to login: http://127.0.0.1:3000/login`);
});
6 changes: 1 addition & 5 deletions examples/oauth2-callback_pkce_plain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ const STATE = "my-state";
app.get("/callback", async function (req, res) {
try {
const { code, state } = req.query;
if (state !== STATE) {
return res.status(500).send("State isn't matching");
}
if (state !== STATE) return res.status(500).send("State isn't matching");
await authClient.requestAccessToken(code as string);
res.redirect("/tweets");
} catch (error) {
Expand Down Expand Up @@ -53,8 +51,6 @@ app.get("/tweets", async function (req, res) {

app.get("/revoke", async function (req, res) {
try {
const refresh_token = authClient.refresh_token;
if (!refresh_token) return res.send("No refresh_token found");
const response = await authClient.revokeAccessToken();
res.send(response);
} catch (error) {
Expand Down
6 changes: 1 addition & 5 deletions examples/oauth2-callback_pkce_s256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ const STATE = "my-state";
app.get("/callback", async function (req, res) {
try {
const { code, state } = req.query;
if (state !== STATE) {
return res.status(500).send("State isn't matching");
}
if (state !== STATE) return res.status(500).send("State isn't matching");
await authClient.requestAccessToken(code as string);
res.redirect("/tweets");
} catch (error) {
Expand All @@ -52,8 +50,6 @@ app.get("/tweets", async function (req, res) {

app.get("/revoke", async function (req, res) {
try {
const refresh_token = authClient.refresh_token;
if (!refresh_token) return res.send("No refresh_token found");
const response = await authClient.revokeAccessToken();
res.send(response);
} catch (error) {
Expand Down
66 changes: 66 additions & 0 deletions examples/oauth2-public-callback_pkce_s256.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2021 Twitter, Inc.
// SPDX-License-Identifier: Apache-2.0

import { Client, auth } from "twitter-api-sdk";
import express from "express";
import dotenv from "dotenv";

dotenv.config();

const app = express();

const authClient = new auth.OAuth2User({
client_id: process.env.CLIENT_ID as string,
callback: "http://127.0.0.1:3000/callback",
scopes: ["tweet.read", "users.read", "offline.access"],
});

const client = new Client(authClient);

const STATE = "my-state";

app.get("/callback", async function (req, res) {
try {
const { code, state } = req.query;
if (state !== STATE) return res.status(500).send("State isn't matching");
await authClient.requestAccessToken(code as string);
res.redirect("/tweets");
} catch (error) {
console.log(error);
}
});

app.get("/login", async function (req, res) {
const authUrl = authClient.generateAuthURL({
state: STATE,
code_challenge_method: "s256",
});
res.redirect(authUrl);
});

app.get("/revoke", async function (req, res) {
try {
const response = await authClient.revokeAccessToken();
res.send(response);
} catch (error) {
console.log(error);
}
});

app.get("/tweets", async function (req, res) {
const tweets = await client.tweets.findTweetById("20");
res.send(tweets.data);
});

app.get("/refresh", async function (req, res) {
try {
await authClient.refreshAccessToken();
res.send("Refreshed Access Token");
} catch (error) {
console.log(error);
}
});

app.listen(3000, () => {
console.log(`Go here to login: http://127.0.0.1:3000/login`);
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twitter-api-sdk",
"version": "1.0.3",
"version": "1.0.4",
"description": "A TypeScript SDK for the Twitter API",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
Loading