gpt4 book ai didi

c# - 收获(时间卡应用程序)API

转载 作者:行者123 更新时间:2023-11-30 13:50:39 25 4
gpt4 key购买 nike

Harvest是我在工作中使用的时间跟踪应用程序。虽然 Web UI 非常简单,但我想添加一些自定义功能。我注意到他们有一个 API ...所以我想用 C# 为它制作一个自定义桌面客户端。

只看页面,信息量不大。您可以找到的 C# 示例(在进行一些挖掘之后)也无济于事。那么...究竟如何将 API 与 C# 结合使用?

Link to API page

任何帮助将不胜感激:)

最佳答案

Harvest 正在使用 REST API ,那么您所做的就是对服务器上的网址执行获取/放置/发布请求,它将返回结果(通常采用 XML 或 JSON(在本例中显示为 XML)的格式)。快速谷歌搜索返回 this tutorial关于如何使用 REST API,希望这足以满足您的需求。如果没有,请随时向我们询问您在使用 REST 和 C# 时遇到的具体问题

在这里,我将尝试向他们的样本中添加更多评论:

using System;
using System.Net;
using System.IO;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

class HarvestSample
{
//This is used to validate the certificate the server gives you,
//it allays assumes the cert is valid.
public static bool Validator (object sender, X509Certificate certificate,
X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}

static void Main(string[] args)
{
//setting up the initial request.
HttpWebRequest request;
HttpWebResponse response = null;
StreamReader reader;
StringBuilder sbSource;
//1. Set some variables specific to your account.
//This is the URL that you will be doing your REST call against.
//Think of it as a function in normal library.
string uri = "https://yoursubdomain.harvestapp.com/projects";
string username="youremail@somewhere.com";
string password="yourharvestpassword";
string usernamePassword = username + ":" + password;

//This checks the SSL cert that the server will give us,
//the function is above this one.
ServicePointManager.ServerCertificateValidationCallback = Validator;

try
{
//more setup of the connection
request = WebRequest.Create(uri) as HttpWebRequest;
request.MaximumAutomaticRedirections = 1;
request.AllowAutoRedirect = true;

//2. It's important that both the Accept and ContentType headers
//are set in order for this to be interpreted as an API request.
request.Accept = "application/xml";
request.ContentType = "application/xml";
request.UserAgent = "harvest_api_sample.cs";
//3. Add the Basic Authentication header with username/password string.
request.Headers.Add("Authorization", "Basic " + Convert.
ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
//actually perform the GET request
using (response = request.GetResponse() as HttpWebResponse)
{
//Parse out the XML it returned.
if (request.HaveResponse == true && response != null)
{
reader = new StreamReader(response.GetResponseStream(),
Encoding.UTF8);
sbSource = new StringBuilder(reader.ReadToEnd());
//4. Print out the XML of all projects for this account.
Console.WriteLine(sbSource.ToString());
}
}
}
catch (WebException wex)
{
if (wex.Response != null)
{
using (HttpWebResponse errorResponse = (HttpWebResponse)wex.Response)
{
Console.WriteLine(
"The server returned '{0}' with the status code {1} ({2:d}).",
errorResponse.StatusDescription, errorResponse.StatusCode,
errorResponse.StatusCode);
}
}
else
{
Console.WriteLine( wex);
}
}
finally
{
if (response != null) { response.Close(); }
}
}
}

关于c# - 收获(时间卡应用程序)API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5821808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com