Hackviking He killed Chuck Norris, he ruled dancing so he took up a new hobby…


Easiest way to embed assembly’s into your executables

I usually write a lot of command line tools for different quick fixes. Distributing them to clients with a lot of attached DLL files are not always the best. They get copied around different servers and sooner or later someone runs them without the proper DLL files present. For quick and dirty tools there might be faulty error handling and the tool fails in a critical moment due to the missing assembly. I usually embed the DLL file into the executable, especially when doing command line tools. By far the easiest way of doing that in .Net is to install a Nuget package called Costura.Fody!

Just install it into the project and build the release and all assembly's get compressed and embedded into the executable. Visit Costura.Fody github page for more information!


Webmatrix 3: Access dev site from other device

Webmatrix 3 Loggo

Webmatrix 3 is a free tool by Microsoft for web development. It supports a range of languages, frameworks and CMS like WordPress for instance. It automates the process of installing frameworks, compilers and database backends. It will quickly setup a development environment on your computer so you can start develop. Lately I have been using it to create a new responsive theme for this website. It works out of the box for testing on localhost but not everybody is running 1080p monitors and I need to test on my phone, tablet and laptop as well. Out of the box it doesn't support this but here is how you set it up.
Continue reading...


Facebook API login flow for desktop application

Facebook like

When developing desktop applications that interacts with Facebook you have to implement the login flow your self. After the login flow completes you can use the normal Facebook SDK libraries by supplying it with the access token your received. When implementing the login flow you have to make sure that you receive the access you requested. You can get a partial approval by the user and not be able to access all the scopes you need. In this example I have implemented the Facebook login flow into a C# .Net desktop application. Full source is available for download.

Continue reading...


Python: Remove querystring from URL

Needed to clean an URL from it's querystring in Python. Found a lot of examples telling me to use urlparse and then put all the bits and pieces back together. An easier and more efficient  way is this:

url = 'http://www.hackviking.com/?var=value'
url = url[:url.find('?')]

url now reads 'http://www.hackviking.com/'


.Net C#: Webbrowser control print line break

I was updating an old project for a client today. I needed to print a simple list and figured that the easiest way should be to format the data in HTML. Most developers out there has built webpages now days and we all know that it is a quick way to format the information and print it. So after a quick creation of the HTML i found an article named "Displaying custom HTML in WebBrowser control" by Gunnar Peipman. So easy enough to print the custom HTML from the control, based on Gunnar's article I came up with this code:

WebBrowser webPrint = new WebBrowser();

