skip to Main Content

I am developing an APP where I would like a user to tap a button and send a tweet to his/her timeline.
There are a few known methods of how to do this with Node.JS and Angular, but I am looking for a solution to achieve what I want without a Node Server Running.

Is there a way to embed the Twitter API into your IONIC/Angular APP. and I am not looking for the Ionic Plugin TwitterConnect as that does not have a POST Method.

EDIT

here is the twitter example, but how would I implement this in Angular?

curl -XPOST 
  --url 'https://api.twitter.com/1.1/statuses/update.json?status=hello' 
  --header 'authorization: OAuth
  oauth_consumer_key="oauth_customer_key",
  oauth_nonce="generated_oauth_nonce",
  oauth_signature="generated_oauth_signature",
  oauth_signature_method="HMAC-SHA1",
  oauth_timestamp="generated_timestamp",
  oauth_token="oauth_token",
  oauth_version="1.0"'

here is the link https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update
suggested by @Gonzalo Nicolás
Can anyone assist me with documentation or some coded examples.

4

Answers


  1. Chosen as BEST ANSWER

    So as far as I understand this, you can access the Twitter API with a simple node server. Which originally I thought was daunting but really it is not anything to stress about.

    you can start your node program with creating a project directory

    mkdir server
    cd server
    npm init
    touch server.js
    

    and install the twit api and express js into your node project

    npm install twit body-parser cors express
    

    create your twitter app on the twitter developers page https://apps.twitter.com/

    then copy and paste this code into your server.js file which you need to create in your node application, which you can do simply by creating a file called server.js

    const express = require('express');
    const Twitter = require('twit');
    
    const app = express();
    const client = new Twitter({
      consumer_key:         '...',
      consumer_secret:      '...',
      access_token:         '...',
      access_token_secret:  '...',
    });
    
    
    app.use(require('cors')());
    app.use(require('body-parser').json());
    
    app.post('/post_tweet', (req, res) => {
    
      tweet = req.body;
    
      client
        .post(`statuses/update`, tweet)
        .then(tweeting => {
          console.log(tweeting);
    
          res.send(tweeting);
        })
    
       .catch(error => {
        res.send(error);
      });
    
    
    });
    
    app.listen(3000, () => console.log('Server running'));
    

    then in your angular project just copy and paste this code and you are all set

    api_url = 'http://localhost:3000';
    
      tweet(tweetdata: string) {
        return this.http.post<any>(`${this.api_url}/post_tweet/`, {status: tweetdata})
            .pipe(map(tweet => {
    
                alert("tweet posted")
    
                return tweet;
            }));
    }
    sendTweet() {  
      this.tweet('This is app code')
                .pipe(first())
                .subscribe(
                    data => {
                        console.log('yes')
                    },
                    error => {
                      'failed'
                    });
    }
    

  2. sorry for my english, i’m from argentina. your solution is POST statuses/update API,
    This solution is from twitter, If you search on that, you will find all the documentation and structure that you have to send
    assuming you use auth

    link: https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update

    Login or Signup to reply.
  3. you can try native twitter widget script for tweet in timeline using following code : (I didn’t try it in angular/ionic, but working in my html.)

    // initialization : 
    
    window.twttr = (function (d, s, id) {
        var t, js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s);
        js.id = id;
        js.src = "https://platform.twitter.com/widgets.js";
        fjs.parentNode.insertBefore(js, fjs);
        return window.twttr || (t = {
            _e: [],
            ready: function (f) {
                t._e.push(f)
            }
        });
    }(document, "script", "twitter-wjs"));
    
    // on button click, this will fire  (button will automatically generated by widget.)
    
    twttr.ready(function (twttr) {
        twttr.widgets.createShareButton(
            'link you want to share',
            document.getElementById('twr'), {
                url: 'link that you want to share',
                count: '1',
                size: 'large'
            }).then(function (el) {
            console.log('button created');
        });
        twttr.events.bind('tweet', function (event) {
            console.log('tweet', event);
        });
    });
    

    note : Maybe window.twttr not working in angular so you can use window['twttr'] instead of window.twttr variable.

    Login or Signup to reply.
  4. This will be a helpful article

    const express = require('express');
    const Twitter = require('twit');
    
    const app = express();
    const client = new Twitter({
      consumer_key: 'Consumer Key Here',
      consumer_secret: 'Consumer  Secret  Here',
      access_token: 'Access Token Here',
      access_token_secret: 'Token  Secret Here'
    });
    
    
    app.use(require('cors')());
    app.use(require('body-parser').json());
    
    app.post('/post_tweet', (req, res) => {
    
      tweet = {status:"Hello world"};
    
        client
          .post(`statuses/update`, tweet)
          .then(timeline => {
            console.log(timeline);
    
            res.send(timeline);
          })
    
         .catch(error => {
          res.send(error);
        });
    
    
    });
    
    app.listen(3000, () => console.log('Server running'));
    

    Angular Twitter Service

    export class TwitterService {
    
     api_url = 'http://localhost:3000';
    
      constructor(private http: HttpClient) { }
    
      getTimeline() {
        return this.http
          .get<any[]>(this.api_url+'/home_timeline')
          .pipe(map(data => data));
    
      }
    
      getMentions() {
        return this.http
          .get<any[]>(this.api_url+'/mentions_timeline')
          .pipe(map(data => data));
    
      }
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search