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