这个Twitter工厂错误有什么问题?

时间:2016-01-11 21:26:51

标签: ionic-framework ionic angularjs-factory

我试图获取Twitter Feed时出错。它说:" TwitterService未定义"。我不知道为什么会这样。我有所有" lib"安装的文件(sha,ngResource,ngCordoba)。

这是我的控制者:

myApp.controller('showART', ['$scope','$http', '$ionicLoading', '$timeout', '$sce' , '$ionicScrollDelegate' , '$ionicSlideBoxDelegate' , '$rootScope' , '$ionicPlatform' , TwitterService , function($scope,$http, $ionicLoading, $timeout,$sce, $ionicScrollDelegate ,$ionicSlideBoxDelegate , $rootScope , $ionicPlatform, TwitterService) {...}]);

这是我的工厂:

var myApp = angular.module('xxxx.controllers', []); 

myApp.factory('TwitterService', function($cordovaOauth, $cordovaOauthUtility, $http, $resource, $q) {
                var twitterKey = "xxxx";
                var clientId = 'xxxx';
                var clientSecret = 'xxxx';

                function storeUserToken(data) {
                    window.localStorage.setItem(twitterKey, JSON.stringify(data));
                }

                function getStoredToken() {
                    return window.localStorage.getItem(twitterKey);
                }

                function createTwitterSignature(method, url) {
                    var token = angular.fromJson(getStoredToken());
                    var oauthObject = {
                        oauth_consumer_key: clientId,
                        oauth_nonce: $cordovaOauthUtility.createNonce(32),
                        oauth_signature_method: "HMAC-SHA1",
                        oauth_token: token.oauth_token,
                        oauth_timestamp: Math.round((new Date()).getTime() / 1000.0),
                        oauth_version: "1.0"
                    };
                    var signatureObj = $cordovaOauthUtility.createSignature(method, url, oauthObject, {}, clientSecret, token.oauth_token_secret);
                    $http.defaults.headers.common.Authorization = signatureObj.authorization_header;
                }

                function createTwitterPostSignature(method, url, message) {
                    var token = angular.fromJson(getStoredToken());
                    var oauthObject = {
                        oauth_consumer_key: clientId,
                        oauth_nonce: $cordovaOauthUtility.createNonce(32),
                        oauth_signature_method: "HMAC-SHA1",
                        oauth_timestamp: Math.round((new Date()).getTime() / 1000.0),
                        oauth_token: token.oauth_token,
                        oauth_version: "1.0",
                        status: message
                    };
                    var signatureObj = $cordovaOauthUtility.createSignature(method, url, oauthObject, oauthObject, clientSecret, token.oauth_token_secret);
                    $http.defaults.headers.common.Authorization = signatureObj.authorization_header;
                }

                return {
                    initialize: function() {
                        var deferred = $q.defer();
                        var token = getStoredToken();

                        if (token !== null) {
                            deferred.resolve(true);
                        } else {
                            $cordovaOauth.twitter(clientId, clientSecret).then(function(result) {
                                storeUserToken(result);
                                deferred.resolve(true);
                            }, function(error) {
                                deferred.reject(false);
                            });
                        }
                        return deferred.promise;
                    },

                    isAuthenticated: function() {
                        return getStoredToken() !== null;
                    },

                    getHomeTimeline: function() {
                        var home_tl_url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
                        createTwitterSignature('GET', home_tl_url);
                        return $resource(home_tl_url).query().$promise;
                    },
                    updateStatus: function() {
                        var message = "test from ionic";
                        var update_url = 'https://api.twitter.com/1.1/statuses/update.json';
                        var results = createTwitterPostSignature('POST', update_url, message);
                        return $resource(update_url, {'status': message}).save().$promise;
                    },
                    storeUserToken: storeUserToken,
                    getStoredToken: getStoredToken,
                    createTwitterSignature: createTwitterSignature,
                    createTwitterPostSignature: createTwitterPostSignature
                }; });

参考:http://blog.ionic.io/displaying-the-twitter-feed-within-your-ionic-app/

谢谢:)

0 个答案:

没有答案