离子框架 Twitter 集成

Ionic Framework Twitter integration

我目前正在开展一个项目,在该项目中我必须使用 Ionic Framework 进行 Twitter 集成。

我使用的是来自 ionic 论坛的示例程序:http://forum.ionicframework.com/t/twitter-integration-with-jsoauth/3936

在 bitbucket 上可用:https://bitbucket.org/aaronksaunders/ionic.twitter.sample

我已经以两种方式对其进行了测试,即使用 ionic serve 和在模拟器上进行测试,但结果相同:每当我单击登录时,都会出现一个新的浏览器 window,地址为:https://api.twitter.com/oauth/authorize?出现包含以下错误消息。

Whoa there!
There is no request token for this page. That's the special key we need
from applications asking to use your Twitter account. Please go back to
the site or application that sent you here and try again; it was probably 
just a mistake.

我已经把我的推特 API Key 和 API Secret 放在了合适的地方。

您应该尽可能使用 ngCordova

Oauth plugin 文档说明您需要使用 jsSHA 对 Twitter 进行身份验证。

To use Twitter in your project you must have the open source library, jsSHA, included in your project. This is because Twitter requires request signing using HMAC-SHA1, not natively found in JavaScript.

更多信息可在 ngCordova Oauth 插件文档中找到。

我其实很喜欢用hello.js

这是一个很棒的库,可以为您处理社交媒体令牌。

示例初始化:

hello.init({
    facebook : '12345678912345'
}, {
    // Define the OAuth2 return URL
    redirect_uri : 'http://adodson.com/hello.js/redirect.html'
});

登录:

hello( "facebook" ).login().then( function(){
    alert("You are signed in to Facebook");
}, function( e ){
    alert("Signin error: " + e.error.message );
});

登录后,您可以拨打您选择的社交媒体帐户。

按照@darryn.ten 的建议,尝试将 Cordova oAuth 插件与应用内浏览器插件结合使用。以下是如何使用 oAuth 插件触发 Twitter 登录的示例:

控制器

 angular.module('myApp')
       .controller('MyAppCtrl', ['$scope', '$cordovaOauth', function($scope, $cordovaOauth) {

       $scope.twitterLogin = function() {
            $cordovaOauth.twitter(<consumer key>, <secret key>).then(function(r) {
                //retrieve oAuth token from result
            }, function(error) {
                //show error
            });
        }
    }])

查看

<a class="button button-calm" href="#" ng-click="twitterLogin()">Twitter Login</a>

查看文档 here