Need a little help woth OAuth and C#

Moderator: Telldus

Post Reply
MrNorth
Posts: 1
Joined: Fri Mar 17, 2023 9:45 am

Need a little help woth OAuth and C#

Post by MrNorth »

Hi!

I am trying to create some kind of API I can use in a Windows 8 app, for accessing telldus live. Being completly new to OAuth and the documentation is really lacking, I just had to dive into the code.

So, I registered at telldus and got 4 keys, let's call them A, B, C and D

Public key: A
Private key: B
Token: C
Token secret: D

Then, to be able to make queries to the api, I need a header value for "Authentication" that the server will accept. Next step is to get this header value. I decided to use a wrapper for OAuth calls found at http://deanhume.com/Home/BlogPost/a-sim ... with-c-/49

It turned out it wasn't as simple as this page suggested. My code, step by step is:

1)

Code: Select all

var oauth = new OAuth.Manager();
            var rtUrl = "http://api.telldus.com/oauth/requestToken";
            oauth["consumer_key"] = "A";
            oauth["consumer_secret"] = "B";
          
            oauth.AcquireRequestToken(rtUrl, "POST");
This confused me first, because of different namings, the consumer secret is actually the private key. Well, once I solved that small puzzle, the method AcquireRequestToken() executed perfectly.

2) Now I am confused. in my oauth object I now got two different values for token and token_secret than I got from telldus, which ones should I use? I tried the ones already in the object and now proceeded to next step, which is apparently to get an accesstoken:

3)

Code: Select all

  OAuthResponse accessToken = oauth.AcquireAccessToken("http://api.telldus.com/oauth/accessToken", "POST", pin);
I dont really understand this part. Apparently the process is that you should be directed to some website where the user log in and then get a pin in return. I have no plan to use this kind of process, so I am a bit confused how to handle this part. Do I even need to execute this code? It feels like I have everything I need already in the oauth object? How should I handle the "real" app when the user just enters username and password? I guess I need some kind of login method first, to get tokens?

4) Anyhow, the code in 3) just throws an 401 unauthorized at me so I decided to skip this part and try to make a call to the api using this code:

Code: Select all

...
var authzHeader = oauth.GenerateAuthzHeader("http://api.telldus.com/oauth/xml", "GET");
            string res = Get(new Uri("http://api.telldus.com/xml/devices/list"), authzHeader);
}

        public string Get(Uri requestUri, string authHeaders)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
            request.Method = "GET";
            request.ServicePoint.Expect100Continue = false;
            request.ContentType = "x-www-form-urlencoded";
            request.Headers["Authorization"] = authHeaders;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            return GetResponseString(response);
        }

Now I am totally stuck, no matter what combinations I try with using keys and tokens, I just get 401 unauthorized. What do I do wrong? I generate the header, set the Authorization header parameter, and send the request.

Any help is greatly appreciated, all threads in the forum regarding C# has ended unresolved and the documentation doesn't bother with C# at all.

If I can just get pass this step, and be able to send requests to the server, I can easily continue.

HELP!!!!!!
/Henrik
Post Reply