Improve company productivity with a Business Account.Sign Up

x
?
Solved

how to call API in Winform C#

Posted on 2013-11-15
10
Medium Priority
?
5,227 Views
Last Modified: 2013-11-19
HI All,

i using third part website to enter my time sheet, now they released their API to call and get the report like below.

Doco: https://github.com/toggl/toggl_api_docs/blob/master/reports.md
.

Now i am trying to create a simple winform in C# and call the Toggl API to display the weekly time report for my time sheet.

Could you please advise me what's the best way to call the API and pull the report from Toggl and view in my winforms ?

Thanks in Advance
0
Comment
Question by:Sha1395
  • 6
  • 4
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39653472
You should be able to use an HttpWebRequest to make the necessary calls to what looks like a  REST web service, as shown in this example:

Make Yahoo! Web Service REST Calls With C#
http://developer.yahoo.com/dotnet/howto-rest_cs.html

using System;  
using System.IO;  
using System.Net;  
using System.Text;  
  
// Create the web request  
HttpWebRequest request = WebRequest.Create("http://developer.yahoo.com/") as HttpWebRequest;  
  
// Get response  
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)  
{  
    // Get the response stream  
    StreamReader reader = new StreamReader(response.GetResponseStream());  
  
    // Console application output  
    Console.WriteLine(reader.ReadToEnd());  
}  

Open in new window

0
 

Author Comment

by:Sha1395
ID: 39655249
Thanks learned one, you help is much appreciated . Here what i found from their link

Im trying to do simple thing call this third party API "https://github.com/toggl/toggl_api_docs/blob/master/reports.md"

The reports API base URL is https://toggl.com/reports/api/v2

Weekly report URL GET https://toggl.com/reports/api/v2/weekly

Detailed report URL: GET https://toggl.com/reports/api/v2/details

Summary report URL: GET https://toggl.com/reports/api/v2/summary

they provide three different API URL for different reports.

All im trying to get the weekly report pass the token.

I just look at their examples but its not very clear to me

This is what i try to do based on their examples

var apiKey="2d1d95cef10e17831ec505e9e6f9f7ea";
var usrSrv = new Toggl.Services.UserService(apiKey); //i have no idea what is this line means ???
var c = usrSrv.GetCurrent();
Console.WriteLine(c.FullName);
Console.WriteLine(c.Email);

Open in new window



They do have more code samples here but its very tough to follow for me  "http://litemedia.info/connect-to-toggl-api-with-net"

Please throw me some light all i want just call that api and pull the report. Thank you so much in advance
0
 

Author Comment

by:Sha1395
ID: 39655292
Here what i tried finally and i got

"The remote server returned an error: (407) Proxy Authentication Required." exception

 HttpWebRequest request
               = WebRequest.Create("https://toggl.com/reports/api/v2/weekly") as HttpWebRequest;
            request.Method = "GET";
            request.Accept = "application/atom+xml";
            request.Headers.Add(HttpRequestHeader.Authorization, "d17816281ed29c7a8460a36bfb59f262");
            // Get response  
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                // Get the response stream  
                StreamReader reader = new StreamReader(response.GetResponseStream());

                // Console application output  
                Console.WriteLine(reader.ReadToEnd());
            }  

Open in new window

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 39655452
Do you have a web proxy within your network?  

Here is one possible solution:

HttpWebRequest WebException: The remote server returned an error: (407) Proxy Authentication Required.
http://blogs.msdn.com/b/jpsanders/archive/2009/03/24/httpwebrequest-webexcepton-the-remote-server-returned-an-error-407-proxy-authentication-required.aspx
0
 

Author Comment

by:Sha1395
ID: 39655481
Thank again TheLearnedOne, i will change the proxy settings in my config and update ,Thanks again.
0
 

Author Comment

by:Sha1395
ID: 39657994
I changed the code like below

 // create request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.toggl.com/api/v2/me");
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://toggl.com/reports/api/v2/details");
            IWebProxy theProxy = request.Proxy;
            if (theProxy != null)
            {
                theProxy.Credentials = CredentialCache.DefaultCredentials;
            }
            CookieContainer cookies = new CookieContainer();
            request.UseDefaultCredentials = true;
            request.CookieContainer = cookies;

            // build authentication token
            //string authToken = "15869be800840d58ed4ec1a9e5e32235:api_token";
            string authToken = "15869be800840d58ed4ec1a9e5e32235";

            // encode to Base64
            byte[] bytes = Encoding.ASCII.GetBytes(authToken);
            authToken = Convert.ToBase64String(bytes);

            // write the "Authorization" header
            request.Headers.Add("Authorization", "Basic " + authToken);
           //request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
            request.Method = "GET";

            // get the response
            WebResponse response = request.GetResponse();

Open in new window



App Config :

<configuration>
  <system.net>
    <defaultProxy useDefaultCredentials="true">
     </defaultProxy>
  </system.net>
</configuration>

Open in new window

and added app config what you mentioned in the previous post but i'm getting new exception

"The remote server returned an error: (403) Forbidden.".

here is the account details of my test account in toggl

UserName :Test1395@gmil.com
Pwd:Test1395
Token:c620724108921c4530e745798ed19104

Thanks
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39658341
The only thing that I can see is the you are using the ASCII encoder, and I would think that you would need to use the UTF-8 encoder:

 var bytes = Encoding.Utf8.GetBytes(authToken);
0
 

Author Comment

by:Sha1395
ID: 39658346
Thanks for all your help LearnedOne, you always rock. Finally figured out and its working.

here is the code
 // create request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.toggl.com/api/v8/me");
            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://toggl.com/reports/api/v2/details");
            IWebProxy theProxy = request.Proxy;
            if (theProxy != null)
            {
                theProxy.Credentials = CredentialCache.DefaultCredentials;
            }
            CookieContainer cookies = new CookieContainer();
            request.UseDefaultCredentials = true;
            request.CookieContainer = cookies;
            request.ContentType = "application/json";
            request.CookieContainer = cookies;
            // build authentication token
            //string authToken = "15869be800840d58ed4ec1a9e5e32235:api_token";
            string authToken = "c620724108921c4530e745798ed19104:api_token";

            // encode to Base64
            byte[] bytes = Encoding.ASCII.GetBytes(authToken);
            authToken = Convert.ToBase64String(bytes);

            // write the "Authorization" header
            request.Headers.Add("Authorization", "Basic " + authToken);
           //request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
            request.Method = "GET";

            // get the response
            //WebResponse response = request.GetResponse();
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                Console.Write(reader.ReadToEnd());
            }

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39659192
OK, so you had the wrong auth token...that would explain it!!
0
 

Author Comment

by:Sha1395
ID: 39661044
No i reset the authentication code again to check that helps me to get thru but the real catch is

https://www.toggl.com/api/v2/me to
https://www.toggl.com/api/v8/me

 request.ContentType = "application/json";

from their link

The API Format

The API accepts only JSON requests. Please make sure you're setting Content-type: application/jsonin your request header. Each request returns a JSON-encoded body.

The result of each action is communicated via standard HTTP response codes.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …
If you are looking for an automated tool which can generate reports for Outlook emails and other items from PST file, then you can go for Kernel PST Reporter tool. The reports which are created by this tool are helpful to analyze and understand PST …

602 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question