|
@@ -0,0 +1,69 @@
|
|
|
+import { Injectable } from '@angular/core';
|
|
|
+import { HttpClient, HttpHeaders } from '@angular/common/http';
|
|
|
+import { Storage } from '@ionic/storage';
|
|
|
+import CryptoJS from 'crypto-js';
|
|
|
+
|
|
|
+
|
|
|
+@Injectable()
|
|
|
+export class TwitterApiProvider {
|
|
|
+ consumer_key: string = 'UxZkbKotkr8Uc6seupnaZ1kDE';
|
|
|
+ consumer_secret: string = 'fEAas8iugR60FOEXsFG0iajq6oyfIIXRBVMlTgWxBd1stWIKHq';
|
|
|
+ access_token_key: string;
|
|
|
+ access_token_secret: string;
|
|
|
+ client;
|
|
|
+
|
|
|
+ constructor(public http: HttpClient, private storage: Storage) {
|
|
|
+ console.log('Hello TwitterApiProvider Provider');
|
|
|
+
|
|
|
+ Promise.all([this.storage.get('accessTokenKey'), this.storage.get('accessTokenSecret')]).then(values => {
|
|
|
+ this.access_token_key = values[0];
|
|
|
+ this.access_token_secret = values[1];
|
|
|
+ this.initApi();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ initApi() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private buildHeaders(): HttpHeaders {
|
|
|
+ let oauth_consumer_key = this.consumer_key;
|
|
|
+ let oauth_nonce = this.getNonce();
|
|
|
+ let oauth_signature = this.getSignature('get', 'https://api.twitter.com/1.1/statuses/home_timeline.json', '');
|
|
|
+ let oauth_signature_method = 'HMAC-SHA1';
|
|
|
+ let oauth_timestamp = Math.floor(Date.now() / 1000).toString();
|
|
|
+ let oauth_token = this.access_token_key;
|
|
|
+ let oauth_version = '1.0';
|
|
|
+
|
|
|
+ let authToken = `OAuth oauth_consumer_key="${oauth_consumer_key}", oauth_nonce="${oauth_nonce}", oauth_signature="${oauth_signature}", oauth_signature_method="${oauth_signature_method}", oauth_timestamp="${oauth_timestamp}", oauth_token="${oauth_token}", oauth_version="${oauth_version}"`;
|
|
|
+ console.log(authToken);
|
|
|
+
|
|
|
+ return new HttpHeaders({
|
|
|
+ 'Authorization': authToken
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private getNonce(numChars = 32): string {
|
|
|
+ var st = "0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz";
|
|
|
+ var l = st.length;
|
|
|
+ var result = "";
|
|
|
+
|
|
|
+ for (var i = 0; i < numChars; i++) {
|
|
|
+ var randomIndex = Math.floor(Math.random() * l);
|
|
|
+ result = result + st.charAt(randomIndex);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private getSignature(method: string, url: string, parameterString: string): string {
|
|
|
+ let shaKey = encodeURIComponent(this.consumer_secret) + "&" + encodeURIComponent(this.access_token_secret);
|
|
|
+ let signatureBaseString = `${method.toUpperCase()}&${encodeURIComponent(url)}&${encodeURIComponent(parameterString)}`;
|
|
|
+ let signature = CryptoJS.HmacSHA1(signatureBaseString, shaKey);
|
|
|
+ return CryptoJS.enc.Base64.stringify(signature);
|
|
|
+ }
|
|
|
+
|
|
|
+ fetchHomeFeed() {
|
|
|
+ return this.http.get('https://api.twitter.com/1.1/statuses/home_timeline.json', { headers: this.buildHeaders() });
|
|
|
+ }
|
|
|
+}
|