6

I have a node server that is running. and it sends a post to a Twitter Timeline, which I have an Ionic/Angular Application the consumes the Node Server and sends the message.

however, the problem is that in the node server my user's Twitter account info is hard coded and I would like to know how I can send the user's details that I get from the Twitter connect Plugin.

here is my node sever

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'));

twitter service in my angular/ionic app

export class TwitterserviceService {
  api_url = 'http://localhost:3000';

  constructor(private http: HttpClient) { } 

  tweet(tweetdata: string) {
    return this.http.post<any>(`${this.api_url}/post_tweet/`, {status: tweetdata})
        .pipe(map(tweet => {

            alert("tweet posted")

            return tweet;
        }));
}
}

and here is my angular Code that sends a tweet to the node server

sendTweet() {  
  this.api.tweet('sent from phone')
            .pipe(first())
            .subscribe(
                data => {
                    console.log('yes')
                },
                error => {
                  'failed'
                });
}

connect plugin

twitterLogin() {
    this.twitter.login().then((res: any) => {
      localStorage.setItem('twitterLogin', "true");
      this.firebaseService.twitterDetail(this.userId, res);


      this.twitterUser = true;
      if(this.twitterUser == true){
        this.twitter.showUser()
        .then(user => {
          console.log("User: " + user +
                        'username ' + res.username+ " " + user.username);

        });
      }


    }, (err: any) => {
      alert('error: ' + err);
    });
  }
3
  • Could you show an example of "hard coded" user? Commented Nov 20, 2019 at 20:59
  • the top piece of code is the hardcode(where I enter the client key etc) I want my app to post the user's details that I get from the Twitter Connect Plugin in my Angular Ionic App and post that details to my node server. so that when the user wants to post a message to twitter. it Posts a message to the User's account and not the account that has been hardcoded into the node sever Commented Nov 21, 2019 at 7:38
  • 9
    What you requires is authorization from user to post on behalf of him/her. This usually requires oauth style authentication. Your application should be registered in Twitter. To perform authorization refer to the following process - developer.twitter.com/en/docs/basics/authentication/overview/… The process has been described in the following URL. Check the Application-user authentication section developer.twitter.com/en/docs/basics/authentication/overview/… Commented Nov 24, 2019 at 11:45

1 Answer 1

1

You will need to set params to AuthenticationHeaders while calling the API service which will implement Interceptor interface in Angular.

You can use this for reference HttpRequest with Interceptor in Angular

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.