【问题标题】:Authenticating with Clockify API?使用 Clockify API 进行身份验证?
【发布时间】:2019-04-11 21:30:07
【问题描述】:

我正在开发一个需要使用 Clockify API 的新应用程序。当我为概念验证制作测试应用程序时,我注意到我收到了 401 错误,这是对使用其基本功能之一的响应,即按工作空间获取客户。我是否缺少身份验证的内容?我需要在我的个人资料上允许设置吗?我得到的错误是:System.Net.WebException:'远程服务器返回错误:(401)未经授权。'谢谢。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace Api
{
    public class ApiHelper
    {
        public static HttpClient ApiClient { get; set; } = new HttpClient();

        public static void InitializeClient(string username, string password)
        {
            ApiClient = new HttpClient();
            ApiClient.BaseAddress = new Uri("https://api.clockify.me/api/");
            ApiClient.DefaultRequestHeaders.Accept.Clear();
            ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }

        public static void GetClientsFromWorkspace(string workspace)
        {
            ApiClient.DefaultRequestHeaders.Add("X-Api-Key", "*********");
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.clockify.me/api/workspaces/" + workspace + "/clients");
            httpWebRequest.ContentType = "text/json";
            httpWebRequest.Method = "GET";
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

        }
    }
}

【问题讨论】:

  • 嘿,我已经为 Clockify API 创建了一个库。你可以找到它here

标签: c# json api cloud clockify


【解决方案1】:

您在 ApiClient 上设置了 api 密钥标头,但随后使用新创建的HttpWebRequest 发出请求,该HttpWebRequest 没有所需的 api 密钥标头。

您应该使用ApiClient 提出请求或将X-Api-Key 标头添加到HttpWebRequest,如下所示:

httpWebRequest.Headers.Add(“X-Api-Key”, “********”)

【讨论】:

  • 谢谢!这让我解决了这个问题。我正在研究中收到 500 条回复,但这让我获得了授权。
猜你喜欢
  • 2016-08-30
  • 1970-01-01
  • 2017-01-21
  • 2016-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-17
相关资源
最近更新 更多