1 | import * as oauth from 'oauth';
|
2 |
|
3 | export interface AccountsOauthTwitterOptions {
|
4 | key: string;
|
5 | secret: string;
|
6 | }
|
7 |
|
8 | export class AccountsOAuthTwitter {
|
9 | private options: AccountsOauthTwitterOptions;
|
10 | private oauth: any;
|
11 |
|
12 | constructor(options: AccountsOauthTwitterOptions) {
|
13 | this.options = options;
|
14 | this.oauth = new oauth.OAuth(
|
15 | 'https://twitter.com/oauth/request_token',
|
16 | 'https://twitter.com/oauth/access_token',
|
17 | this.options.key,
|
18 | this.options.secret,
|
19 | '1.0A',
|
20 | null,
|
21 | 'HMAC-SHA1'
|
22 | );
|
23 | }
|
24 |
|
25 | public authenticate(params) {
|
26 | return new Promise((resolve, reject) => {
|
27 | this.oauth.get(
|
28 | 'https://api.twitter.com/1.1/account/verify_credentials.json?include_email=true',
|
29 | params.access_token,
|
30 | params.access_token_secret,
|
31 | (err, data) => {
|
32 | if (err) {
|
33 | reject(err);
|
34 | } else {
|
35 | data = JSON.parse(data);
|
36 | const user = {
|
37 | id: data.id_str,
|
38 | screenName: data.screen_name,
|
39 | profilePicture: data.profile_image_url_https,
|
40 | email: data.email,
|
41 | accessToken: params.access_token,
|
42 | accessTokenSecret: params.access_token_secret,
|
43 | };
|
44 | resolve(user);
|
45 | }
|
46 | }
|
47 | );
|
48 | });
|
49 | }
|
50 | }
|