-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoauth-utils.js
54 lines (51 loc) · 1.72 KB
/
oauth-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { OAuth } from "oauth";
import { promisify } from "util";
import dotenv from "dotenv";
dotenv.config();
const TWITTER_CONSUMER_API_KEY = process.env.TWITTER_CONSUMER_KEY;
const TWITTER_CONSUMER_API_SECRET_KEY = process.env.TWITTER_CONSUMER_SECRET;
const TWITTER_CALLBACK_URL = process.env.TWITTER_CALLBACK_URL;
const oauthConsumer = new OAuth(
"https://twitter.com/oauth/request_token",
"https://twitter.com/oauth/access_token",
TWITTER_CONSUMER_API_KEY,
TWITTER_CONSUMER_API_SECRET_KEY,
"1.0A",
TWITTER_CALLBACK_URL,
"HMAC-SHA1"
);
export const oauthGetUserById = (
userId,
{ oauthAccessToken, oauthAccessTokenSecret } = {}
) => {
return promisify(oauthConsumer.get.bind(oauthConsumer))(
`https://api.twitter.com/1.1/users/show.json?user_id=${userId}`,
oauthAccessToken,
oauthAccessTokenSecret
).then((body) => JSON.parse(body));
};
export const getOAuthAccessTokenWith = async ({
oauthRequestToken,
oauthRequestTokenSecret,
oauthVerifier,
} = {}) =>
new Promise((resolve, reject) => {
oauthConsumer.getOAuthAccessToken(
oauthRequestToken,
oauthRequestTokenSecret,
oauthVerifier,
(error, oauthAccessToken, oauthAccessTokenSecret, results) =>
error
? reject(new Error("Error getting OAuth access token"))
: resolve({ oauthAccessToken, oauthAccessTokenSecret, results })
);
});
export const getOAuthRequestToken = async () =>
new Promise((resolve, reject) => {
oauthConsumer.getOAuthRequestToken(
(error, oauthRequestToken, oauthRequestTokenSecret, results) =>
error
? reject(new Error("Error getting OAuth request token"))
: resolve({ oauthRequestToken, oauthRequestTokenSecret, results })
);
});