Martin Ocando
Martin Ocando

Reputation: 1828

Twitter OAuth Ionic 2

Its possible generate a Twitter token and secret token in Nodejs and after use it to open the browser for authenticate with "https://api.twitter.com/oauth/authenticate"?

I use this way to get the token:

app.get('/auth/twitter/token', function (req, res) {
    var requestTokenUrl = 'https://api.twitter.com/oauth/request_token';
    var requestTokenOauth = {
        consumer_key: "2z8MTR8KAZuFafPHsEQ0ZBgo1",
        consumer_secret: "ksPiaQz7ihCrOh3m4iRCsXZzQuSkkmcv4CLGiJQwREWeaQl7St"
    };
    request.post({
       url: requestTokenUrl,
       oauth: requestTokenOauth
    }, function (err, response, body) {
        var oauthToken = qs.parse(body);
        res.send(oauthToken);
    });
});

When I get this token in the client "https://api.twitter.com/oauth/authenticate?oauth_token=TOKEN" I got this problem: "This page is no longer valid. It's looks like someone already used the token information your provider, blabla.."

The problem is due to the way that I get the Token?

I'm using ng2-cordova-auth but this lib dont have twitter auth, I'm just trying to implement

This is my implementation:

"use strict";
var utility_1 = require("../utility");
var PROVIDER_NAME = "Twitter";
var Twitter = (function () {
    function Twitter(options) {
        this.twitterOptions = options;
        this.flowUrl = ""
    }
    Twitter.prototype.login = function (token, tokenSecret) {
        var _this = this;
        return new Promise(function (resolve, reject) {
        _    this.flowUrl = "https://api.twitter.com/oauth/authenticate?oauth_token="+token;
            var browserRef = window.cordova.InAppBrowser.open(_this.flowUrl);
            browserRef.addEventListener("loadstart", function (event) {
                if ((event.url).indexOf(_this.twitterOptions.redirectUri) === 0) { 
                    browserRef.removeEventListener("exit", function (event) { });
                    browserRef.close();
                    var parsedResponse = event.url.split("?")[1].split("&");
                    if (parsedResponse) {
                        resolve(parsedResponse);
                    }
                    else {
                        reject("Problem authenticating with " + PROVIDER_NAME);
                    }
                }

           });
            browserRef.addEventListener("exit", function (event) {
                reject("The " + PROVIDER_NAME + " sign in flow was canceled");
            });
         });
    };
    return Twitter;
}());
exports.Twitter = Twitter;

In my component/controller I make this:

//With twitterToken I get the token from NodeJs
this.API.twitterToken().subscribe(
    data => {
        this.twitterOAuth.login(data.oauth_token, data.oauth_token_secret).then((success) => {
        alert(JSON.stringify(success))
        }, (error) => {
           alert(JSON.stringify(error));
        });
    },
    err => alert(JSON.stringify(err))
);

Upvotes: 0

Views: 956

Answers (1)

Artur Alkaim
Artur Alkaim

Reputation: 185

Have you tried the Twitter Connect plugin? Does this help?

Plugin to use Twitter Single Sign On Uses Twitter's Fabric SDK

An example of use is

import {TwitterConnect} from 'ionic-native';

function onSuccess(response) {
  console.log(response);

  // Will console log something like:
  // {
  //   userName: 'myuser',
  //   userId: '12358102',
  //   secret: 'tokenSecret'
  //   token: 'accessTokenHere'
  // }
}

TwitterConnect.login().then(onSuccess, onError);

Upvotes: 1

Related Questions