The remote server return an error :(401) Unauthorized

Sep 21, 2014 at 8:38 AM
Edited Sep 21, 2014 at 8:45 AM
I am a beginner to use this api.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tweetinvi;
namespace Twitter
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        TwitterCredentials.SetCredentials("xxxxxxx", "xxxxx", "xxxxxx", "xxxxxx");  
        TwitterCredentials.ApplicationCredentials = TwitterCredentials.CreateCredentials("xxxxx", "xxxxxx", "xxxxx", "xxxxxx");

    }

    private void button1_Click(object sender, EventArgs e)
    {
            var tweet = Tweet.PublishTweet("C# API Test");       
    }
}
}


This simple code not give any error but its not also post my tweet...
and when i debug the code then it will give like following....

Exception

The remote server return an error :(401) Unauthorized!

Image
Sep 21, 2014 at 1:58 PM
Edited Sep 24, 2014 at 10:16 AM
Firstly, you do not need to set the application credentials. You only need to call SetCredentials once. Remove the assignment to ApplicationCredentials.

If you are receiving Unauthorized exception. Then it could be because of:
  • Your credentials are invalid.
  • Your application does not have read/write permissions that are required to be able to post tweets.
You can change application permission at http://apps.twitter.com.

imlokesh
Marked as answer by Ketan_Somani on 9/21/2014 at 6:25 AM
Sep 21, 2014 at 2:29 PM
Edited Sep 21, 2014 at 2:37 PM
Thank you @imlokesh.... I have one more problem..

i want to read tweet from my account.for that i am using following code but it doesnt give any output on the console.
plz help me...
         var userStream = Stream.CreateUserStream();
         userStream.TweetCreatedByMe += (s, a) => { Console.WriteLine("I posted {0}", a.Tweet.Text); };
         userStream.StartStream();
Is it proper way to read tweet from my timeline...?


Output console show output like below
Image
Sep 21, 2014 at 2:35 PM
Edited Sep 24, 2014 at 10:16 AM
This will only give you live tweets. Meaning, if you tweet while this code is running, then only you'll receive the tweet. Read up on dev.twitter.com to get more idea about the differences between streaming and rest API.

If you want to get your past tweets, you can use the following code:
var loggedUser = User.GetLoggedUser();
var tweets = loggedUser.GetUserTimeline();
foreach (var tweet in tweets)
{
    Console.WriteLine(tweet.Text);
}
imlokesh
Marked as answer by Ketan_Somani on 9/21/2014 at 6:46 AM
Sep 21, 2014 at 2:48 PM
Thank you again @imlokesh for quick reply...

Code working perfectly..
but i want all tweet which tweet by my friend or any person whom i follow.
Sep 21, 2014 at 2:55 PM
Edited Sep 21, 2014 at 2:55 PM
I suggest that you download the source code of TweetInvi and go through the ExampleInvi project. It'll help you a lot in getting started.
var user = User.GetUserFromScreenName("yourfriend");
var tweets = user.GetUserTimeline(1000); // Get 1000 tweets
foreach (var tweet in tweets)
{
    Console.WriteLine(tweet.Text);
}
The maximum number of tweets that you can get this way is limited by the twitter api. Read up dev.twitter.com for more info.

imlokesh
Marked as answer by imlokesh on 9/21/2014 at 6:56 AM