c# - 使用 HttpWebRequest 从 C# 连接到 Tagpacker.com

标签 c# http authorization httpwebrequest

有谁知道我如何发帖到 tagpacker网站(在 .Net 中有以下 API?因为不知何故我不明白对他们服务器的授权...

我目前使用的代码如下:

private readonly HttpWebRequest _httpWebRequest = (HttpWebRequest)
WebRequest.Create("https://tagpacker.com/api/users/{YourUserID}/tags");


    public void Load()
    {
        _httpWebRequest.ContentType = "application/json";
        _httpWebRequest.Method = "POST";

        using (var streamWriter = new StreamWriter(_httpWebRequest.GetRequestStream()))
        {
            var json = "{\"user\":\"test\"," +
                          "\"password\":\"bla\"}";

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }

        var httpResponse = (HttpWebResponse)_httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
        }
    }

但我收到了以下消息:

{
   "status": 401,
   "message": "User not authorized",
   "code": "USER_NOT_AUTHORIZED",
   "url": "/unauthorizedHandler"
}

您从他们那里得到的唯一其他东西是“API 访问”代码,如下所示:1b3f314dd132b0015b5415b1:bd0ffe4b-a01e-4bf5-b1ed-12b24145d12d 其中第一部分是您的用户 ID,第二个是你的 API 代码 我猜?有没有人知道如何使用它? Tagpacker's Api FAQs are here.

最佳答案

tagpacker 团队的官方回答是,您需要在 header 中设置 api key ,如下所示(Java 示例):

URL url = new URL("https://tagpacker.com/api/users/<your_user_id>/links?limit=20");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("api_key", "<your_api_key>");

我将尝试在 C# 中执行此操作,然后再次编辑答案。

关于c# - 使用 HttpWebRequest 从 C# 连接到 Tagpacker.com,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41313219/

相关文章:

c# - 创建既不是 true 也不是 false 的 bool 值

c# - 模型中属性的基于规则的可用值

java - HttpServletResponse.setHeader 方法应该如何处理 null 值?

authentication - 谷歌分析测量协议(protocol)认证?

C# 创建表时,如果我的列/字段名称包含 "incorrect syntax"连字符,我会收到 "-"错误

c# - 具有复杂抽象类型集合的 WCF 抽象基类未包含在服务响应中进行反序列化

javascript - $http.post 给出未捕获的类型错误 : Cannot read property 'post' of undefined in angularjs

http - 使用 Arduino/XMPP 客户端实现家庭自动化

authentication - OAuth 与 OpenID : confused about when to use one of these and why

node.js - 调用 FirestoreAdminClient.exportDocuments 时解析 "7 PERMISSION_DENIED: The caller does not have permission"