if (webPrint.Document != null) { 
webPrint.DocumentText = printHTML.ToString();
webPrint.DocumentCompleted += webPrint_DocumentCompleted; 

void webPrint_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { 
    WebBrowser webPrint = (WebBrowser)sender; 

If you don't use the "DocumentComplete" event handler your printout will be blank. In the print I used a basic HTML table to show the data with rows like this:

<table cellpadding="3" style="width:100%">
<tr style="color:White;background-color:Black;font-weight:bold;"><td>Box ID</td><td>Gång</td><td>Hylla</td><td>Nivå</td><td>Nåbar från golvet</td></tr>

When printing data that took up several pages I ended up with this:

pagebreak error

The data was cut of at the line breaks. I Googled a lot and found a few suggestions about css "page-break.." but they didn't have any effect on the webbrowser control print layout. Finally I found a simple soulition, just add a bland column to the left of the table and the line break works. Like this:

<table cellpadding="3" style="width:100%">
<tr style="color:White;background-color:Black;font-weight:bold;"><td> </td><td>Box ID</td><td>Gång</td><td>Hylla</td><td>Nivå</td><td>Nåbar från golvet</td></tr>
<tr><td> </td><td>10346</td><td>AB</td><td>3</td><td>F</td><td>True</td></tr>
<tr><td> </td><td>10294</td><td>AB</td><td>3</td><td>D</td><td>True</td></tr>
<tr><td> </td><td>10296</td><td>AB</td><td>3</td><td>C</td><td>True</td></tr>
<tr><td> </td><td>10298</td><td>AB</td><td>3</td><td>C</td><td>True</td></tr>
<tr><td> </td><td>10300</td><td>AB</td><td>3</td><td>B</td><td>True</td></tr>
<tr><td> </td><td>10302</td><td>AB</td><td>3</td><td>B</td><td>True</td></tr>
<tr><td> </td><td>10382</td><td>AB</td><td>4</td><td>F</td><td>True</td></tr>

Then the printout looked like this:

pagebreak fix


Google Picasa API Python: Developers Guide

Before you ask... Yes I struggled with the title of this post, but give me a break it's 4:30 am here! 🙂

I'm currently developing in python for Google App Engine and are using the Picasa API. I found this great guide from Google with exampels: https://developers.google.com/picasa-web/docs/1.0/developers_guide_python It's far from complete but the examples pushes me in the right direction and then some testing, trial and error gives me a chance to learn even more! So i thought I would share one of the things that got me in to trouble this morning.

I was trying to add a new album to the feed and got this error:

(404, 'Not Found', 'Unknown user.')

Even though the user is authenticated via oauth. But then i realized that this was the same issue I head before, the users e-mail isn't in the mix. I needed it for layout purposes before, to display profiles and so on, you can read about that here: Python: Get user info after oauth All the request I have done before, getting feeds of albums, pictures and so on, has been uri input from me in the code. This function just looks like this:

gd_client.InsertAlbum(title='Test', summary='a test album')

Where the gd_client is an instance of gdata.photos.service.PhotosService() with the oauth token all-ready specified using the code example from the official Google guide and spiced with the functions from my previous mentioned post. But still it doesn't work! So I realized that even though it is authorized it has no idea who the user is trying to insert the new album. According to the feed documentation you should do a post like this:

POST https://picasaweb.google.com/data/feed/api/user/userID

<entry xmlns='http://www.w3.org/2005/Atom'
<title type='text'>Trip To Italy</title>
<summary type='text'>This was the recent trip I took to Italy.</summary>
<media:keywords>italy, vacation</media:keywords>
<category scheme='http://schemas.google.com/g/2005#kind'

So here is what stuff goes wrong. Looking what the gd_client is actually doing is posting to https://picasaweb.google.com/data/feed/api/user/. There is a really simple soloution for this, if we return to the oauth example from the guide:

gd_client = gdata.photos.service.PhotosService()
gd_client.auth_token = authsub_token

This is a cut out where you get the "use once" token back and upgrade it to a session token. Here you can do two things to make the gd_client work with the oauth user as a default all the time, either:

gd_client = gdata.photos.service.PhotoService(email='default')


gd_client.email = 'default'

Then it all will work just fine!


Python: Get user info after oauth

Building webapps with Google APIs are pretty straight forward for the most part. Specially when using the Google API Client Libraries! But if you want to accomplish something that are outside of all the nice tutorials that Google supply us with it get's trickier! Being a C# programmer for many years I have noticed that there is loads of examples, tutorials and how-to write ups on the web for any and (almost) all problems I might run into. With the Google APIs and Google App Engine, not as much. I don't know why it should be pretty many people in that community willing to share?


I'm currently developing a web application running on Google App Engine that uses the Picasa Web API. User get's redirected to the oauth site to approve access and I get a token back, easy enough! Getting lists of albums, pictures and movies is easy but getting user info? I'm fairly new to both python and Google App Engine so I browsed around for a long while without finding any solution to what I wanted to do! So before going to bed, defeated, I published a question on stackoverflow: http://stackoverflow.com/questions/19473250/how-to-get-user-email-after-oauth-with-google-api-python-client

Usually you get pretty quick answers from your peers but this time, nothing! Again... I'm I the only one doing stuff like this? Doubt it!
So I needed to solve the issue my self, getting dirty!

Break it down...

So my problem was that I wanted the users profile information like email, name and so on. I started by reading the protocol definition for the Picasa Web API, found here: https://developers.google.com/picasa-web/docs/2.0/reference Soon I realized that what I needed wasn't even sent back to me. So I needed to request the information!

After some Googleing for the issue I found a couple of suggestions that I should use Google Contacts API and get the author field from my own contacts, not a pretty solution!

Went over to the OAuth 2.0 Playground to check out what feeds would help me get the user info. In the list on the playground I didn't find much! Some of the other services provided the information with there feeds but I wanted something that just replied the information that I wanted. So I went to the source, Google APIs Explorer. I found a couple of scopes that would fit my needs:

  • https://www.googleapis.com/auth/userinfo.email
  • https://www.googleapis.com/auth/userinfo.profile

Checking out the scopes

So over to the Oauth playground and tried them out to see what info I could get out of them.

Scope: https://www.googleapis.com/auth/userinfo.email

Request URL: https://www.googleapis.com/userinfo/v2/me


"id": "113303504361682404626",
"email": "puh.kallsbo@gmail.com",
"verified_email": true

More or less exactly what I was looking for, needed the e-mail to get the gravatar. But why stop here let's see what else is available...

Scope: https://www.googleapis.com/auth/userinfo.profile

Request URL: https://www.googleapis.com/userinfo/v2/me


"id": "113303504361682404626",
"name": "Kristofer Källsbo",
"given_name": "Kristofer",
"family_name": "Källsbo",
"link": "https://profiles.google.com/puh.kallsbo",
"picture": "https://lh3.googleusercontent.com/-n6hDipRgm5k/AAAAAAAAAAI/AAAAAAAAVYY/XZd9zlnMmGk/photo.jpg",
"gender": "male",
"locale": "en"

So this more or less gave me all the user info but not the e-mail. So combining this with the one above would be a good idea!

Getting down to business (python)

So now I knew where to find the information. But how do a I access it as well as the Picasa Web API? So if we start from this exampel: https://developers.google.com/picasa-web/docs/1.0/developers_guide_python?csw=1#AuthSub

def GetAuthSubUrl():
next = 'http://www.example.com/welcome.pyc'
scope = 'https://picasaweb.google.com/data/'
secure = False
session = True
gd_client = gdata.photos.service.PhotosService()
return gd_client.GenerateAuthSubURL(next, scope, secure, session);
authSubUrl = GetAuthSubUrl();
print '&lt;a href="%s"&gt;Login to your Google account&lt;/a&gt;' % authSubUrl

So we need to add an additional scope for the user to authorize. Instead of passing a string as the scope variable we can pass a list of strings like this:

scope = ['https://picasaweb.google.com/data/', 'https://www.googleapis.com/auth/userinfo.email']

That will generate a token that is valid for both scopes! Following the example about the Picasa Web API we go ahead and retrieve a list of the users albums, but then what?

#Setup a basic gdata client
userClient = gdata.client.GDClient()
#Create a gauth sub token object from the session token we allready have in the gd_client
oauthToken = gdata.gauth.AuthSubToken(gd_client.GetAuthSubToken())
#Get the request, this userinfo is in json
response = userClient.request('GET','https://www.googleapis.com/userinfo/v2/me', oauthToken)
#Parse the webresponse
jobj = json.loads(response.read())
self.response.write('Your email is: {0}'.format(jobj['email']))

We start a gdata client and use that to retrieve the json. Line by line we actually doing the following:

userClient = gdata.client.GDClient() - Just creating an instanse of the Gdata client.

oauthToken = gdata.gauth.AuthSubToken(gd_client.GetAuthSubToken()) - Retrieving the OAuth token all ready used in the gd_client for the Picasa Web Albums. To be able to use it in the Gdata client we have to pass it as an object of type AuthSubToken or similar.

response = userClient.request('GET','https://www.googleapis.com/userinfo/v2/me', oauthToken) - Will return a standard python httplib HTTPResponse with the data returned from the server.

jobj = json.loads(response.read()) - The .read() function returns the body of the HTTPResponse object that we then can parse into a json object.

self.response.write('Your email is: {0}'.format(jobj['email'])) - Finally we have the users e-mail to do what we need to display profile info and so on...


Not all the documentation is great when it comes to Google APIs and Google App Engine. I have been programming sense the age of 8 so I usually find my way through it but I realize that this isn't a beginners language or field. On the other hand all the technical info is there like protocol specifications and stuff like that. Then there is some great tools to try the API out like OAuth playground. I hope this helps someone and a complete demo project for Google App Engine is available on Google Code, link below.



Picasa Web Downloader Source

Had a few downloads of the Picasa Web Downloader that I put up yesterday. Also had a few request of the source code, people seem to be nervous about putting there login information into an application that isn't open source.

So now it is open source available at: https://code.google.com/p/picasa-web-downloader/
Compiled and readu to run:


PSC: Move share

shareerrorI have decided what the Power of scripting script should do! If you move a share by moving it in windows explorer the share will stop work. If you move the folder with File System Object from vbscript the share will still be there but will be pointing at the old path that no longer exists. So how to solve this?

I know a few ways that you don't. First attempt was to use the winnt://localhost/lanmanserver/ object to try updating the path of the share. No luck the property is read only!

Next attempt was using WMI instead. Same thing, unable to update the path. So i thought I delete the share and then create it again. Well enough that worked how ever I didn't manage to get the rights for the share to come along to the new one. There is methods to extract the access mask for the share and supply it when creating a new one, so I think it can be solved. I didn't have the time to do so this time and I think I proved my point about the power of scripting.

At the same time I have learned or realized a few things that I didn't thought about. Most scripts I have written in the past has been for controlled environments.  By that I mean a system where a specific task was asked from the script and the run was always with the same parameters. Here I'm writing a script that should run on different versions of the operating system and with several different variables.

So some of the limitations I ran into can be expressed in the words: Access Denied! It was issues with running some of the commands from the script without running it with elevation. This is no issue running it from the command prompt with cscript moveshare.vbs as long as you start the cmd with elevation (right-click and select Run As Administrator). There are ways around this even with the "double-click execution" of scripts that are discussed in detail here.

So the window version of the script is pretty useless with a standard setup. How ever if you run the script with elevation from the cmd you will be able to move shares around on the disk. The script is pretty ruff around the edges and some more error handling and stuff needs to be added. There probably will be solvable to get the share to keep it's access list as well. This script should be seen as a proof of concept.

'*		Power of scripting challange!	
'*		moveshare.vbs
'*		Purpose: Move a shared folder from one location to another
'*		without losing the share!
'*		Written by Kristofer Källsbo 
'*		http://www.hackviking.com
Option explicit

'Get the filesystem object ready
Set FSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)

'First check if we are running in console mode or window mode
If (InStr(1, WScript.FullName, &quot;cscript&quot;, vbTextCompare)) Then
ElseIf (InStr(1, WScript.FullName, &quot;wscript&quot;, vbTextCompare)) Then
	' Hope this never fires! 🙂
    wscript.echo &quot;How the hell did you start this script?&quot;
End If

'Clean up
Set FSO = Nothing

'Handles the command line run of the script
Sub InitConsole()
	'Check if we have any parameters
	If (Wscript.Arguments.Count &lt; 2) Then
		'Display help
		wscript.echo &quot;Move share script&quot;
		wscript.echo &quot;&quot;
		wscript.echo &quot;Use: cscript moveshare.vbs {source} {destination}&quot;
		wscript.echo &quot;&quot;
		wscript.echo &quot;source = complete path of the shared folder&quot;
		wscript.echo &quot;destination = full path of the folder where the shared folder should be moved&quot;
		wscript.echo &quot;&quot;
		wscript.echo &quot;moveshare.vbs		Kristofer Källsbo	http://www.hackviking.com&quot;
		'Verrify the parameters
		Dim sourcePath, destPath
		sourcePath = Wscript.Arguments(0)
		destPath = Wscript.Arguments(1)
		If Not (FSO.FolderExists(sourcePath)) Then
			'Display error
			wscript.echo &quot;Source path was not found!&quot;
			Exit Sub
		End If
		If Not (FSO.FolderExists(destPath)) Then
			'Display error
			wscript.echo &quot;Destination path was not found!&quot;
			'End this execution run
			Exit Sub
		End If
		'Do the move
		wscript.echo MoveShare(sourcePath, destPath)
	End If
End Sub

'Handles the double click run of the script
Sub InitWindow()
	Dim sourcePath, destPath, msgboxRetval
	'Get source path from user
	sourcePath = InputBox(&quot;This script will move an entire shared folder to a new location withou losing the share!&quot; &amp; vbNewLine &amp; vbNewLine &amp; &quot;Enter path of share:&quot;, &quot;Move Share Script&quot;)
	'Validate source path
	If (sourcePath = &quot;&quot;) Then
		'Display error and end it
		msgboxRetval = MsgBox(&quot;Source path not entered or cancel was clicked!&quot;, vbOKOnly+vbCritical, &quot;Source path error!&quot;)
		Exit Sub
		'Check that path exists
		If Not (FSO.FolderExists(sourcePath)) Then
			'Display error
			msgboxRetval = MsgBox(&quot;Source path was not found!&quot;, vbRetryCancel+vbCritical+vbDefaultButton1, &quot;Source path error!&quot;) 
			'Check if user wants to retry
			If (msgboxRetval = 4) Then
			End If
			'End this execution run
			Exit Sub
		End If
	End if
	'Get destination path from user
	destPath = InputBox(&quot;Enter destination path of share:&quot;, &quot;Move share script&quot;)
	'Validate destination path
	If (destPath = &quot;&quot;) Then
		'Display error and end it
		msgboxRetval = MsgBox(&quot;Destination path not entered or cancel was clicked!&quot;, vbOKOnly+vbCritical, &quot;Destination path error!&quot;)
		Exit Sub
		'Check that path exists
		If Not (FSO.FolderExists(destPath)) Then
			'Display error
			msgboxRetval = MsgBox(&quot;Destination path was not found!&quot;, vbRetryCancel+vbCritical+vbDefaultButton1, &quot;Destination path error!&quot;) 
			'Check if user wants to retry
			If (msgboxRetval = 4) Then
			End If
			'End this execution run
			Exit Sub
		End If
	End If
	'We are all good to go, lets move
	wscript.echo MoveShare(sourcePath, destPath)
End Sub	

Function MoveShare(sourcePath, destPath)
	Dim retval, objWMI, objInstances, objInstance, sourceFolder, sourceName, AccessMask, AllowMaximum, Caption, Description, MaximumAllowed, Name, shareType, delRetval, objNewShare, createRetval

	'Add a \ to the destpath, making shure it will be treated as a folder
	destPath = destPath &amp; &quot;\&quot;
	'Get the shares
	Set objWMI = GetObject(&quot;winmgmts://./root\cimv2&quot;)
	Set objInstances = objWMI.InstancesOf(&quot;Win32_Share&quot;,48)
	'Get the correct folder path with upper and lower cases
	Set sourceFolder = FSO.GetFolder(sourcePath)
	sourcePath = sourceFolder.Path
	sourceName = sourceFolder.Name
	'Get the share
	'On Error Resume Next
	For Each objInstance in objInstances
		If(objInstance.Path = sourcePath) Then
			'Store all the options
			With objInstance
				AccessMask = .AccessMask
				AllowMaximum = .AllowMaximum
				Caption = .Caption
				Description = .Description
				MaximumAllowed = .MaximumAllowed
				Name = .Name
				shareType = .Type
			End With
			'Delete the share
			delRetval = objInstance.Delete()
			'Check that delete worked
			If Not(delRetval = 0) Then
				retval = &quot;Error: Unable to delete share!&quot; &amp; vbNewLine &amp; &quot;Error code: &quot; &amp; delRetval
				MoveShare = retval
				Exit Function
			End If
		End If
	On Error Goto 0
	'Move the folder
	FSO.MoveFolder sourcePath, destPath
	'Setup the share in the new location
	Set objNewShare = objWMI.Get(&quot;Win32_Share&quot;) 
	createRetval = objNewShare.Create(destPath &amp; sourceName, Name, shareType, MaximumAllowed, Description, null, null)
	'Check that the new share was created
	If Not(createRetval = 0) Then
		retval = &quot;Error: New share not created!&quot;
	End If
	'Return result
	MoveShare = retval
End Function	

PSC: Basic script

So the challenge was accepted! Follow it here! One of the conditions was that the script has to perform like a program or command line tool to be exact! But why go for one when you can go for both! Still not sure what the final script will do but I have to detect how the script is started and act accordingly.

I always use Option explicit, makes troubleshooting so much easier!

Option explicit

Check if the script is running from console or windows mode by checkking the WScript.FullName string.

If InStr(1, WScript.FullName, "cscript", vbTextCompare) Then
ElseIf InStr(1, WScript.FullName, "wscript", vbTextCompare) Then
	' Hope this never fires! 🙂
    wscript.echo "How the hell did you start this script?"
End If

Sub for handling the command line version. Checks for command line arguments, if none displays help.

Sub InitConsole()
	'Check if we have any parameters
	If Wscript.Arguments.Count = 0 then
		'Display help
		wscript.echo "You didn't supply any command line parameters... At least one parameter is required!"
		'Verrify the parameters
		wscript.echo "You supplied " &amp; Wscript.Arguments.Count &amp; " arguments!"
	End If
End Sub

Sub for handling if the user double clicks the script...

Sub InitWindow()
	wscript.echo "You are running this by dubble click..."
End Sub

Stay tuned for the next version!