How to get latest tweets from Twitter REST API?

My blog uses twitter REST API to pull up my latest tweets to the right side navigation. About a year ago twitter deprecated 1.0 API and now every request needs to be authenticated. According to twitter's documentation I can use application-only authentication, since I am only getting tweets and don't post anything.

Application-only authentication approach consists of two steps:

  1. Get access token from
  2. Use access token for any read only operations (get posts, friends, followers, user information or search tweets)

I didn't find any .NET bare-bones example that suits my needs. The code below don't need any third party libraries. It is .NET 4.5 (I used some dynamic and async / await).

Here is how you get access token (step 1 from the above):

public async Task<string> GetAccessToken()
	var httpClient = new HttpClient();
	var request = new HttpRequestMessage(HttpMethod.Post, " ");
	var customerInfo = Convert.ToBase64String(new UTF8Encoding()
							  .GetBytes(OAuthConsumerKey + ":" + OAuthConsumerSecret));
	request.Headers.Add("Authorization", "Basic " + customerInfo);
	request.Content = new StringContent("grant_type=client_credentials", 
                                            Encoding.UTF8, "application/x-www-form-urlencoded");
	HttpResponseMessage response = await httpClient.SendAsync(request);

	string json = await response.Content.ReadAsStringAsync();
	var serializer = new JavaScriptSerializer();
	dynamic item = serializer.Deserialize<object>(json);
	return  item["access_token"];            

Here is how you get tweets:

public async Task<IEnumerable<string>> GetTweets(string userName,int count, string accessToken = null)
	if (accessToken == null)
		accessToken = await GetAccessToken();   

	var requestUserTimeline = new HttpRequestMessage(HttpMethod.Get, 
                               count={0}&screen_name={1}&trim_user=1&exclude_replies=1", count, userName));
	requestUserTimeline.Headers.Add("Authorization", "Bearer " + accessToken);
	var httpClient = new HttpClient();
	HttpResponseMessage responseUserTimeLine = await httpClient.SendAsync(requestUserTimeline);
	var serializer = new JavaScriptSerializer();
	dynamic json = serializer.Deserialize<object>(await responseUserTimeLine.Content.ReadAsStringAsync());
	var enumerableTweets = (json as IEnumerable<dynamic>);

	if (enumerableTweets == null)
		return null;
	return enumerableTweets.Select(t => (string)(t["text"].ToString()));                        

See complete console application at github repository Twitter .NET C# Sample Application

Posted on Monday, August 18, 2014 by | Comments (12) | Add Comment

How to migrate from subversion to git with almost no down time?

Svn to git migration

Last year I was in charge of SVN to Git migration at company that I work for. We wanted to migrate the history as well. In our case there were about 40,000 revisions made during last 8 years. In order to minimize developers downtime I did a lot of scripting preparation ahead of time. Actual switch from SVN to Git took less then 2 hours. Here are the steps that we took.

1. Retrieve a list of all committers

You'll need to create a list of users that have committed to the SVN repo and then convert those users over to the Git format as Subversion only supplies the username of the person committing and not the username and email. To retrieve the list of users from SVN, create a new folder, right click and select Git Bash Here to open a Git command window. Run the following command:

svn log http://url/to/svn/repository -q | awk -F '|' '/^r/ {sub("^ ", "", $2); 
sub(" $", "", $2); 
print $2" = "$2" <"$2">"}' | sort -u > users.txt

Note: this will take a couple of minutes to complete based on the size of your repository, number of commits, and number of committers.

The text file will have separate lines for each committer and will need to be transformed from vkarpach = vkarpach <vkarpach> to vkarpach = Viktar Karpach <>

2. Clone the repository using git-svn

Note - this step will take hours to complete, so it is suggested to run this step over night on dedicated box. Run the following command to convert the repository to a Git repository:

git svn clone --stdlayout --no-metadata -A users.txt http://url/to/svn/repository dest_dir-tmp

3. Make a copy of this folder.

git svn clone takes a lot of time. For our main project it took 48 hours for about 18000 commits. Make a copy of this folder, so you don't need to do it again. Create scripts for next steps, so when you are ready to switch you can do it quickly.

4. Fetch latest commits.

The team continued to use Subversion until a very last moment, so while working on migration scripts time to time I had to fetch latest commits.

git svn fetch
git reset --hard trunk

5. Clean up script.

Delete tags

for t in `git branch -r | grep 'tags/' | sed s_tags/__` ; do
  git tag $t tags/$t^
  git branch -d -r tags/$t

Delete trunk, since we will use master from now on.

git branch -d -r trunk

Remove SVN references

git config --remove-section svn-remote.svn
rm -rf .git/svn .git/{logs/,}refs/remotes/svn/

And finally convert the remaining remote branches to local branches

git config remote.origin.url .
git config --add remote.origin.fetch +refs/remotes/*:refs/heads/*
git fetch

Remove remote branches:

for t in `git branch -r` ; do
  git branch -d -r $t

Git doesn't support space in branch names, so git svn fetch replaced spaces with %20. I think it is more aesthetic to use underscore instead of %20:

for t in `git branch -a|grep '%20'` ; do
  newName=`echo $t | sed 's/%20/-/g'`
  git branch -m $t $newName

You might want to delete some unused branches:

for t in `cat ../list_of_branches_for_deletion.txt`; do 
  git branch -D $t

Where list_of_branches_for_deletion.txt contains branch names that will be deleted. Use following code to populate this files:

git branch -a > ../list_of_branches_for_deletion.txt

Manually edit list_of_branches_for_deletion.txt file. Leave only those branches that you want to delete.

6. Replace any svn externals with git submodules

git submodule add ssh:// ExternalFolderName
git commit -m "Added submodules"

Use sumbodules only for external projects that don't change very often. We had to combine our internal projects in one git repository, since it is hard to maintain submodules for rapidly changing projects. Each project gets its own directory in git repository:

Before migration:


Where svn_main_project has to externals external_1 and external_2.

After migration


You can use following bash script to push everything in sub_folder, so later you can combine repositories. The script will modify commit history as well.

git filter-branch --index-filter \	
	'git ls-files -s | sed "s-\t\"*-&sub_folder/-" |
			git update-index --index-info &&
	 mv "$" "$GIT_INDEX_FILE" || true' HEAD

7. Get your repository onto the server

Create a repository on your git server.

Init local repository

git init

Use following if you are combining repositories:

git remote add external_1 ../external_1/
git pull external_1 master
git remote rm external_1

Add gitignore

cp ../gitignore.txt .gitignore
git add .
git commit -m "Added .gitignore"

Push all branches in one shot:

git remote add origin ssh://
git push --all origin
Posted on Monday, January 27, 2014 by | Add Comment

How to hide SyncToy console window in windows Task Scheduler?

SyncToy is a free app from Microsoft that synchronizes files and folders between different locations. It even has a command line version (SyncToyCmd.exe), which can be used in windows Task Scheduler. Unfortunately Task Scheduler build-in "Hidden" mode didn't hide console window, so I composed from different sources a little vbs script. This script can be used in Task Scheduler, so no console window will show up.


strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")

errReturn = objProcess.Create("C:\Program Files\Synctoy 2.1\SynctoyCmd.exe -R" , null, objConfig, intProcessID)

Posted on Thursday, January 16, 2014 by | Add Comment


Recent Tweets

  • Simon Ince's Blog: Hierarchies with HierarchyID in SQL 2008

Valid HTML5