twitter-api.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpHeaders } from '@angular/common/http';
  3. import { Storage } from '@ionic/storage';
  4. import Twit from 'twit';
  5. @Injectable()
  6. export class TwitterApiProvider {
  7. consumer_key: string = 'UxZkbKotkr8Uc6seupnaZ1kDE';
  8. consumer_secret: string = 'fEAas8iugR60FOEXsFG0iajq6oyfIIXRBVMlTgWxBd1stWIKHq';
  9. access_token_key: string;
  10. access_token_secret: string;
  11. client;
  12. constructor(public http: HttpClient, private storage: Storage) {
  13. console.log('Hello TwitterApiProvider Provider');
  14. Promise.all([this.storage.get('accessTokenKey'), this.storage.get('accessTokenSecret')]).then(values => {
  15. this.access_token_key = values[0];
  16. this.access_token_secret = values[1];
  17. this.initApi();
  18. });
  19. }
  20. private initApi() {
  21. this.client = new Twit({
  22. consumer_key: this.consumer_key,
  23. consumer_secret: this.consumer_secret,
  24. access_token: this.access_token_key,
  25. access_token_secret: this.access_token_secret,
  26. timeout_ms: 60 * 1000, // optional HTTP request timeout to apply to all requests.
  27. })
  28. }
  29. public fetchHomeFeed() {
  30. return this.client.get('statuses/home_timeline')
  31. .then(res => {
  32. return res;
  33. })
  34. .catch(err => {
  35. console.log(err);
  36. });
  37. }
  38. }