Monday 1 October 2007

Digg API and .NET


I have being trying to develop a simple console application, which utilises Digg API in the Boo programming language.

This is the source code I have for my application:

/*
Simple application to access the Digg API
Version: 1.0
Language: Boo
Environment: SharpDevelop
Author: Samuel Saint-Pettersen
---------------------------------------------------------------------
Prerequisites:
* Boo libraries (included with)
* Microsoft .NET 2.0 Framework
---------------------------------------------------------------------
*/
namespace DiggAPI

import System
import System.Collections
import System.IO
import System.Net
import System.Text
import System.Xml
import System.Web

print "Digg API application\n"

// base request url
burl = "http://services.digg.com/stories/topic/gadgets?count=3&appkey="

// encode appkey
appkey = HttpUtility.UrlEncode("http://apidoc.digg.com")

// full request url
address = burl + appkey + "&type=xml"

// create request
request = cast(HttpWebRequest, WebRequest.Create(address))

// method
request.Method = "GET"

// get reponse
response = cast(HttpWebResponse, request.GetResponse())

// get response stream into reader
reader = StreamReader(response.GetResponseStream(), Encoding.UTF8)

// print response
print reader.ReadToEnd()
print "\n"

// clean up
reader.Close()

// wait for key press
print "Press any key to continue...\n"
Console.ReadKey(true)


For some reason, when I run this application I always get a Forbidden (403) error returned.

Weird? I'm sure I've got the the request URL correct. I get this error whether or not I encoded the appkey.

I'm sure this is some sort of odd behaviour on .NET's part, so I decided to try using the Digg API with something else.

If you know what the the problem is with the .NET application, please leave me a comment or e-mail


EDIT 10/03/2007:

Timothy Parez helped me out. The solution was to specify the
UserAgent. So I used:


request.UserAgent = appkey;


I used my encoded appkey for the the user agent string. However, in
the Digg API's current state it does not seem to matter whether or not
the appkey is encoded. But I might as well encode it in case they
change this.

Thanks, Timothy.

Digg This Story

4 comments:

Timothy Parez said...

I will test this using C# on Windows, Mac and Linux. If you send me a compiled version of your boo code I'll test it on these platforms as well. That way we can figure out if this is a boo compiler problem, a Framework problem or simply a coding problem.

Timothy Parez said...

Hi,

Ok the problem can be solved very easily.

Set the UserAgent for the request

request.UserAgent = "yourappname";

Sam Saint-Pettersen said...

Thanks, Timothy.
Worked a charm :D

avsaotel said...

Thanksss admin nice siteeeeee niceeeeeeee posttttt
tekirdağ koltuk yıkama
tekirdağ halı yıkama
tekirdağ halı yıkama
tekirdağ halı yıkama
tekirdağ çilingir
okey sitesi açmak

tekirdağ halıyıkama
ankara halı yıkama
tekirdağ temizlik
ankara tel örgü
mankenler
cast
su pompası
ısı yalıtım
kilo vermek