Skip to content

Commit b5f312b

Browse files
author
Alan Lee
committed
Added JS SDK samples for Bookmarks
1 parent bb663f4 commit b5f312b

File tree

3 files changed

+276
-0
lines changed

3 files changed

+276
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
const { Client, auth } = require("twitter-api-sdk");
2+
3+
const readline = require("readline").createInterface({
4+
input: process.stdin,
5+
output: process.stdout,
6+
});
7+
8+
//Helper function to parse callback
9+
const getQueryStringParams = (query) => {
10+
return query
11+
? (/^[?#]/.test(query) ? query.slice(1) : query)
12+
.split(/[\?\&]/)
13+
.reduce((params, param) => {
14+
let [key, value] = param.split("=");
15+
params[key] = value
16+
? decodeURIComponent(value.replace(/\+/g, " "))
17+
: "";
18+
return params;
19+
}, {})
20+
: {};
21+
};
22+
23+
//Helper terminal input function
24+
async function input(prompt) {
25+
return new Promise(async (resolve, reject) => {
26+
readline.question(prompt, (out) => {
27+
readline.close();
28+
resolve(out);
29+
});
30+
});
31+
}
32+
33+
// The code below sets the consumer key and consumer secret from your environment variables
34+
// To set environment variables on macOS or Linux, run the export commands below from the terminal:
35+
// export CLIENT_ID='YOUR-CLIENT-ID'
36+
// export CLIENET_SECRET='YOUR-CLIENT-SECRET'
37+
const CLIENT_ID = process.env.CLIENT_ID;
38+
const CLIENT_SECRET = process.env.CLIENT_SECRET;
39+
40+
// Optional parameters for additional payload data
41+
const params = {
42+
expansions: "author_id",
43+
"user.fields": ["username", "created_at"],
44+
"tweet.fields": ["geo", "entities", "context_annotations"],
45+
};
46+
47+
(async () => {
48+
const authClient = new auth.OAuth2User({
49+
client_id: CLIENT_ID,
50+
client_secret: CLIENT_SECRET,
51+
callback: "https://www.example.com/oauth",
52+
scopes: ["tweet.read", "users.read", "bookmark.read"],
53+
});
54+
55+
const client = new Client(authClient);
56+
const STATE = "my-state";
57+
58+
//Get authorization
59+
const authUrl = authClient.generateAuthURL({
60+
state: STATE,
61+
code_challenge: "challenge",
62+
});
63+
64+
console.log(`Please go here and authorize:`, authUrl);
65+
66+
//Input users callback url in termnial
67+
const redirectCallback = await input("Paste the redirected callback here: ");
68+
69+
try {
70+
//Parse callback
71+
const { state, code } = getQueryStringParams(redirectCallback);
72+
if (state !== STATE) {
73+
console.log("State isn't matching");
74+
}
75+
//Gets access token
76+
await authClient.requestAccessToken(code);
77+
78+
//Get the user ID
79+
const {
80+
data: { id },
81+
} = await client.users.findMyUser();
82+
83+
//Makes api call
84+
const getBookmark = await client.bookmarks.getUsersIdBookmarks(id, params);
85+
console.dir(getBookmark, {
86+
depth: null,
87+
});
88+
process.exit();
89+
} catch (error) {
90+
console.log(error);
91+
}
92+
})();
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
const { Client, auth } = require("twitter-api-sdk");
2+
3+
const readline = require("readline").createInterface({
4+
input: process.stdin,
5+
output: process.stdout,
6+
});
7+
8+
//Helper function to parse callback
9+
const getQueryStringParams = (query) => {
10+
return query
11+
? (/^[?#]/.test(query) ? query.slice(1) : query)
12+
.split(/[\?\&]/)
13+
.reduce((params, param) => {
14+
let [key, value] = param.split("=");
15+
params[key] = value
16+
? decodeURIComponent(value.replace(/\+/g, " "))
17+
: "";
18+
return params;
19+
}, {})
20+
: {};
21+
};
22+
23+
//Helper terminal input function
24+
async function input(prompt) {
25+
return new Promise(async (resolve, reject) => {
26+
readline.question(prompt, (out) => {
27+
readline.close();
28+
resolve(out);
29+
});
30+
});
31+
}
32+
33+
// The code below sets the consumer key and consumer secret from your environment variables
34+
// To set environment variables on macOS or Linux, run the export commands below from the terminal:
35+
// export CLIENT_ID='YOUR-CLIENT-ID'
36+
// export CLIENET_SECRET='YOUR-CLIENT-SECRET'
37+
const CLIENT_ID = process.env.CLIENT_ID;
38+
const CLIENT_SECRET = process.env.CLIENT_SECRET;
39+
40+
// Include the ID of the Tweet you wish to bookmark
41+
const params = {
42+
tweet_id: "replace-with-tweet-id",
43+
};
44+
45+
(async () => {
46+
const authClient = new auth.OAuth2User({
47+
client_id: CLIENT_ID,
48+
client_secret: CLIENT_SECRET,
49+
callback: "https://www.example.com/oauth",
50+
scopes: ["tweet.read", "users.read", "bookmark.write"],
51+
});
52+
53+
const client = new Client(authClient);
54+
const STATE = "my-state";
55+
56+
//Get authorization
57+
const authUrl = authClient.generateAuthURL({
58+
state: STATE,
59+
code_challenge: "challenge",
60+
});
61+
62+
console.log(`Please go here and authorize:`, authUrl);
63+
64+
//Input users callback url in termnial
65+
const redirectCallback = await input("Paste the redirected callback here: ");
66+
67+
try {
68+
//Parse callback
69+
const { state, code } = getQueryStringParams(redirectCallback);
70+
if (state !== STATE) {
71+
console.log("State isn't matching");
72+
}
73+
//Gets access token
74+
await authClient.requestAccessToken(code);
75+
76+
//Get the user ID
77+
const {
78+
data: { id },
79+
} = await client.users.findMyUser();
80+
81+
//Makes api call
82+
const postBookmark = await client.bookmarks.postUsersIdBookmarks(
83+
id,
84+
params
85+
);
86+
console.dir(postBookmark, {
87+
depth: null,
88+
});
89+
process.exit();
90+
} catch (error) {
91+
console.log(error);
92+
}
93+
})();
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const { Client, auth } = require("twitter-api-sdk");
2+
3+
const readline = require("readline").createInterface({
4+
input: process.stdin,
5+
output: process.stdout,
6+
});
7+
8+
//Helper function to parse callback
9+
const getQueryStringParams = (query) => {
10+
return query
11+
? (/^[?#]/.test(query) ? query.slice(1) : query)
12+
.split(/[\?\&]/)
13+
.reduce((params, param) => {
14+
let [key, value] = param.split("=");
15+
params[key] = value
16+
? decodeURIComponent(value.replace(/\+/g, " "))
17+
: "";
18+
return params;
19+
}, {})
20+
: {};
21+
};
22+
23+
//Helper terminal input function
24+
async function input(prompt) {
25+
return new Promise(async (resolve, reject) => {
26+
readline.question(prompt, (out) => {
27+
readline.close();
28+
resolve(out);
29+
});
30+
});
31+
}
32+
33+
// The code below sets the consumer key and consumer secret from your environment variables
34+
// To set environment variables on macOS or Linux, run the export commands below from the terminal:
35+
// export CLIENT_ID='YOUR-CLIENT-ID'
36+
// export CLIENET_SECRET='YOUR-CLIENT-SECRET'
37+
const CLIENT_ID = process.env.CLIENT_ID;
38+
const CLIENT_SECRET = process.env.CLIENT_SECRET;
39+
40+
// Include the ID of the Tweet you wish to unbookmark
41+
const tweetId = "replace-with-tweet-id";
42+
43+
(async () => {
44+
const authClient = new auth.OAuth2User({
45+
client_id: CLIENT_ID,
46+
client_secret: CLIENT_SECRET,
47+
callback: "https://www.example.com/oauth",
48+
scopes: ["tweet.read", "users.read", "bookmark.write"],
49+
});
50+
51+
const client = new Client(authClient);
52+
const STATE = "my-state";
53+
54+
//Get authorization
55+
const authUrl = authClient.generateAuthURL({
56+
state: STATE,
57+
code_challenge: "challenge",
58+
});
59+
60+
console.log(`Please go here and authorize:`, authUrl);
61+
62+
//Input users callback url in termnial
63+
const redirectCallback = await input("Paste the redirected callback here: ");
64+
65+
try {
66+
//Parse callback
67+
const { state, code } = getQueryStringParams(redirectCallback);
68+
if (state !== STATE) {
69+
console.log("State isn't matching");
70+
}
71+
//Gets access token
72+
await authClient.requestAccessToken(code);
73+
74+
//Get the user ID
75+
const {
76+
data: { id },
77+
} = await client.users.findMyUser();
78+
79+
//Makes api call
80+
const deleteBookmark = await client.bookmarks.usersIdBookmarksDelete(
81+
id,
82+
tweetId
83+
);
84+
console.dir(deleteBookmark, {
85+
depth: null,
86+
});
87+
process.exit();
88+
} catch (error) {
89+
console.log(error);
90+
}
91+
})();

0 commit comments

Comments
 (0)